Skip to content

Commit

Permalink
Fixes #2347 - Correct full path building for lateral scopes (#2469)
Browse files Browse the repository at this point in the history
* add spec for renamed parameter in given block

* Fix the full_path for the lateral scope

* Update CHANGELOG.md

---------

Co-authored-by: Boris Drovnin <Boris.Drovnin@bia-tech.ru>
  • Loading branch information
numbata and Boris Drovnin committed Jul 7, 2024
1 parent 5affa8f commit da9815d
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 2 deletions.
2 changes: 1 addition & 1 deletion .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ Metrics/BlockLength:
- spec/**/*_spec.rb

Metrics/ClassLength:
Max: 300
Max: 305

Metrics/CyclomaticComplexity:
Max: 15
Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
8 changes: 7 additions & 1 deletion lib/grape/validations/params_scope.rb
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,13 @@ def push_declared_params(attrs, **opts)
#
# @return [Array<Symbol>] 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
Expand Down
26 changes: 26 additions & 0 deletions spec/grape/endpoint/declared_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit da9815d

Please sign in to comment.