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(#2195): Fix declared method for hash params with overlapping names #2372

Merged
merged 1 commit into from
Nov 17, 2023
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 @@ -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