From cc998c332ecd75e6dfba801af87760e79f25918a Mon Sep 17 00:00:00 2001 From: Boris Drovnin Date: Tue, 25 Jun 2024 20:42:53 +0300 Subject: [PATCH 1/3] add spec for renamed parameter in given block --- spec/grape/endpoint/declared_spec.rb | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/spec/grape/endpoint/declared_spec.rb b/spec/grape/endpoint/declared_spec.rb index 6bf5d206ca..9b561d886a 100644 --- a/spec/grape/endpoint/declared_spec.rb +++ b/spec/grape/endpoint/declared_spec.rb @@ -829,5 +829,31 @@ expect(JSON.parse(last_response.body)).to match({}) end end + + context 'with a renamed field inside `given` block nested in hashes' do + before do + subject.format :json + subject.params do + requires :a, type: Hash do + optional :c, type: String + given :c do + requires :b, type: Hash do + requires :input_field, as: :output_field + end + end + end + end + subject.post '/test' do + declared(params) + end + end + + it 'renames parameter input_field to output_field' do + post '/test', { a: { b: { input_field: 'value' }, c: 'value2' } } + + expect(JSON.parse(last_response.body)).to \ + match('a' => { 'b' => { 'output_field' => 'value' }, 'c' => 'value2' }) + end + end end end From 1b1eeba56790b9eee5ea7c2c90d70ad3e6e61f6f Mon Sep 17 00:00:00 2001 From: Andrey Subbota Date: Thu, 4 Jul 2024 16:10:21 +0200 Subject: [PATCH 2/3] Fix the full_path for the lateral scope --- .rubocop.yml | 2 +- lib/grape/validations/params_scope.rb | 8 +++++++- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/.rubocop.yml b/.rubocop.yml index 44b9d7e874..432e84f559 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -40,7 +40,7 @@ Metrics/BlockLength: - spec/**/*_spec.rb Metrics/ClassLength: - Max: 300 + Max: 305 Metrics/CyclomaticComplexity: Max: 15 diff --git a/lib/grape/validations/params_scope.rb b/lib/grape/validations/params_scope.rb index ef8d3ec1b7..1d3384883c 100644 --- a/lib/grape/validations/params_scope.rb +++ b/lib/grape/validations/params_scope.rb @@ -190,7 +190,13 @@ def push_declared_params(attrs, **opts) # # @return [Array] the nesting/path of the current parameter scope def full_path - nested? ? @parent.full_path + [@element] : [] + if nested? + (@parent.full_path + [@element]) + elsif lateral? + @parent.full_path + else + [] + end end private From cc54313ef17063d19453b5c8ee052536b83f116c Mon Sep 17 00:00:00 2001 From: Andrey Subbota Date: Thu, 4 Jul 2024 16:46:27 +0200 Subject: [PATCH 3/3] Update CHANGELOG.md --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index abc7852746..634a172f9b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,6 +8,7 @@ * [#2467](https://github.com/ruby-grape/grape/pull/2467): Fix repo coverage - [@ericproulx](https://github.com/ericproulx). * [#2468](https://github.com/ruby-grape/grape/pull/2468): Align `error!` method signatures across different places - [@numbata](https://github.com/numbata). +* [#2469](https://github.com/ruby-grape/grape/pull/2469): Fix full path building for lateral scopes - [@numbata](https://github.com/numbata). * Your contribution here. ### 2.1.2 (2024-06-28)