-
Notifications
You must be signed in to change notification settings - Fork 13k
fix: HTTP query string params array parsing #38222
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
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
da2f455
fix: HTTP query params array parsing
sampaiodiego af7081e
test: add tests
sampaiodiego eaa9ff5
return status 400 if query string is not valid
sampaiodiego 76a6045
test: parse as array even tithout brackets
sampaiodiego 0f668c9
revert changes
sampaiodiego File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| import { parseQueryParams } from './parseQueryParams'; | ||
|
|
||
| describe('parseQueryParams', () => { | ||
| it('should parse simple query string', () => { | ||
| const result = parseQueryParams('foo=bar'); | ||
| expect(result).toEqual({ foo: 'bar' }); | ||
| }); | ||
|
|
||
| it('should parse multiple query parameters', () => { | ||
| const result = parseQueryParams('foo=bar&baz=qux'); | ||
| expect(result).toEqual({ foo: 'bar', baz: 'qux' }); | ||
| }); | ||
|
|
||
| it('should parse array parameters', () => { | ||
| const result = parseQueryParams('ids[]=1&ids[]=2&ids[]=3'); | ||
| expect(result).toEqual({ ids: ['1', '2', '3'] }); | ||
| }); | ||
|
|
||
| it('should parse nested objects', () => { | ||
| const result = parseQueryParams('user[name]=john&user[age]=30'); | ||
| expect(result).toEqual({ user: { name: 'john', age: '30' } }); | ||
| }); | ||
|
|
||
| it('should handle empty query string', () => { | ||
| const result = parseQueryParams(''); | ||
| expect(result).toEqual({}); | ||
| }); | ||
|
|
||
| it('should decode URL encoded values', () => { | ||
| const result = parseQueryParams('name=John%20Doe'); | ||
| expect(result).toEqual({ name: 'John Doe' }); | ||
| }); | ||
|
|
||
| it('should handle boolean-like values as strings', () => { | ||
| const result = parseQueryParams('active=true&disabled=false'); | ||
| expect(result).toEqual({ active: 'true', disabled: 'false' }); | ||
| }); | ||
|
|
||
| it('should throw error when array limit is exceeded', () => { | ||
| const largeArray = Array(501) | ||
| .fill(0) | ||
| .map((_, i) => `ids[]=${i}`) | ||
| .join('&'); | ||
| expect(() => parseQueryParams(largeArray)).toThrow(); | ||
| }); | ||
|
|
||
| it('should parse arrays within the limit', () => { | ||
| const array = Array(500) | ||
| .fill(0) | ||
| .map((_, i) => `ids[]=${i}`) | ||
| .join('&'); | ||
| const result = parseQueryParams(array); | ||
| expect(result.ids).toHaveLength(500); | ||
| }); | ||
|
|
||
| it('should parse as array even without brackets', () => { | ||
| const result = parseQueryParams('ids=1&ids=2'); | ||
| expect(result.ids).toHaveLength(2); | ||
| }); | ||
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| import qs from 'qs'; // Using qs specifically to keep express compatibility | ||
|
|
||
| export function parseQueryParams(url: string) { | ||
| return qs.parse(url, { arrayLimit: 500, throwOnLimitExceeded: true }); | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.