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

Conversation

Jack12816
Copy link
Contributor

- What is it good for

I stumbled over the version 5 to 6 upgrade issue (#398) and this is an approach to solve the issue.

- What I did

I debugged the cur property to be an array and the p[:name] access resulted in an TypeError: no implicit conversion of String into Integer. Now we detect this situation and pick the first object of the array. Furthermore we add an additional empty string to the scopes array as an indicator. This results in parameter names like this: props[pictures][][id].

- A picture of a cute animal (not mandatory but encouraged)

images 1

Signed-off-by: Hermann Mayer <hermann.mayer92@gmail.com>
@oestrich oestrich merged commit 6a4a173 into zipmark:master Oct 7, 2018
@oestrich
Copy link
Contributor

oestrich commented Oct 7, 2018

Thanks!

@anklos
Copy link

anklos commented Apr 16, 2019

Hi there,

I used the master branch in my Gemfile, however I am still experiencing the issue when submit the params like this:

params = {
        booking: {
          sessions: [
              {
                court_id: court_1.id,
                time: [20,21],
                date: '2017-03-02'
              },
              {
                court_id: court_2.id,
                time: [20,21,22],
                date: '2017-03-02'
              }
            ]
          }
        }

      do_request params

i debugged around the same place of the pull request. As param[:scope] << '' is introduced, it throws an error in the line [*p[:scope]].each { |scope| cur = cur && (cur[scope.to_sym] || cur[scope.to_s]) } when the scope is an empty string.

In my example, cur is [{:court_id=>22265, :time=>[20, 21], :date=>"2017-03-02"}, {:court_id=>22266, :time=>[20, 21, 22], :date=>"2017-03-02"}] and cur[scope.to_sym] would throw *** TypeError Exception: no implicit conversion of Symbol into Integer when the scope is ''

Any suggestion? Thank you.

@trevorrjohn
Copy link

Yeah I don't think this fixed it, here is a spec master...trevorrjohn:show-broken-spec

 % bundle exec rspec spec/dsl_spec.rb:426
Run options: include {:locations=>{"./spec/dsl_spec.rb"=>[426]}}
F

Failures:

  1) Order proper query_string serialization GET /orders scope with array of hashes param parsed properly
     Failure/Error: param[:scope] << ''

     NoMethodError:
       undefined method `<<' for :search:Symbol
       Did you mean?  <
     # ./spec/dsl_spec.rb:426:in `block (5 levels) in <top (required)>'

Finished in 0.00842 seconds (files took 0.59245 seconds to load)
1 example, 1 failure

Failed examples:

rspec ./spec/dsl_spec.rb:420 # Order proper query_string serialization GET /orders scope with array of hashes param parsed properly

@diegotoral
Copy link

I am dealing with the same problem with version 6.1.0.

Just updated a spec to include nested attributes to my params hash and it throws TypeError: no implicit conversion of String into Integer.

@Uysim
Copy link

Uysim commented Oct 7, 2020

@oestrich @Jack12816 Do you know what we still face this problem after merged?
Seem like you missed this info

@jakehow
Copy link
Member

jakehow commented Oct 7, 2020

@Uysim please open a new issue and/or PR with details if you have a fix.

# 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 )

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

8 participants