ruby-grape / ruby-grape/grape-entity

Wrong except option for nested entities

Open
#195 1 comment 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

Good day. I have a problem with except option. If I use it on entity Patient like this

Patient::Entity.represent(patient, except: [testssets: [:patient]]).as_json

all works great, but I have an infinite recursion if use it on opposite entity Testsset

Testsset::Entity.represent(testsset, except: [patient: [:testssets]]).as_json

I think that there's a problem with Exposure::Base#serializeable_value. All works fine in first case because it is one-to-many relation and handled by this code

partial_output.map(&:serializable_hash)

In second case it is handled by this code (with options argument)

partial_output.serializable_hash(options)

And lately options generated for a nested entity are rewritten in Options#merge method like this

options = {except: [:testssets]}
new_options = {except: [{patient: [:testssets]}]}
options.merge(new_options)

So, now we have except fields for a nested entity such as for the parent.
It seems that this code is broken and options hash should be passed in both cases. And except fields shouldn't be merged.

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

Read lib/grape_entity/exposure/base.rb at Exposure::Base#serializeable_value and lib/grape_entity/options.rb at Options#merge, then reproduce both Patient::Entity and Testsset::Entity examples from the issue. Done means nested except options no longer cause infinite recursion and the requested nested fields remain excluded.

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
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.