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

Only Option Not Correct for Nested Value #181

Open
Linell opened this issue Oct 12, 2015 · 2 comments
Open

Only Option Not Correct for Nested Value #181

Linell opened this issue Oct 12, 2015 · 2 comments
Labels

Comments

@Linell
Copy link

Linell commented Oct 12, 2015

I have a couple entities that look like this:

class Foo::Entity < Grape::Entity
  expose :id, :foo_name
end

class Bar::Entity < Grape::Entity
  expose :id, :stuff, :things

  expose :foo, :using => Foo::Entity
end

In the resource for Bar, I'm doing

get '/' do
  present @bars.index(:limit => @limit), :only => @only
end

For the first layer, this works great. For example, passing [:id, :things] to @only works perfectly. Passing nothing to @only results in me getting the full Foo object too, which is exactly what I want.

The issue arises when I pass [:id, :things, {:foo => [:foo_name]}]. Instead of the expected id, things, and foo hash containing foo_name, I get back id, things and a foo hash containing just the foo object's id.

@Linell
Copy link
Author

Linell commented Oct 12, 2015

The problem with my above example lies in the code for should_return_key?.

The only_fields variable, in my case, is this:

{:id => true, :things => true, :foo => [:foo_name]}.

BUT the actual check for whether or not the key should be included is just only_fields.key?(key). Which fails for foo_name, since it isn't a key of only_fields. Anything nested the way the README shows isn't going to work.

Even worse though, is that the id is returning in my above example. This is caused by the fact that it's just looping over all of the keys in each exposure and trying them against only_fields. What that means is anything that shares a key is going to show up (in my case because I want the bar id).

Is there a way to know which object we're comparing the key to? Or maybe even just if we could know if it's supposed to be nested at all?

@Linell
Copy link
Author

Linell commented Oct 13, 2015

So I've narrowed it down a little more. I don't 100% understand everything that's going on, but it looks like when serializable_hash is called for my nested object the options[:only] field (which is correct) is being overridden by the options.merge(runtime_options || {}) line.

If the override wasn't happening, it works correctly. The only_fields variable from my previous comment is populated with just the things it should be and so it works just fine. If I just make opts = options it seems golden.

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

Successfully merging a pull request may close this issue.

2 participants