ruby-grape / ruby-grape/grape-entity

Only Option Not Correct for Nested Value

Open
#181 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

bug?
Dominant language
Ruby
Stars
729
Forks
154
PR merge metrics
No merged PRs in 30d

Description

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.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the nested Foo::Entity exposure through the Bar resource's present @bars.index(:limit => @limit), :only => @only call. Compare the result for [:id, :things, {:foo => [:foo_name]}] with the expected nested foo_name output, and consider the issue complete when the nested selection returns that field rather than only id.

Written by the indexing model from the issue text.

Assessment

Tech stack
ruby
Domain
api
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.