Skip to content

Commit

Permalink
fix(#2195): Fix declared method for hash params with overlapping names
Browse files Browse the repository at this point in the history
  • Loading branch information
jcagarcia committed Nov 17, 2023
1 parent b34c722 commit c480bfd
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 c480bfd

Please sign in to comment.