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

Representations cannot be serialized/cached

Open esoto opened this issue 10 years ago • 6 comments

Hey guys! I'm having an issue using Grape with cache (Garner) and send the results with grape-entity.

The issue is: the second time I request an object Garner start working, but the call for Grape Entity have a problem, because it sets the options as nil, making grape-entity to throw an error, options is not define.

Versions Grape 0.6.1 Grape-entity 0.4.0 Garner 0.3.2

esoto avatar Jul 07 '14 16:07 esoto

Can you show some code please? What options?

dblock avatar Jul 07 '14 16:07 dblock

well the options variable which is nil is present on the line https://github.com/intridea/grape-entity/blob/master/lib/grape_entity/entity.rb#L394 and actually the error is because the merge can't be done to a nil object

the controller looks like this

route(:get, ':id') do
  cache(bind: { klass: ClassName, object: { id: params[:id] } }) do
     # Some validations
     object = Object.find_by_id(params[:id].to_i)
     Object.represent(object,  :uri=>"some_uri")
  end
end

esoto avatar Jul 07 '14 17:07 esoto

I see. I think the real bug is that a representation cannot be cached (serialized/deserialized). It would be helpful if you could build a failing test within grape-entity's RSpec tests that describes that.

As a workaround you could serialize the JSON if that's your only format, for example, which is probably better anyway for performance, so:

route(:get, ':id') do
  cache(bind: { klass: ClassName, object: { id: params[:id] } }) do
     # Some validations
     object = Object.find_by_id(params[:id].to_i)
     Object.represent(object,  :uri=>"some_uri").as_json
  end
end

dblock avatar Jul 07 '14 17:07 dblock

It's a tradition on the team to give an ice cream for a fix like this, so we own you!! haha We'll use for now the workaround, but I'll make the test for this scenario in my free time.

Thank you very much.

esoto avatar Jul 07 '14 20:07 esoto

So that .as_json worked, right?

dblock avatar Jul 07 '14 20:07 dblock

kind of late, but yes it does worked

esoto avatar Jul 09 '14 15:07 esoto