Skip to content

Commit 2e8a8b0

Browse files
committed
fix(#2195): Fix declared method for hash params with overlapping names
1 parent b34c722 commit 2e8a8b0

File tree

3 files changed

+5
-3
lines changed

3 files changed

+5
-3
lines changed

CHANGELOG.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
#### Fixes
88

9-
* Your contribution here.
9+
* [#2372](https://github.com/ruby-grape/grape/pull/2372): Fix `declared` method for hash params with overlapping names - [@jcagarcia](https://github.com/jcagarcia).
1010

1111
### 2.0.0 (2023/11/11)
1212

lib/grape/dsl/inside_route.rb

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

100100
route_options_params = options[:route_options][:params] || {}
101101
type = route_options_params.dig(key, :type)
102-
has_children = route_options_params.keys.any? { |k| k != key && k.start_with?(key) }
102+
has_children = route_options_params.keys.any? { |k| k != key && k.start_with?("#{key}[") }
103103

104104
if type == 'Hash' && !has_children
105105
{}

spec/grape/endpoint/declared_spec.rb

+3-1
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ def app
3939
optional :empty_arr, type: Array
4040
optional :empty_typed_arr, type: Array[String]
4141
optional :empty_hash, type: Hash
42+
optional :empty_hash_two, type: Hash
4243
optional :empty_set, type: Set
4344
optional :empty_typed_set, type: Set[String]
4445
end
@@ -122,7 +123,7 @@ def app
122123
end
123124
get '/declared?first=present'
124125
expect(last_response.status).to eq(200)
125-
expect(JSON.parse(last_response.body).keys.size).to eq(11)
126+
expect(JSON.parse(last_response.body).keys.size).to eq(12)
126127
end
127128

128129
it 'has a optional param with default value all the time' do
@@ -201,6 +202,7 @@ def app
201202

202203
body = JSON.parse(last_response.body)
203204
expect(body['empty_hash']).to eq({})
205+
expect(body['empty_hash_two']).to eq({})
204206
expect(body['nested']).to be_a(Hash)
205207
expect(body['nested']['empty_hash']).to eq({})
206208
expect(body['nested']['nested_two']).to be_a(Hash)

0 commit comments

Comments
 (0)