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

Addressable wrong query_values when parsing query with array params #282

Closed
PetrKaleta opened this issue Nov 22, 2017 · 5 comments
Closed
Labels

Comments

@PetrKaleta
Copy link

Please have a look on the following example

uri = Addressable::URI.parse 'http://foo.com/bar?par[]=1&par[]=2&par[]=3'
uri.query_values # => {"par[]"=>"3"}

expected query value is {"par": ["1", "2", "3"]}

Also this is very strange:

uri = Addressable::URI.parse 'http://foo.com/bar'
uri.query_values = {par: [1, 2, 3]}
uri.to_s # => "http://foo.com/bar?par=1&par=2&par=3"

uri = Addressable::URI.parse uri.to_s
uri.query_values # => {"par"=>"3"}

Which is really inconsistent...

@dentarg
Copy link
Collaborator

dentarg commented Nov 22, 2017

I think the Stack Overflow thread Is array syntax using square brackets in URL query strings valid? might help explain the lack of support in Addressable. The accepted answer starts out with

The answer is not simple.

Had to check myself, and percent encoding doesn't work either:

$ irb -raddressable/uri
irb(main):001:0> Addressable::VERSION::STRING
=> "2.5.2"
irb(main):002:0> uri = Addressable::URI.parse("http://foo.com/?abc%5B%5D=123&abc%5B%5D=456")
=> #<Addressable::URI:0x3fe170044e30 URI:http://foo.com/?abc%5B%5D=123&abc%5B%5D=456>
irb(main):003:0> uri.query_values
=> {"abc[]"=>"456"}
irb(main):004:0> uri.to_s
=> "http://foo.com/?abc%5B%5D=123&abc%5B%5D=456"
irb(main):005:0> Addressable::URI.parse(uri.to_s)
=> #<Addressable::URI:0x3fe170031790 URI:http://foo.com/?abc%5B%5D=123&abc%5B%5D=456>
irb(main):006:0> Addressable::URI.parse(uri.to_s).query_values
=> {"abc[]"=>"456"}

@PetrKaleta
Copy link
Author

Yes, percent doesn't work either and btw it will be enough for me, to make it work...

@PetrKaleta
Copy link
Author

PetrKaleta commented Nov 22, 2017

I think that also when setting a given query param to array value, it should turn to %5B%5D encoded form. So it should work like this:

uri = Addressable::URI.parse 'http://foo.com/'
uri.query_values = { abc: [123, 456] }
uri.to_s # => http://foo.com/?abc%5B%5D=123&abc%5B%5D=456

Instead of http://foo.com/?abc=123&abc=456 which is simply wrong...

@sporkmonger
Copy link
Owner

uri.query_values(Array)
# => [["abc", "123"], ["abc", "456"]]

@sporkmonger
Copy link
Owner

You can also find plenty of examples of query parameters that are repeated, both in the wild, and in some cases, in specifications. The model of using [] suffixes has no support in a standards-based context and will not be supported by Addressable.

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

No branches or pull requests

3 participants