-
-
Notifications
You must be signed in to change notification settings - Fork 735
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
Option to preserve empty arrays and objects at "qs.stringify()" #362
Comments
This is how most backends interpret it - an empty array means there's nothing there, so there's nothing lost. Can you elaborate on the use cases? |
I'm trying to persist query string which contains complex JSON structure on page refresh. This is why inconsistency matters for my case. Currently I have to iterate through all JSON fields recursively and append missing fields, to properly initialize JSON on page refresh - which looks not clean and brings unnecessary complication to process. |
Is there a reason why the code interacting with your complex structure can't create the structure when needed, rather than requiring it be initialized already? |
This is exactly what I have to do, just noticed it is strange that I can't get back same object which was stringified. |
JSON itself doesn't provide that guarantee; generally |
qs ignores empty array/object and prevents us from sending `?array[]=`. This is a workaround to map an empty array to `[null]` so it gets treated as an empty string, and utilize qs `filter` function to map the value. A regression test was included. ljharb/qs#362
Did Anybody find workaround for this issue. my use case is, i am trying to render client using parsed data stored in url, i need to preserve all the state during qs.stringify(). |
This is necessary for cases like the mongo |
Seems like sending a query argument that might be empty (like an array of filter fields) is a pretty common use case. I'm fighting with this now. |
I could see a case where |
I am having the same issue. Was there a fix to this? |
@damonblack @nikkwong @vdenisenko-waverley - if you pass in JSON.stringify([]) this worked for me.
|
I'm working on a project where the query parameters are used to specify overrides, falling back to defaults for missing parameters. In this scenario, |
@jmbtutor would serializing to just If so, I'd be willing to accept a PR for an option to both parse and stringify that explicitly preserved empty arrays and objects in this manner. |
I ran into a case where I'm using Joi array conditionals to control the attributes returned by the model. When there's no attributes in the req we use defaults, when the attributes array is empty we don't return any attributes, only associations. Was surprised to find out we couldn't pass empty arrays with qs. Is there a better solution to passing empty arrays in the query?
|
Sometimes the backend parse the query string to know what was requested, for example, an update request needs to know which field needs updating; If a field of array needs to be cleared, the backend might be expecting |
@wisetwo in which of these scenarios can the backend not be changed to adapt to the frontend (which it should always do, since the frontend should never have to change for the sake of the backend)? |
@ljharb Sorry, I just noticed this ping. As long as that deserializes to an empty array, I think that should be fine. It communicates that the option is specified and that the value is empty. |
I've just spent a few hours debugging this exact issue, I send an object containing various properties to my back end, some of which are arrays, in the case that these are empty they are essentially being ignored. I do some processing on the object and then return the modified object back to my front end. With the empty arrays now missing my reactive Vue JS templates on the front end were erroring as I was checking the length of the arrays, which in some instances no longer exist. I think there are some pretty solid use cases for not ignoring empty arrays. The workaround is having to check if these properties exist and then append empty arrays, ideally I would just be returning all the original object properties. |
Turns out that in ASP.net if you define a query parameter from type dictionary and don't pass a value it will automatically be populated by the all query string options. This is a horrible behaviour which they claim is a bug and not a feature :| Nonetheless, it'd be very helpful if |
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
At the very least, how about parametrizing the behavior to use an empty string This is still not ideal. The ideal would be that encoding and decoding could happen back and forth without surprises. |
@jose-codaio #362 (comment) already says i'd accept a PR that added an option. Surprises are always inevitable, because everybody's expectations are different. |
Sorry, missed that.
By "surprises", I meant anything preventing the encoding process from being reversible. If an encoding process isn't reversible, then it just adds burden onto the requester. It'd be awesome if I could write the following with no problem. stringify(parse(stringify(parse(...)))) I understand if we can't always live up to that, but that is the ideal. Otherwise, I'll need custom logic to handle these "surprises". Out of curiosity: unless x includes non-JSON aspects (eg functions, non-enumerable properties), why would that not work? |
What is a "non-JSON aspect"? Try round-tripping a Date object, or a regex, or anything with a It's just not a reasonable expectation that a serialize/deserialize function is reversible for all input - only for a subset of input. It's a great goal, but it's not a reasonable expectation. |
Dates and regex's are not JSON. JSON is pretty much just:
That method is misleading, but it's probably just to easily dump an object into a JSON-compatible format.
Yeah, that's fair. 😔 |
This comment was marked as off-topic.
This comment was marked as off-topic.
This comment was marked as resolved.
This comment was marked as resolved.
I'm having the same problem. I solve my problem by doing a little trick on the frontend. But a parameter would be nice to have empty arrays behave differently. |
@HyopeR #362 (comment) is still waiting for a PR. |
Please make a PR, it is very strange functionality |
Fixed in #487. |
Documentation saying:
However there are cases when this behavior is not desirable. It brings inconsistencies between
stringify
andparse
methods, meaning same object can't be stringified and then parsed. Example:From above example
foo
field is lost.The text was updated successfully, but these errors were encountered: