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

Fixes #2347 - Correct full path building for lateral scopes #2469

Merged
merged 3 commits into from
Jul 7, 2024
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
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