Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
olbrich opened this issue Mar 5, 2018 · 9 comments

Comments

@olbrich
Copy link

olbrich commented Mar 5, 2018

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"]
@dramalho
Copy link

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

@dramalho
Copy link

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

@olbrich
Copy link
Author

olbrich commented Jul 18, 2018

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.

@dramalho
Copy link

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

@swistaczek
Copy link

Hey, this problem still occurs :(

@klondaiker
Copy link

I solved the problem through

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

@dramalho
Copy link

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
Copy link

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

minshi123 pushed a commit to minshi123/gitlab that referenced this issue Aug 27, 2020
Fix problem caching Grape::Entity
- This ensures Marshall.dump does not have issues
  with the Grape::Entity object we are caching via Rails.cache
  Reference: ruby-grape/grape-entity#299

Fix missing params for OccurenceCache init

Include force flag in #fetch params

Make sure hash has indifferent access
@pacoluissl
Copy link

This issue just happened to me today. After some hours, I could solve it thanks to this comment: #299 (comment)
That being said, it would be great if this issue is solved :)
Thanks in advance.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants