grape-entity icon indicating copy to clipboard operation
grape-entity copied to clipboard

serializing Grape::Entity::Exposure::NestingExposure::OutputBuilder fails in 0.7.0

Open olbrich opened this issue 7 years ago • 9 comments

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"]

olbrich avatar Mar 05 '18 22:03 olbrich

I'm getting a different error actually TypeError: no _dump_data is defined for class Proc

dramalho avatar Jul 18 '18 10:07 dramalho

But same scenario, all my Rails.cache blocks with entities started failing (well, specs did :) )

dramalho avatar Jul 18 '18 10:07 dramalho

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.

olbrich avatar Jul 18 '18 13:07 olbrich

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

dramalho avatar Jul 18 '18 13:07 dramalho

Hey, this problem still occurs :(

swistaczek avatar Sep 19 '18 14:09 swistaczek

I solved the problem through

class Grape::Entity
  def as_json(*args)
    serializable_hash(*args).as_json
  end
end

klondaiker avatar Sep 10 '19 14:09 klondaiker

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

dramalho avatar Jan 13 '20 13:01 dramalho

@dblock any inputs? I'm sure this happens often enough

dramalho avatar Jan 13 '20 13:01 dramalho

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.

pacoluissl avatar Oct 07 '22 16:10 pacoluissl