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

qs.parse omitting empty string keys #432

Open
Coobaha opened this issue Mar 3, 2022 · 6 comments · May be fixed by #433
Open

qs.parse omitting empty string keys #432

Coobaha opened this issue Mar 3, 2022 · 6 comments · May be fixed by #433

Comments

@Coobaha
Copy link
Contributor

Coobaha commented Mar 3, 2022

Hi, it will be nice to have this part configurable:

qs/lib/parse.js

Lines 150 to 152 in 542a5c7

var parseKeys = function parseQueryStringKeys(givenKey, val, options, valuesParsed) {
if (!givenKey) {
return;

current output:

qs.parse("=1&=2") === {}
qs.parse(" =1& =2") === { " ": ["1","2"] }

expected output :

qs.parse("=1&=2", { allowEmptyKeys: true }) === { "": ["1","2"] }

If you are ok - I can open a PR

@ljharb
Copy link
Owner

ljharb commented Mar 3, 2022

Please link me if I'm wrong, but I don't believe the spec permits empty keys in a query string. What's your use case/software where that's expected?

@Coobaha
Copy link
Contributor Author

Coobaha commented Mar 3, 2022

Thanks for the fast reply, I was using this as a reference:

new URLSearchParams('=1').getAll('') // ['1']

Our use case is that we need to evaluate user input in HTTP client editor, in some cases key might be not present yet or really evaluates to empty string. As a workaround I am assigning empty key names to temporal key and after parsing replacing them back :D This also requires preserving initial keys order, so hack becomes quite complex..

I can also imagine that some server/api might use this empty param name, even if it is not part of any RFC

@ljharb
Copy link
Owner

ljharb commented Mar 3, 2022

"USP supports it" is a good argument, although your use cases are not convincing - if the key's not present yet, then the value isn't either, and I'm not clear on why the key would really evaluate to an empty string.

The PR adding this would have to add it to both parse and stringify, so that there could be a round trip.

Additionally, what happens with =1&=2, in the arrayFormat repeat vs brackets? What about []=1&[]=2?

@Coobaha
Copy link
Contributor Author

Coobaha commented Mar 4, 2022

Great! i will open a pr then soon and we can continue there 🙏

@ljharb
Copy link
Owner

ljharb commented Mar 4, 2022

To be clear: a PR will need:

  • to add an option to both parse and stringify
  • to document in the readme
  • to have tests, for both parse and stringify, for at least:
    • repeat keys with all array formats
    • bracketed keys with all array formats
    • a lone = (and =& and &= and &=&)

@Coobaha Coobaha linked a pull request Mar 5, 2022 that will close this issue
9 tasks
@Coobaha
Copy link
Contributor Author

Coobaha commented Mar 5, 2022

@ljharb opened #433 👍

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

Successfully merging a pull request may close this issue.

2 participants