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

Refactor route and namespace combination logic #926

Merged
merged 1 commit into from
Apr 30, 2024

Conversation

numbata
Copy link
Contributor

@numbata numbata commented Apr 26, 2024

Motivation

The current implementation of route and namespace combinations uses @target_class.combined_* accessors that inadvertently trigger hash copies due to transform_values. This leads to inefficiencies and unexpected behavior where changes to supposedly original hashes do not persist.

Solution

Shortly

The proposed refactor avoids using @target_class.combined_* until the final step, instead using local variables to accumulate and combine routes and namespaces directly. This change prevents premature hash copying and streamlines the combination process, ensuring that changes are properly reflected and improving overall performance.

TLTR;

All @target_class.combined_* calls go through the add_setup method of the grape gem (caused by override_all_methods!). In the depths of the add_setup method it does:

argument.transform_values { |value| evaluate_arguments(configuration, value).first }

The transform_values makes a copy of the original hash object and returns it. This leads to unexpected behavior in cases such as:

@target_class.combined_routes[resource] ||= []

Where expected flow:

  1. The @target_class.combined_routes returns an original hash.
  2. The empty array sets under the resource key on the original hash.
  3. The original combined_routes is filled with a new key-value.

But actual flow:

  1. The @target_class.combined_routes creates a copy of the empty hash
  2. The empty array is set to the empty hash under the resource key
  3. The original combined_routes is still an empty hash.

@numbata numbata marked this pull request as ready for review April 26, 2024 21:32
@numbata numbata changed the title Avoid unnecessary add_setup calls Refactor route and namespace combination logic Apr 26, 2024
@numbata
Copy link
Contributor Author

numbata commented Apr 30, 2024

@LeFnord just a friendly reminder, no rush.

Copy link
Member

@LeFnord LeFnord left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

awesome … thanks @numbata

@LeFnord LeFnord merged commit 0b969bc into ruby-grape:master Apr 30, 2024
21 checks passed
@numbata
Copy link
Contributor Author

numbata commented Apr 30, 2024

Ouch. I forgot about changelog 🙈

@LeFnord
Copy link
Member

LeFnord commented Apr 30, 2024

no problem … I'll add the entry by the next release

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

Successfully merging this pull request may close these issues.

2 participants