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 declared_params regression with multiple allowed types #2116

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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#### Fixes

* Your contribution here.
* [#2115](https://github.com/ruby-grape/grape/pull/2115): Fix declared_params regression with multiple allowed types - [@stanhu](https://github.com/stanhu).

### 1.5.0 (2020/10/05)

Expand Down
2 changes: 1 addition & 1 deletion lib/grape/dsl/inside_route.rb
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ def handle_passed_param(params_nested_path, has_passed_children = false, &_block

if type == 'Hash' && !has_children
{}
elsif type == 'Array' || type&.start_with?('[')
elsif type == 'Array' || type&.start_with?('[') && !type&.include?(',')
[]
elsif type == 'Set' || type&.start_with?('#<Set')
Set.new
Expand Down
13 changes: 12 additions & 1 deletion spec/grape/endpoint/declared_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ def app
requires :first
optional :second
optional :third, default: 'third-default'
optional :multiple_types, types: [Integer, String]
optional :nested, type: Hash do
optional :fourth
optional :fifth
Expand Down Expand Up @@ -96,6 +97,16 @@ def app
expect(JSON.parse(last_response.body)['nested']['fourth']).to be_nil
end

it 'should show nil for multiple allowed types if include_missing is true' do
subject.get '/declared' do
declared(params, include_missing: true)
end

get '/declared?first=present'
expect(last_response.status).to eq(200)
expect(JSON.parse(last_response.body)['multiple_types']).to be_nil
end

it 'does not work in a before filter' do
subject.before do
declared(params)
Expand All @@ -113,7 +124,7 @@ def app
end
get '/declared?first=present'
expect(last_response.status).to eq(200)
expect(JSON.parse(last_response.body).keys.size).to eq(10)
expect(JSON.parse(last_response.body).keys.size).to eq(11)
end

it 'has a optional param with default value all the time' do
Expand Down