From 2e8a8b028f2d819be3104a7e9d311cae3fcd2618 Mon Sep 17 00:00:00 2001 From: Juan Carlos Garcia Date: Fri, 17 Nov 2023 22:23:56 +0100 Subject: [PATCH] fix(#2195): Fix `declared` method for hash params with overlapping names --- CHANGELOG.md | 2 +- lib/grape/dsl/inside_route.rb | 2 +- spec/grape/endpoint/declared_spec.rb | 4 +++- 3 files changed, 5 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 8d85e957e9..6c56042c1d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,7 +6,7 @@ #### Fixes -* Your contribution here. +* [#2372](https://github.com/ruby-grape/grape/pull/2372): Fix `declared` method for hash params with overlapping names - [@jcagarcia](https://github.com/jcagarcia). ### 2.0.0 (2023/11/11) diff --git a/lib/grape/dsl/inside_route.rb b/lib/grape/dsl/inside_route.rb index bcfd3e84c8..7ebd6aab8f 100644 --- a/lib/grape/dsl/inside_route.rb +++ b/lib/grape/dsl/inside_route.rb @@ -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 {} diff --git a/spec/grape/endpoint/declared_spec.rb b/spec/grape/endpoint/declared_spec.rb index f4402416d5..b73538fdb9 100644 --- a/spec/grape/endpoint/declared_spec.rb +++ b/spec/grape/endpoint/declared_spec.rb @@ -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 @@ -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 @@ -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)