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

Added a fix for the value mapping of an array of objects #413

Merged
merged 1 commit into from
Oct 7, 2018
Merged
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
10 changes: 10 additions & 0 deletions lib/rspec_api_documentation/dsl/endpoint/params.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,16 @@ def extended
unless p[:value]
cur = extra_params
[*p[:scope]].each { |scope| cur = cur && (cur[scope.to_sym] || cur[scope.to_s]) }

# When the current parameter is an array of objects, we use the
# first one for the value and add a scope indicator. The
# resulting parameter name looks like +props[pictures][][id]+
# this.
if cur.is_a?(Array) && cur.first.is_a?(Hash)
cur = cur.first
param[:scope] << ''
Copy link

@ngoral ngoral Feb 16, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The problem anyone's having is here.
This line changes params, so next time we use example { do_request }, it is called with altered scope. Causing line 32 to fail.
If we move these lines into a loop, we'll go deeper and deeper, ending up with as many []-s as we had do_request-s. E.g. props[pictures][][][id] if we called do_request inside a group for request twice

I would suggest changing the code in such way that if we want to do nested arrays in parameter, we should use '' there. Like so:

parameter :pictures, type: :array
parameter :id, scope: [:pictures, '']

I can do it, just need some kind of approval on it =)

Auto-adding [] for array scopes of course would be nice, but I can't see a good way of doing it right now (if you do, you are very welcome). I think it should be done somewhere in writers, though, not changing user-defined params.

Or we can just use plain old scope: 'pictures[]'

I would revert this PR, 'cause it seems to break work with any array parameters (see @jsmartt code in #462 )

end

p[:value] = cur && (cur[p[:name].to_s] || cur[p[:name].to_sym])
end
p
Expand Down