-
-
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
Stringify with "arrayFormat: comma" encodes objects inside arrays as "[object Object]" #378
Comments
I agree the current behavior is unusable. However, it is really unclear to me what the format should be, because the "comma" format doesn't work the same way normal query strings work. In other words, i have no idea whatsoever how an object inside an array would be represented in that format. Any suggestions? cc @daggerjames @bryanlarsen who originally implemented/requested this format |
I guess |
@ls-urs-keller one possibility is that qs could throw if it encounters a non-primitive when stringifying arrayFormat comma, but maybe there's something useful to be done instead. |
My need is compact query strings when possible, but still, have it work correctly for nested object arrays. I'm using this workaround for the moment:
|
You're missing a few primitive types; i'd suggest doing |
Thank you. How about now? |
This is a good idea as people will likely get tripped up by this |
I would prefer a mixed mode as per my comments above. |
Howdy, folks that care about the My intuition is that what you want most is either to be able to round-trip things between parse and stringify (which requires If the latter, can folks please describe exactly what they're using and what format it requires? If it supports what |
Most likely every user of JSON:API https://jsonapi.org/format/#fetching-includes
|
@runspired so, a single array, represented as |
@ljharb correct. E.g. |
@runspired and if there's an array of length 1, does it need to be |
Guarantees it's an array regardless of how many are in it |
@ljharb btw you got me thinking and I am going to bring up to the spec authors that maybe |
It 1000% should be :-) |
This is a misunderstanding of the meaning of square brackets in query parameters as defined by JSON:API specification. For JSON:API specification query brackets do not indicate a list. Instead they build a query parameter family: A set of related but distinct query parameters. This is an example given in the specification:
Please see the section on query parameter families in the specification for details. |
@jelhan this isn't about what the meaning is currently, its about the current meaning being unparseable without intimate knowledge of the schema the url is going to expand to. |
@jelhan that sounds like a design flaw in the specification then, since there needs to be a clear way to determine if something is a single item or a list of one item, and brackets are otherwise the universal mechanism to do that for query strings. |
I would recommend disabling array parsing ( In addition, I would recommend not relying on One may argue that it's a design flaw not being able to derive from encoding schema if value should be parsed as a list. But one could make the same argument for not being able to derive from encoding schema if it should be parsed as s boolean, number, null, date, or even RSQL. I guess it's on everyone individually to judge. I don't think this issue is the correct place to discuss design decisions of JSON:API specification. I invite you to continue that discussion in JSON:API discussion forum or in an issue at JSON:API GitHub repository. |
I think the same argument holds! It's very much a flaw that these things aren't part of the encoding, but a list is far more fundamental than a type. I appreciate your context; I'm trying to figure out how and when the
|
im using qs for query params on FE part of app. |
@ihor-zinchenko can you elaborate? query strings have a massive limit; if you’re approaching it you should be using POST and not GET. |
@ljharb i use it for deeplinking on my FE app, i know about limits but i wont had so long string. The main problem that Object becames Object Object with comma |
can you share an example? deep linking just requires a path which usually isn’t particularly long. |
@ljharb sure
And i just need to convert this object to URL what QS do really good, but i need save the data length so i need some kind shortify the url. |
Thanks, that sounds like a really complex URL scheme. I wonder if it might make more sense to translate the author names into numeric IDs, and similar data normalization techniques? |
@ljharb its make sense in filters case but for sorting is not |
True. Either way it seems like a better approach for you is to use a custom encoder and/or filter function to make your query string shorter rather than trying to use comma for that purpose. Thanks for the use case! |
Tested on version 6.9.4
Stringify with
arrayFormat: comma
encodes objects inside arrays as[object Object]
I took this code from the examples:
and added an array around the inside object
The inside data is lost this way.
I tried the
allowDots
setting too, but that didn't fix anything.I'm not sure what the output should be like exactly, but this is unusable right now.
In my project I did
Array.map(p =>
${p.ID}::${p.Value})
, but that's very specific and I'm unsure if the::
is a good seperator.The text was updated successfully, but these errors were encountered: