ljharb / qs

A querystring parser with nesting support
BSD 3-Clause "New" or "Revised" License
8.5k stars 729 forks source link

ArrayFormat Type Comma is producing the output of Array Format Repeat when the array length is 1 #442

Closed manoj-rp closed 2 years ago

manoj-rp commented 2 years ago

qs.stringify({ a: ['b'] }, { arrayFormat: 'comma' }) should return the a=b.

But it is returning the repeat type format - a%5B%5D=b ~ a[]=b

Below is the documentation and expected behaviour from readme

qs.stringify({ a: ['b', 'c'] }, { arrayFormat: 'indices' })
// 'a[0]=b&a[1]=c'
qs.stringify({ a: ['b', 'c'] }, { arrayFormat: 'brackets' })
// 'a[]=b&a[]=c'
qs.stringify({ a: ['b', 'c'] }, { arrayFormat: 'repeat' })
// 'a=b&a=c'
qs.stringify({ a: ['b', 'c'] }, { arrayFormat: 'comma' })
// 'a=b,c'

Below is the screenshot for the same

image

Can anyone help me if this is an issue or Am I missing something?

goodrick commented 2 years ago

I encountered the same problem. I locked version 6.9.6

manoj-rp commented 2 years ago

Thanks for your reply. Unfortunately this module is being used by some other module. And I'm using that module for my usecase.

ljharb commented 2 years ago

This isn’t a bug, it is intentional behavior so that stringifying will preserve the array-ness.

ljharb commented 2 years ago

See #434

manoj-rp commented 2 years ago

I have read the thread. But Shouldn't it be configurable?

Can we have another option along with arrayType.

preserveArrayness: true/false

Because there can be usecases like

One can use qs for stringifying it and may not be using it for parsing back again?

In our case,

We stringify it and pass the query string to an external service and that service doesn't use the qs module to parse it back?

Any thoughts @ljharb

mima0815 commented 2 years ago

we run into the same issue with the new version.

not all APIs we are using are capable to handling query strings with brackets, they are expecting comma separated lists in all cases or no comma if it is only one value.

+1 for the additional option to toggle the "Arrayness" feature.

glomotion commented 2 years ago

we have also just slammed into this issue. Tis causing a great deal of errors in production for us right now (we use QS to stringify params which are sent to an API - which does NOT support array bracket query string notation). 😭

for now, we'll just lock to 6.9.6 like the suggestion above.

Shooz136 commented 2 years ago

Same here - I'd appreciate a configurable option.

ljharb commented 2 years ago

I will plan on reverting the behavior by default, and adding an option to satisfy the original request.

manoj-rp commented 2 years ago

I have overcome this issue temporarily by converting my arrays to length 2 when the actual length is 1

Example original array - [‘abc’] Transformed array- [‘abc’,’’]. Just added an empty element at the end. This solved my problem for now.

But it would be great if we can revert the recent change since many of the production applications are breaking.

On Wed, 15 Jun 2022 at 9:56 PM, Benjamin Shoemaker @.***> wrote:

Same here - I'd appreciate a configurable option.

— Reply to this email directly, view it on GitHub https://github.com/ljharb/qs/issues/442#issuecomment-1156682423, or unsubscribe https://github.com/notifications/unsubscribe-auth/AREQDQUMEP6P4YKEY22VUHTVPH747ANCNFSM5YIVZJXA . You are receiving this because you authored the thread.Message ID: @.***>

-- Manoj Kumar Richpanel.com

chengwb53 commented 2 years ago

debug find this: image array.length === 1 use brackets

lock version

lpillonel commented 2 years ago

Same issue here. If this change is intentional, I suggest to consider it as a breaking change

ljharb commented 2 years ago

Released in v6.11.0.