ruby-grape / ruby-grape/grape-roar
Represented objects via `extend` are not serializable
Nobody has claimed this yet.
- 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
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- 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