Skip to content
  • Sponsor ruby-grape/grape

  • Notifications You must be signed in to change notification settings
  • Fork 1.2k

Commit 44217dd

Browse files
committedOct 9, 2020
Fix declared_params regression with multiple allowed types
Prior to Grape v1.5.0 and #2103, the following would return `nil`: ``` params do optional :status_code, types: [Integer, String] end get '/' do declared_params end ``` However, now it turns an empty `Array`. We restore the previous behavior by not returning an empty `Array` if multiple types are used. Closes #2115
1 parent 02d7113 commit 44217dd

File tree

3 files changed

+14
-2
lines changed

3 files changed

+14
-2
lines changed
 

‎CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#### Fixes
88

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

1112
### 1.5.0 (2020/10/05)
1213

‎lib/grape/dsl/inside_route.rb

+1-1
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ def handle_passed_param(params_nested_path, has_passed_children = false, &_block
9494

9595
if type == 'Hash' && !has_children
9696
{}
97-
elsif type == 'Array' || type&.start_with?('[')
97+
elsif type == 'Array' || type&.start_with?('[') && !type&.include?(',')
9898
[]
9999
elsif type == 'Set' || type&.start_with?('#<Set')
100100
Set.new

‎spec/grape/endpoint/declared_spec.rb

+12-1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ def app
1616
requires :first
1717
optional :second
1818
optional :third, default: 'third-default'
19+
optional :multiple_types, types: [Integer, String]
1920
optional :nested, type: Hash do
2021
optional :fourth
2122
optional :fifth
@@ -96,6 +97,16 @@ def app
9697
expect(JSON.parse(last_response.body)['nested']['fourth']).to be_nil
9798
end
9899

100+
it 'should show nil for multiple allowed types if include_missing is true' do
101+
subject.get '/declared' do
102+
declared(params, include_missing: true)
103+
end
104+
105+
get '/declared?first=present'
106+
expect(last_response.status).to eq(200)
107+
expect(JSON.parse(last_response.body)['multiple_types']).to be_nil
108+
end
109+
99110
it 'does not work in a before filter' do
100111
subject.before do
101112
declared(params)
@@ -113,7 +124,7 @@ def app
113124
end
114125
get '/declared?first=present'
115126
expect(last_response.status).to eq(200)
116-
expect(JSON.parse(last_response.body).keys.size).to eq(10)
127+
expect(JSON.parse(last_response.body).keys.size).to eq(11)
117128
end
118129

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

0 commit comments

Comments
 (0)