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 ordering issues between as: and default: validations #2177

Merged
merged 5 commits into from
May 5, 2021
Merged
Show file tree
Hide file tree
Changes from all 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
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@

#### Fixes

* [#2176](https://github.com/ruby-grape/grape/pull/2176): Fixes issue-2175 - options call failing if matching all routes - [@myxoh](https://github.com/myxoh).
* [#2176](https://github.com/ruby-grape/grape/pull/2176): Fix: OPTIONS fails if matching all routes - [@myxoh](https://github.com/myxoh).
* [#2177](https://github.com/ruby-grape/grape/pull/2177): Fix: `default` validator fails if preceded by `as` validator - [@Catsuko](https://github.com/Catsuko).
* Your contribution here.
### 1.5.3 (2021/03/07)

Expand Down
28 changes: 22 additions & 6 deletions lib/grape/validations/params_scope.rb
Original file line number Diff line number Diff line change
Expand Up @@ -283,16 +283,15 @@ def validates(attrs, validations)

doc_attrs[:documentation] = validations.delete(:documentation) if validations.key?(:documentation)

full_attrs = attrs.collect { |name| { name: name, full_name: full_name(name) } }
@api.document_attribute(full_attrs, doc_attrs)
document_attribute(attrs, doc_attrs)

opts = derive_validator_options(validations)

order_specific_validations = Set[:as]

# Validate for presence before any other validators
if validations.key?(:presence) && validations[:presence]
validate('presence', validations[:presence], attrs, doc_attrs, opts)
validations.delete(:presence)
validations.delete(:message) if validations.key?(:message)
validates_presence(validations, attrs, doc_attrs, opts) do |validation_type|
order_specific_validations << validation_type
end

# Before we run the rest of the validators, let's handle
Expand All @@ -301,8 +300,13 @@ def validates(attrs, validations)
coerce_type validations, attrs, doc_attrs, opts

validations.each do |type, options|
next if order_specific_validations.include?(type)
validate(type, options, attrs, doc_attrs, opts)
end

# Apply as validator last so other validations are applied to
# renamed param
validate(:as, validations[:as], attrs, doc_attrs, opts) if validations.key?(:as)
end

# Validate and comprehend the +:type+, +:types+, and +:coerce_with+
Expand Down Expand Up @@ -464,6 +468,18 @@ def derive_validator_options(validations)
fail_fast: validations.delete(:fail_fast) || false
}
end

def validates_presence(validations, attrs, doc_attrs, opts)
return unless validations.key?(:presence) && validations[:presence]
validate(:presence, validations[:presence], attrs, doc_attrs, opts)
yield :presence
yield :message if validations.key?(:message)
end

def document_attribute(attrs, doc_attrs)
full_attrs = attrs.collect { |name| { name: name, full_name: full_name(name) } }
@api.document_attribute(full_attrs, doc_attrs)
end
end
end
end
22 changes: 22 additions & 0 deletions spec/grape/validations/params_scope_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,28 @@ def initialize(value)
expect(last_response.status).to eq(200)
expect(last_response.body).to eq('{"baz":{"qux":"any"}}')
end

it 'renaming can be defined before default' do
subject.params do
optional :foo, as: :bar, default: 'before'
end
subject.get('/rename-before-default') { params[:bar] }
get '/rename-before-default'

expect(last_response.status).to eq(200)
expect(last_response.body).to eq('before')
end

it 'renaming can be defined after default' do
subject.params do
optional :foo, default: 'after', as: :bar
end
subject.get('/rename-after-default') { params[:bar] }
get '/rename-after-default'

expect(last_response.status).to eq(200)
expect(last_response.body).to eq('after')
end
end

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