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

fix: validators checking aliased param instead of original one #1852

Merged
merged 3 commits into from
Dec 21, 2018
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@
* Your contribution here.
* [#1850](https://github.com/ruby-grape/grape/pull/1850): Adds `same_as` validator - [@glaucocustodio](https://github.com/glaucocustodio).
* [#1833](https://github.com/ruby-grape/grape/pull/1833): Allows to set the `ParamBuilder` globally - [@myxoh](https://github.com/myxoh).
* [#1844](https://github.com/ruby-grape/grape/pull/1844): Fix: enforce `:tempfile` to be a `Tempfile` object in `File` validator - [@Nyangawa](https://github.com/Nyangawa).

#### Fixes

* Your contribution here.
* [#1852](https://github.com/ruby-grape/grape/pull/1852): Validators checking aliased param instead of original one - [@glaucocustodio](https://github.com/glaucocustodio).
* [#1844](https://github.com/ruby-grape/grape/pull/1844): Enforce `:tempfile` to be a `Tempfile` object in `File` validator - [@Nyangawa](https://github.com/Nyangawa).

### 1.2.2 (2018/12/07)

Expand Down
7 changes: 7 additions & 0 deletions lib/grape/validations/params_scope.rb
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,8 @@ def validates(attrs, validations)
# type casted values
coerce_type validations, attrs, doc_attrs, opts

move_alias_to_end(validations)
Copy link
Member

Choose a reason for hiding this comment

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

This isn't reused, and we do a ton of things above, so I would just inline the two lines of the method here.

I think delete also always makes a copy, and we should assume a nil value is a thing, so to be consistent I what we do above:

# explain why
validations[:as] = validations.delete(:as) if validations.key?(:as)

Copy link
Member

@dblock dblock Dec 18, 2018

Choose a reason for hiding this comment

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

Errrr.... re-reading the code we move that :as to the "end". I understand why it works, but feels to me that it's wrong to rely on the order in validations which is not a visibly ordered array or hash.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I agree that relying on the order is not good, but that was the easier way I found to fix the bug so far.

Another approach would be set a variable to let others validators know about alias, but it seems to be a bigger change and I am not sure whether the outcome would be worth..

Please let me know whether you have more ideas.

Copy link
Member

Choose a reason for hiding this comment

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

I just don't think we can use on this approach to work reliably. There're no ordering guarantees. I think extracting as somehow is the proper way of doing it. Give it a try! We're here to help.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Check the new approach now please. Build is failing because of rubocop lib/grape/endpoint.rb:6:3: C: Class has too many lines. [295/288].

Copy link
Member

Choose a reason for hiding this comment

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

Run rubocop -a ; rubocop --auto-gen-config for this.


validations.each do |type, options|
validate(type, options, attrs, doc_attrs, opts)
end
Expand Down Expand Up @@ -431,6 +433,11 @@ def validate_value_coercion(coerce_type, *values_list)
end
end

def move_alias_to_end(validations)
alias_validation = validations.delete(:as)
validations[:as] = alias_validation if alias_validation
end

def extract_message_option(attrs)
return nil unless attrs.is_a?(Array)
opts = attrs.last.is_a?(Hash) ? attrs.pop : {}
Expand Down
10 changes: 10 additions & 0 deletions spec/grape/validations/params_scope_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,16 @@ def initialize(value)
expect(last_response.status).to eq(400)
expect(last_response.body).to eq('foo is empty')
end

it do
subject.params do
requires :foo, as: :bar, allow_blank: false
end
subject.get('/alias-not-blank-with-value') {}
get '/alias-not-blank-with-value', foo: 'any'

expect(last_response.status).to eq(200)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

I get status 400 here with the last_response.body = foo is empty.

As the alias (as) is called before the allow_blank, it deletes the foo key from params and sets params[:bar] with its value.

Given that alias is just for internal usage (inside get block I mean), I think this should not fail, cause the foo param was given as expected. Do you agree?

Copy link
Member

Choose a reason for hiding this comment

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

I'd tend to agree that this looks like a bug to me. Could you open an issue for this?

Copy link
Member

Choose a reason for hiding this comment

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

(Thanks for looking at this!)

end
end

context 'array without coerce type explicitly given' do
Expand Down