grape-entity
grape-entity copied to clipboard
serializing Grape::Entity::Exposure::NestingExposure::OutputBuilder fails in 0.7.0
When attempting to serialize a Grape::Entity::Exposure::NestingExposure::OutputBuilder in 0.7.0 using Marshal.dump (for caching), we encounter an error like TypeError: can't dump IO.
Rails.cache.fetch(key) do
present(entity_items).as_json # this used to just give a Hash which could be marshaled properly
end
This error seems to stem from the options hash in the entity. When it contains the request environment (in the :env key), there are objects that cannot be marshaled.
In my testing (in development mode) the following keys contain unmarshalable items.
opts[:env].reject {|k,v| Marshal.dump(v) rescue false }.keys
["rack.errors",
"puma.socket",
"rack.hijack",
"puma.config",
"action_dispatch.logger",
"action_dispatch.backtrace_cleaner",
"action_dispatch.routes",
"action_dispatch.remote_ip",
"rack.session",
"rack.session.options",
"warden",
"grape.routing_args",
"api.endpoint"]
I'm getting a different error actually TypeError: no _dump_data is defined for class Proc
But same scenario, all my Rails.cache blocks with entities started failing (well, specs did :) )
FYI, I ended up working around this by actually generating the json string instead of the intermediate json-like hash and then caching the raw value. That works better in our case anyway since all I'm doing after the cache call is returning raw json.
Yeah we do that most of the time but occasionally we need to massage the hash in runtime (when it's still worth to pull stuff from the cache) and this kind of ruins that strategy. I followed down the rabbit hole a little bit but not so much that I can have a clear clue as to what's happening
annoying :)
thanks Kevin
Hey, this problem still occurs :(
I solved the problem through
class Grape::Entity
def as_json(*args)
serializable_hash(*args).as_json
end
end
Any chance of this ever going in ? I'll patch my own code but it would be nice to make it official .
As a hint for specs, anything that does Marshal.dump on these objects explodes
@dblock any inputs? I'm sure this happens often enough
This issue just happened to me today. After some hours, I could solve it thanks to this comment: https://github.com/ruby-grape/grape-entity/issues/299#issuecomment-529976379 That being said, it would be great if this issue is solved :) Thanks in advance.