-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
Fix alias on dependent param bug #1810
Conversation
0c51e2b
to
c6e3f23
Compare
Thanks! So what happens if I was using the unaliased param before, that is
I am going to guess that without this fix If that's the case we should either support both or clearly state what happens here in UPGRADING. |
CHANGELOG.md
Outdated
@@ -14,6 +14,7 @@ | |||
* [#1776](/~https://github.com/ruby-grape/grape/pull/1776): Validate response returned by the exception handler - [@darren987469](/~https://github.com/darren987469). | |||
* [#1787](/~https://github.com/ruby-grape/grape/pull/1787): Add documented but not implemented ability to `.insert` a middleware in the stack - [@michaellennox](/~https://github.com/michaellennox). | |||
* [#1788](/~https://github.com/ruby-grape/grape/pull/1788): Fix route requirements bug - [@darren987469](/~https://github.com/darren987469), [@darrellnash](/~https://github.com/darrellnash). | |||
* [#1810](/~https://github.com/ruby-grape/grape/pull/1810): Fix alias on dependent param bug - [@darren987469](/~https://github.com/darren987469). |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe say something more explicit? Eg. "Fix support in given
for aliased params"?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, it is more clear.
Let me describe Without this fix case subject.params do
optional :a, as: :b # 'as: :b' change params form { a: 'something' } to { b: 'something' }
given a: ->(val) { val == 'x' } do # val = params[:a] = nil, condition doesn't meet
requires :c
end
end
subject.get('/') { declared(params) }
get '/', a: 'x'
expect(last_response.status).to eq 400 # fail, since given condition doesn't meet
expect(last_response.body).to eq 'c is missing' Without this fix case subject.params do
optional :a, as: :b
given b: ->(val) { val == 'x' } do # raise Grape::Exceptions::UnknownParameter. @declared_params in grape/lib/grape/dsl/parameters.rb is [:a], which doesn't contain :b. That why this fix try to solve.
requires :c
end
end With this fix case subject.params do
optional :a, as: :b # 'as: :b' change params form { a: 'somthing' } to { b: 'something' }
given a: ->(val) { val == 'x' } do # raise Grape::Exceptions::UnknownParameter. @declared_params in grape/lib/grape/dsl/parameters.rb is now [:b], which doesn't contain :a.
requires :c
end
end With this fix case subject.params do
optional :a, as: :b # 'as: :b' change params form { a: 'somthing' } to { b: 'something' }
given b: ->(val) { val == 'x' } do # val = params[:b]
requires :c
end
end
get '/', a: 'x'
expect(last_response.status).to eq 400 # success, since given condition meets
expect(last_response.body).to eq 'c is missing' I think it makes sense to force to use |
expect do | ||
subject.params do | ||
optional :a, as: :b | ||
given :a do |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I add a spec to show with this fix, given :a
(not the aliased param :b
) case.
Perfect, thanks for the specs. |
Fixes #1808
The root cause is
as: :action
alias would changeparams
from{ type: 'some type' }
to{ action: 'some type' }
in as validator.grape/lib/grape/validations/validators/as.rb
Lines 9 to 12 in 3bb835a
When checking
->(val) { ActionService::ACTIONS.include?(val) }
in the following snippet(line 61), dependency_key is still:type
, so params[:type] is nil because the params is changed in previous validator.grape/lib/grape/validations/params_scope.rb
Lines 57 to 65 in 3bb835a
The idea to fix is let attribute name in
given
the same as the aliased one. That means the usage now becomes: