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

URI.setData() and multiple parameters with the same name #1308

Open
Ivan-Perez opened this issue May 8, 2015 · 3 comments
Open

URI.setData() and multiple parameters with the same name #1308

Ivan-Perez opened this issue May 8, 2015 · 3 comments
Milestone

Comments

@Ivan-Perez
Copy link

Ivan-Perez commented May 8, 2015

Hi,

There's a problem on the URI class, which is producing bad URIs when there are multiple parameters with the same name.

Code to reproduce the bug:

var uri = new URI('http://test/?param=1&param=2');
uri.toString();// "http://test/?param=1&param=2" (OK)
uri.setData('another', 'parameter');
uri.toString();// "http://test/?param[0]=1&param[1]=2&another=parameter" (ERROR)
//                                   ^^^        ^^^

The last uri.toString() is producing a wrong URI, the correct one should be http://test/?param=1&param=2&another=parameter (notice those array indexes, which don't make sense).

Thanks in advance.

--- Want to back this issue? **[Post a bounty on it!](https://www.bountysource.com/issues/13654836-uri-setdata-and-multiple-parameters-with-the-same-name?utm_campaign=plugin&utm_content=tracker%2F22069&utm_medium=issues&utm_source=github)** We accept bounties via [Bountysource](https://www.bountysource.com/?utm_campaign=plugin&utm_content=tracker%2F22069&utm_medium=issues&utm_source=github).
@SergioCrisostomo
Copy link
Member

Related: #1028

@SergioCrisostomo SergioCrisostomo added this to the 1.5.2 milestone May 8, 2015
@boppy
Copy link
Contributor

boppy commented Sep 26, 2015

Keep in mind that this change will break compatibility for some users, because PHP can only handle arrays from query-strings with indexes:

qs.php?foo=bar&foo=baz

Results in $_GET['foo'] to be string('baz') (whereas ?foo[]=bar&foo[]=baz [even without index] results in an array).

@Ivan-Perez
Copy link
Author

The current behaviur doesn't match PHP's behaviour either. For example:

var string = 'qs.php?foo=bar&foo=baz';
var uri = new Uri(string);
uri.toString();// returns the same `string` (not matching PHP behaviour)

The problem appears after adding another paramater, then toString() adds the indexed.

var string = 'qs.php?foo=bar&foo=baz';
var uri = new Uri(string);
uri.setData('param', 'test');
uri.setData('param', null);
uri.toString();// no longer returns `string`; now indexes were added, even if the parameters are the same

Nonetheless, where I really found this problem was in this piece of code:

var string = 'index.php?p[]=1&p[]=2&p[]=3&foo=bar';
var uri = new Uri(string);
uri.toString();// ok
uri.setData('foo', 'baz');
uri.toString();// doesn't look good:
// "index.php?p[][0]=1&p[][1]=2&p[][2]=3&foo=baz"
//               ^^^ indexes shouldn't be added in this case

Thanks

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

No branches or pull requests

3 participants