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

Parsing of arrayFormat: 'comma' not working for queries of single value with comma #296

Open
marxlow opened this issue Nov 17, 2020 · 7 comments
Labels

Comments

@marxlow
Copy link

marxlow commented Nov 17, 2020

Say we have an Object we are stringifying to become a query param of the following shape (notice the comma in the value)

  1. Create an object with its value as a single string / array of one string.
const singleQueryObject = { not_important: [ "I, am, one, single, value"]};

// OR (the value can also just be a plain string, not an array like the above)

const singleQueryObject = { not_important: "I, am, one, single, value" };
  1. We stringify it:
const stringifyResult = queryString.stringify(singleQueryObject, { arrayFormat: 'comma' });
//=>  'not_important=I%2C%20am%2C%20one%2C%20single%2C%20value'
  1. We parse it (Actual)
const parsedResult = queryString.parse(stringifyResult, { arrayFormat: 'comma' });
//=> { not_important: [ 'I', ' am', ' one', ' single', ' value' ] }

(Expected)

{ not_important: [ 'I, am, one, single, value' ] };

Work around (?)

This bug only occurs when there is a single value. So say if we run the same step as above with this object instead:

const singleQueryObject = { not_important: [ 'I, am, one, single, value', 'It works!'] };
const stringifyResult = queryString.stringify(singleQueryObject, { arrayFormat: 'comma' });
const parsedResult = queryString.parse(stringifyResult, { arrayFormat: 'comma' });

//=> { not_important: [ 'I, am, one, single, value', 'It works!' ] }
@Yashkochar20
Copy link

@sindresorhus @Richienb This is also causing issues on our side ? Any timeline for this fix ?

@sindresorhus
Copy link
Owner

// @mishugana

@Yashkochar20
Copy link

@sindresorhus @mishugana any update on this ?

@cainlevy
Copy link

The current parse API doesn't appear to have enough information to solve this problem.

One solution might be an option to encode array-ness into the parameter key during stringify, resulting in something like myArray[]=a,b,c. I could imagine this gracefully degrading for backwards compatible if the development philosophy requires it.

@nathan-logan
Copy link

The workaround from #211 solved this problem for me.

@EECOLOR
Copy link

EECOLOR commented Nov 1, 2022

This line is causing the problem: https://github.com/sindresorhus/query-string/blob/main/index.js#L177

In order for this to be fixed in a backwards compatible fashion an option would need to be introduced that allows prevention of the use of 'encoded arrays'.

@EECOLOR
Copy link

EECOLOR commented Nov 1, 2022

qs library has the correct behavior for comma separated arrays

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

No branches or pull requests

6 participants