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

fix: Add brackets to querystring to clarify it's array (#1672) #1673

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

3jins
Copy link

@3jins 3jins commented Jun 11, 2022

If length of array is 1, it was being recognized as a string, not an array.

  • AS-IS:
    { colors: ['black', 'mustard'] } -> colors=black&colors=mustard
    { colors: ['black'] } -> colors=black
  • TO-BE:
    { colors: ['black', 'mustard'] } -> colors[]=black&colors[]=mustard
    { colors: ['black'] } -> colors[]=black

Added qs dependency for this fix.

Checklist

  • I have ensured my pull request is not behind the main or master branch of the original repository.
  • I have rebased all commits where necessary so that reviewing this pull request can be done without having to merge it first.
  • I have written a commit message that passes commitlint linting.
  • I have ensured that my code changes pass linting tests.
  • I have ensured that my code changes pass unit tests.
  • I have described my pull request and the reasons for code changes along with context if necessary.

If length of array is 1, it was being recognized as a string, not an array.

- AS-IS:
  { colors: ['black', 'mustard'] } -> colors=black&colors=mustard
  { colors: ['black'] } -> colors=black
- TO-BE:
  { colors: ['black', 'mustard'] } -> colors[]=black&colors[]=mustard
  { colors: ['black'] } -> colors[]=black

Added qs dependency for this fix.
@codecov
Copy link

codecov bot commented Jun 11, 2022

Codecov Report

Merging #1673 (4a53007) into master (00ce71f) will not change coverage.
The diff coverage is 100.00%.

@@           Coverage Diff           @@
##           master    #1673   +/-   ##
=======================================
  Coverage   99.60%   99.60%           
=======================================
  Files           5        5           
  Lines         507      507           
  Branches      142      142           
=======================================
  Hits          505      505           
  Misses          2        2           
Impacted Files Coverage Δ
lib/request.js 100.00% <100.00%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 00ce71f...4a53007. Read the comment docs.

Copy link
Member

@miwnwski miwnwski left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm for this change, however I fear this can break a lot of applications because users might rely on current behaviour today. I wouldn't be comfortable releasing this under Koa 2.x

Copy link
Member

@miwnwski miwnwski left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Seems like a decent change but I'm going to let other maintainers decide on this one.

@3jins
Copy link
Author

3jins commented Sep 12, 2022

Is anyone willing to merge this PR?
I don't have write access so that I cannot merge this.

Copy link
Member

@3imed-jaberi 3imed-jaberi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@miwnwski From my side, in theory yes we can merge this PR now since we are on v3-alpha.

But we need to make it flexible because we have many strategies here and we don't push one over the other, let's prevent breaking changes

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'

cc: @fengmk2 🤔?!

@andrew0
Copy link
Contributor

andrew0 commented Jan 15, 2024

Please can we not introduce qs to Koa... it's one of the things I wanted to escape from Express. The behavior is very unusual compared to my experience with other web frameworks and makes working with the query object a pain. Any query value could be either a string, an array of strings, or an object, and these arrays/objects can also be nested. So all query values accesses need to be overly defensive, or just call .toString() and accept that sometimes you might be getting [object Object] as a value.

The "bug" that this is fixing doesn't really seem like a problem to me. I think it's reasonable to assume that ctx.request.query will never have any values that are single-value arrays, and that you should always expect that it could be either a string or an array of strings. As far as I can tell, the only situation that this is "fixing" is if you are modifying the request's query object in middleware, setting its value to something that Node's built-in querystring parser would never produce, and you want the rest of your middleware to be able to access ctx.request.query while assuming certain values are an array without checking. Adding non-standard syntax to the query string to support this edge case feels like a hack.

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

Successfully merging this pull request may close these issues.

None yet

4 participants