question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

qs.parse doesn't handle encoded comma when parsing array with comma option

See original GitHub issue

Hello this is my first time opening an issue here! First of all thank you for this amazing library.

Basically my company is following jsonapi’s spec for formatting filter in URL

GET /comments?filter[post]=1,2 HTTP/1.1

This works fine if the comma is not encoded

it('should parse post into an array', () => {
    expect(
      qs.parse('filter[post]=1,2', {
        comma: true,
      })
    ).toStrictEqual({ filter: { post: ['1', '2'] } })
  })

Atm our http library automatically encodes comma, which I suspect makes parse treat the comma-separated list as just a string.

it('should parse post into an array', () => {
    expect(
      qs.parse('filter[post]=1%2C2', {
        comma: true,
      })
    ).toStrictEqual({ filter: { post: ['1', '2'] } })
})

This test fails and the returned array is a string, "1,2".

Please let me know if there’s anything that I miss that would help.

P/S: After reading the source code and the tests I think maybe this feature is not supported.

t.test('arrayFormat: comma allows only comma-separated arrays', function (st) {
        st.deepEqual(qs.parse('a[]=b&a[]=c', { arrayFormat: 'comma' }), { a: ['b', 'c'] });
        st.deepEqual(qs.parse('a[0]=b&a[1]=c', { arrayFormat: 'comma' }), { a: ['b', 'c'] });
        st.deepEqual(qs.parse('a=b,c', { arrayFormat: 'comma' }), { a: 'b,c' });
        st.deepEqual(qs.parse('a=b&a=c', { arrayFormat: 'comma' }), { a: ['b', 'c'] });
        st.end();
    });

If you think this is not a bug but rather a missing feature, do you think it should be included in the lib?

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:8 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
ljharbcommented, Apr 19, 2021

I’ve made a branch with 5 failing tests - 2 of which I don’t actually know how it would be possible to represent (nested arrays inside an object using the comma format).

A PR into that branch that improves the tests and/or provides a solution would be much appreciated.

1reaction
Ugzuzgcommented, Apr 19, 2021

I don’t think #237 is the solution to the problem above. I agree, that my comment is a duplicate of #387. I only commented in this issue, because it seemed relevant to me at the time.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Parse query string arrays using either multi-key or comma syntax
I have a .NET Core 6.0 Azure Function HTTP Trigger. I would it like to handle an array query ...
Read more >
qs - npm
A querystring parser that supports nesting and arrays, with a depth limit. ... (Note: the encoder option does not apply if encode is...
Read more >
qs - UNPKG
The CDN for qs. ... 36, - [Fix] parses comma delimited array while having percent-encoded ... 174, - [New] `qs.stringify`: add `encodeValuesOnly` option....
Read more >
parse_str - Manual - PHP
All variables created (or values returned into array if second parameter is set) are ... As an alternative, I use a "proper" querystring...
Read more >
node_modules/qs · 9bb473c15fffcd9e809a5cfebc14fa7993f6a88e ...
When using the plainObjects option the parsed value is returned as a null object, ... Some people use comma to join array, qs...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found