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

querystring.stringify unexpected behavior on undefined values #28916

Closed
moshest opened this issue Aug 1, 2019 · 8 comments
Closed

querystring.stringify unexpected behavior on undefined values #28916

moshest opened this issue Aug 1, 2019 · 8 comments
Labels
doc Issues and PRs related to the documentations. querystring Issues and PRs related to the built-in querystring module.

Comments

@moshest
Copy link

moshest commented Aug 1, 2019

  • Version: 10
  • Platform: MacOS

JSON.stringify omitting undefined values:

JSON.stringify({s: 's', i: 0, n: null, u: undefined});  
// output: {"s":"s","i":0,"n":null}

But querystring.stringify not omitting undefined values:

querystring.stringify({s: 's', i: 0, n: null, u: undefined});  
// output: s=s&i=0&n=&u=

Moreover, I think that treating nulls and undefineds the same way is less usable for the user (ie. user musts to delete properties from the object in order to filter them).

@moshest moshest changed the title querystring.stringify unexpected behavior on undefined querystring.stringify unexpected behavior on undefined values Aug 1, 2019
@addaleax addaleax added the querystring Issues and PRs related to the built-in querystring module. label Aug 1, 2019
@cjihrig
Copy link
Contributor

cjihrig commented Aug 1, 2019

FWIW, this (what I consider to be) confusing behavior has been brought up for years - nodejs/node-v0.x-archive#7971, nodejs/node-v0.x-archive#8825, etc. I think at this point, we are stuck with the behavior.

I had proposed nodejs/node-v0.x-archive#8829 a long time ago to try to work around it in a non-breaking way. It might be worth revisiting, although I'd have to look into whether it was still relevant 5 years later 😄

@devsnek
Copy link
Member

devsnek commented Aug 1, 2019

I think URLSearchParams are clearly the superior choice here 😄

@moshest
Copy link
Author

moshest commented Aug 1, 2019

@devsnek putting null or undefined values as strings is even worse!

@bnoordhuis bnoordhuis added the doc Issues and PRs related to the documentations. label Aug 6, 2019
@bnoordhuis
Copy link
Member

I think at this point, we are stuck with the behavior.

I would say so. I believe the current behavior goes all the way back to Node.js v0.1.x. Any change would almost certainly result in ecosystem breakage.

It might be good to document it though. I'll add labels.

@moshest You want to send a pull request?

@moshest
Copy link
Author

moshest commented Aug 6, 2019

Where are the docs?

@bnoordhuis
Copy link
Member

@moshest doc/api/querystring.md

moshest added a commit to moshest/node that referenced this issue Aug 8, 2019
@moshest
Copy link
Author

moshest commented Aug 8, 2019

Done.

@moshest
Copy link
Author

moshest commented Jun 12, 2022

If anyone needs way to filter empty values (including empty strings):

// replace queries like "a=1&b=&c=2&d=" to "a=1&c=2"
stringify(query).replace(/&[^=&]+=(?=&|$)/g, '')

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
doc Issues and PRs related to the documentations. querystring Issues and PRs related to the built-in querystring module.
Projects
None yet
Development

No branches or pull requests

5 participants