ruby-grape / ruby-grape/grape-roar

Represented objects via `extend` are not serializable

Open
#3 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

discuss! new feature
Dominant language
Ruby
Stars
59
Forks
14
PR merge metrics
No merged PRs in 30d

Description

If you put present instance, with: Presenter inside a cache block, it cannot be serialized. That is because its implementation looks like this:

def represent(object, options = {})
    object.extend self
    object
end

The objects gets serialized, but not its class definition with includes the extended methods.

A solution is to roll out a presenter that looks like this:

module Grape
  module Roar
    module Representer
      def self.included(base)
        base.extend(ClassMethods)
      end

      module ClassMethods
        def represent(object, options = {})
          object.extend self
          object.as_json(options) # usually cannot be `to_json` because Grape formatter would re-encode this
        end
      end
    end
  end
end

Maybe this should just be a class called Grape::Roar::Representer::JSON?

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start at the represent(object, options = {}) entry point shown in the issue and reproduce the failure when a represented object is placed in a cache block and serialized. Compare the proposed as_json(options) path with the current extension-based behavior, then verify that the chosen design preserves serialization without Grape re-encoding the result.

Written by the indexing model from the issue text.

Assessment

Tech stack
ruby
Domain
api, backend
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.