Skip to content

Commit da9815d

Browse files
numbataBoris Drovnin
and
Boris Drovnin
authored
Fixes #2347 - Correct full path building for lateral scopes (#2469)
* 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>
1 parent 5affa8f commit da9815d

File tree

4 files changed

+35
-2
lines changed

4 files changed

+35
-2
lines changed

.rubocop.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ Metrics/BlockLength:
4040
- spec/**/*_spec.rb
4141

4242
Metrics/ClassLength:
43-
Max: 300
43+
Max: 305
4444

4545
Metrics/CyclomaticComplexity:
4646
Max: 15

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
* [#2467](https://github.com/ruby-grape/grape/pull/2467): Fix repo coverage - [@ericproulx](https://github.com/ericproulx).
1010
* [#2468](https://github.com/ruby-grape/grape/pull/2468): Align `error!` method signatures across different places - [@numbata](https://github.com/numbata).
11+
* [#2469](https://github.com/ruby-grape/grape/pull/2469): Fix full path building for lateral scopes - [@numbata](https://github.com/numbata).
1112
* Your contribution here.
1213

1314
### 2.1.2 (2024-06-28)

lib/grape/validations/params_scope.rb

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -190,7 +190,13 @@ def push_declared_params(attrs, **opts)
190190
#
191191
# @return [Array<Symbol>] the nesting/path of the current parameter scope
192192
def full_path
193-
nested? ? @parent.full_path + [@element] : []
193+
if nested?
194+
(@parent.full_path + [@element])
195+
elsif lateral?
196+
@parent.full_path
197+
else
198+
[]
199+
end
194200
end
195201

196202
private

spec/grape/endpoint/declared_spec.rb

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -829,5 +829,31 @@
829829
expect(JSON.parse(last_response.body)).to match({})
830830
end
831831
end
832+
833+
context 'with a renamed field inside `given` block nested in hashes' do
834+
before do
835+
subject.format :json
836+
subject.params do
837+
requires :a, type: Hash do
838+
optional :c, type: String
839+
given :c do
840+
requires :b, type: Hash do
841+
requires :input_field, as: :output_field
842+
end
843+
end
844+
end
845+
end
846+
subject.post '/test' do
847+
declared(params)
848+
end
849+
end
850+
851+
it 'renames parameter input_field to output_field' do
852+
post '/test', { a: { b: { input_field: 'value' }, c: 'value2' } }
853+
854+
expect(JSON.parse(last_response.body)).to \
855+
match('a' => { 'b' => { 'output_field' => 'value' }, 'c' => 'value2' })
856+
end
857+
end
832858
end
833859
end

0 commit comments

Comments
 (0)