-
-
Notifications
You must be signed in to change notification settings - Fork 732
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
Cannot use square brackets in key name #235
Comments
This behavior is a standard convention across the entire web. If you want square brackets in a key name, you'd need to escape them yourself - In other words, it's not a simple use case, it's an exceedingly uncommon one for decades on the web. |
Then is it an issue if I get the same behaviour no matter if the square brackets are escaped or not? I originally ran into the issue doing escaping first. ( Additionally, the node querystring library does not behave this way, Is that an issue with node? Here's a test with
As you can see, only QS handles square brackets that way. It doesn't include the square brackets as part of the key if they are escaped as you are saying. I also tested in C# with I tested in PHP:
This output Test in python using urllib:
with the following results:
As you can see, other than QS, I have only found one example of the behaviour that QS shows. Others do not do this. Additionally in every test I have done, encoding the square brackets has no effect on the result. |
Thanks, this information changes things slightly. Without a doubt, this should be true (take qs.stringify({
"foo[bar]": "baz"
}) === 'foo%5Bbar%5D=baz'; // true
qs.stringify({
foo: {
bar: "baz",
},
}) === 'foo[bar]=baz'; // true
qs.parse('foo[bar]=baz') =~= {
foo: {
bar: 'baz'
},
}; // true
qs.parse('foo%5Bbar%5D=baz') =~= {
'foo[bar]': 'baz',
}; // true If any of these four fail, then there is decidedly a bug in A PR to add these test cases, and fix, is welcome - I'll take a look at it in a few days otherwise. |
Great! I'll push a PR when i get a chance. |
@ljharb It uncovers the additional issue that In one way it is expected since square brackets are URL encodable, but it's something to be aware of going forward. Edit: Previously mentioned object stringification: Array value stringification: A huge difference shows when parsing the previous values: This all seems to rely on the "encode" option not being passed in and defaulting to I don't know how many people are encoding their square bracketed queries but I'm guessing a lot? This is likely to mess a lot of people up if they don't pay attention to changelogs :) Part of me feels that putting something behind an option would be the right thing to do here to stop it breaking people, but the other part feels like the option should be to enable the legacy functionality of ignoring if the square brackets are escaped or not. The downside of this is that a big user of qs is express, and express doesn't make it easy to change the config passed to qs, so they're gonna pick what they think is best for everyone which most likely is the current limitation which is disappointing in a way. Thoughts? |
Hmm, this is definitely complex. Let me think more about it. It might be better to change the behavior only when an option is passed, so as to avoid breaking changes - even if that means express users can't access that behavior. |
@ljharb Ran into a similar issue recently - I need to be able to include keys which I have no control over, which unfortunately may include square brackets/dots. I was wondering if you are aware of a good way to encode/decode them in this case, so as not to be interpreted by qs as nested properties but rather a flat key-value pair. |
I want to make a request that looks like this:
?foo[bar]=baz
Without looking into all the fancy features that QS provides, i expect the following object:
I see this:
I see that square brackets are used as some sort of object accessor but there seems like no documented way of escaping them or disabling this feature that I can see for this simplest of use cases.
The text was updated successfully, but these errors were encountered: