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

Do not overwrite route_param with a regular one if they share same name #2378

Merged
merged 1 commit into from
Apr 1, 2024
Merged

Do not overwrite route_param with a regular one if they share same name #2378

merged 1 commit into from
Apr 1, 2024

Conversation

arg
Copy link
Contributor

@arg arg commented Nov 23, 2023

#2326 changed the behavior of params merging. Before v1.8.0 the following route:

resources :users do
  route_param :id do
    get do
      user = User.find(params[:id])
      { id: user.id }
    end
  end
end

would return { id: 12345 } if called as

GET /users/12345?id=1

Now, since v1.8.0, it returns { id: 1 } as id passed in params overwrites route_param.

This PR restores the previous behavior.

@arg arg marked this pull request as ready for review November 23, 2023 10:30
Copy link
Member

@dblock dblock left a comment

Choose a reason for hiding this comment

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

Thanks! See below.

lib/grape/extensions/hash.rb Outdated Show resolved Hide resolved
@arg arg requested a review from dblock November 23, 2023 15:08
@arg
Copy link
Contributor Author

arg commented Nov 24, 2023

@dblock could you please take a look, I made all the requested changes but PR is still in "changes requested" state for some reason.

Copy link
Member

@dblock dblock left a comment

Choose a reason for hiding this comment

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

Code looks good.

Looking at this deeper, it sounds like we're changing the undocumented/undefined behavior of a regular param overriding a route param before?

  1. Is that the right thing to do? Why wouldn't a regular parameter specified by the caller override a route parameter?
  2. Does anything change if I declare a regular parameter over a route parameter for the get API in your example? Add a spec for that.
  3. Document this behavior in README.
  4. This is a behavior change, but not breaking since it wasn't documented. Either way add a section to UPGRADING.

@dblock
Copy link
Member

dblock commented Dec 6, 2023

@arg want to finish this?

@arg
Copy link
Contributor Author

arg commented Dec 6, 2023

@arg want to finish this?

Yes, I will. A bit busy now but will try to find some time in the next few days.

@DmytroVasin
Copy link

@dblock
I encountered the same issue, and if you don't mind, here's what I've found:

Why wouldn't a regular parameter specified by the caller override a route parameter?

Hashie::Mash, HashWithIndifferentAccess and Hash (prior 1.8.0) follow a similar logic: rack_params are taken as a base and overwritten with grape_routing_args (thus, grape_routing_args is strongly preferred).

NOTE:
Old deep symbolize behavior,
is functionally equivalent to deep_symbolize_keys

deep_symbolize_keys_in({"id" => nil}.merge(id: '123'))
#=> {:id=>"123"}

{"id" => nil}.merge(id: '123').deep_symbolize_keys
#=> {:id=>"123"}

However
In Use Active support functions we replaced deep_symbolize_keys_in with deep_symbolize_keys! (with a bang), that has a bit different behavior

{"id" => nil}.merge(id: '123').deep_symbolize_keys!
#=> {:id=>nil}

The priority works for Hashie::Mash and HashWithIndifferentAccess perfectly, when Hash relies on a sequence of arguments.
I agree that it was not documented, but the idea of priority of params was broken by our changes

@dblock
Copy link
Member

dblock commented Jan 4, 2024

@DmytroVasin thanks for the deep dive, makes sense, let's get this PR finished and make sure we have the right specs - is there anything else missing here?

@arg
Copy link
Contributor Author

arg commented Jan 4, 2024

@DmytroVasin thanks for the deep dive, makes sense, let's get this PR finished and make sure we have the right specs - is there anything else missing here?

@dblock so what are the changes to make? Just the API test and README note?

@dblock
Copy link
Member

dblock commented Jan 4, 2024

I think so, basically what I said in #2378 (review). Thanks for picking this up!

@DmytroVasin
Copy link

@arg Any updates on this?

Sorry for disturbing you, but I can proceed with completing this MR if you don't mind

@arg
Copy link
Contributor Author

arg commented Feb 10, 2024

@dblock @DmytroVasin sorry, have been quite busy the last couple of months. So anyways, I added test to cover scenario when both route_param and regular param are defined with same nave, also added README and UPGRADING entries. Please review

Copy link
Member

@dblock dblock left a comment

Choose a reason for hiding this comment

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

Thanks! I have some more asks, mostly in README/UPGRADING.

README.md Outdated Show resolved Hide resolved
README.md Outdated Show resolved Hide resolved
README.md Outdated Show resolved Hide resolved
UPGRADING.md Outdated Show resolved Hide resolved
lib/grape/extensions/hash.rb Outdated Show resolved Hide resolved
@DmytroVasin
Copy link

@arg Any updates on this?

Sorry for annoying you :)

@dblock
Copy link
Member

dblock commented Mar 29, 2024

@DmytroVasin want to finish this PR for @arg?

@arg
Copy link
Contributor Author

arg commented Mar 29, 2024 via email

@arg arg requested a review from dblock March 30, 2024 07:58
@arg
Copy link
Contributor Author

arg commented Mar 30, 2024

It looks like all requested changes are applied, please review

Copy link
Member

@dblock dblock left a comment

Choose a reason for hiding this comment

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

Spotted one typo, otherwise good to go!

UPGRADING.md Outdated Show resolved Hide resolved
UPGRADING.md Outdated Show resolved Hide resolved
@arg
Copy link
Contributor Author

arg commented Apr 1, 2024

@dblock ready to merge

@dblock
Copy link
Member

dblock commented Apr 1, 2024

Thanks for the good work and for hanging in here with my OCD!

@dblock dblock merged commit 5121279 into ruby-grape:master Apr 1, 2024
39 checks passed
@arg arg deleted the fix_params_merging branch April 2, 2024 05:54
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.

3 participants