Skip to content

Commit

Permalink
Merge pull request ruby-grape#2372 from jcagarcia/issue-2195
Browse files Browse the repository at this point in the history
fix(ruby-grape#2195): Fix `declared` method for hash params with overlapping names
  • Loading branch information
dblock committed Nov 17, 2023
2 parents b34c722 + c480bfd commit 5c237e3
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

#### Fixes

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

### 2.0.0 (2023/11/11)
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 @@ -99,7 +99,7 @@ def handle_passed_param(params_nested_path, has_passed_children = false, &_block

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

if type == 'Hash' && !has_children
{}
Expand Down
4 changes: 3 additions & 1 deletion spec/grape/endpoint/declared_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ def app
optional :empty_arr, type: Array
optional :empty_typed_arr, type: Array[String]
optional :empty_hash, type: Hash
optional :empty_hash_two, type: Hash
optional :empty_set, type: Set
optional :empty_typed_set, type: Set[String]
end
Expand Down Expand Up @@ -122,7 +123,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(11)
expect(JSON.parse(last_response.body).keys.size).to eq(12)
end

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

body = JSON.parse(last_response.body)
expect(body['empty_hash']).to eq({})
expect(body['empty_hash_two']).to eq({})
expect(body['nested']).to be_a(Hash)
expect(body['nested']['empty_hash']).to eq({})
expect(body['nested']['nested_two']).to be_a(Hash)
Expand Down

0 comments on commit 5c237e3

Please sign in to comment.