-
Notifications
You must be signed in to change notification settings - Fork 7
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
qs.parse returns invalid key with brackets when using nested array query #1751
Merged
Merged
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
a3ea344
add test to fix nested query
tintinthong 148551a
add fix to account for limitations of qs.parse
tintinthong ee8d70d
Add assertion for key wrapped in square brackets
tintinthong 0b25bfb
update qs library that includes strictDepth=true
tintinthong 3494d09
Revert "add test to fix nested query"
tintinthong 0e3ddc4
remove previous fix that manually changes the key. Just use qs config…
tintinthong 83b246e
add qs-test module
tintinthong 4ae93ee
fix lint
tintinthong 5547287
Merge branch 'main' into eco-38/fix-qs-parse
tintinthong 09b2a69
update ts-expect-error for missing strictDepth type
tintinthong cc52a7b
fix spacing
tintinthong 16cc6d9
Upgrade @types/qs to include strictDepth
tintinthong eaedefa
Merge branch 'main' into eco-38/fix-qs-parse
tintinthong 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 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 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,54 @@ | ||
import qs from 'qs'; | ||
import { module, test } from 'qunit'; | ||
|
||
import { parseQuery, Query } from '@cardstack/runtime-common/query'; | ||
|
||
module('Unit | qs | parse', function () { | ||
test('parseQuery errors out if the query is too deep', async function (assert) { | ||
assert.throws( | ||
() => parseQuery('a[b][c][d][e][f][g][h][i][j][k][l]=m'), | ||
/RangeError: Input depth exceeded depth option of 10 and strictDepth is true/, | ||
); | ||
}); | ||
test('invertibility: applying stringify and parse on object will return the same object', async function (assert) { | ||
let testRealmURL = 'https://example.com/'; | ||
let query: Query = { | ||
filter: { | ||
on: { | ||
module: `${testRealmURL}book`, | ||
name: 'Book', | ||
}, | ||
every: [ | ||
{ | ||
eq: { | ||
'author.firstName': 'Cardy', | ||
}, | ||
}, | ||
{ | ||
any: [ | ||
{ | ||
eq: { | ||
'author.lastName': 'Jones', | ||
}, | ||
}, | ||
{ | ||
eq: { | ||
'author.lastName': 'Stackington Jr. III', | ||
}, | ||
}, | ||
], | ||
}, | ||
], | ||
}, | ||
sort: [ | ||
{ | ||
by: 'author.lastName', | ||
on: { module: `${testRealmURL}book`, name: 'Book' }, | ||
}, | ||
], | ||
}; | ||
let queryString = qs.stringify(query); | ||
let parsedQuery: any = parseQuery(queryString); | ||
assert.deepEqual(parsedQuery, query); | ||
}); | ||
}); |
This file contains 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 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 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 |
---|---|---|
|
@@ -307,9 +307,9 @@ function assertEveryFilter( | |
`${pointer.join('/') || '/'}: every must be an array of Filters`, | ||
); | ||
} else { | ||
filter.every.every((value: any, index: number) => | ||
assertFilter(value, pointer.concat(`[${index}]`)), | ||
); | ||
filter.every.forEach((value: any, index: number) => { | ||
assertFilter(value, pointer.concat(`[${index}]`)); | ||
}); | ||
} | ||
} | ||
|
||
|
@@ -348,9 +348,10 @@ function assertEqFilter( | |
if (typeof filter.eq !== 'object' || filter.eq == null) { | ||
throw new Error(`${pointer.join('/') || '/'}: eq must be an object`); | ||
} | ||
Object.entries(filter.eq).every(([key, value]) => | ||
assertJSONValue(value, pointer.concat(key)), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
); | ||
Object.entries(filter.eq).forEach(([key, value]) => { | ||
assertKey(key, pointer); | ||
assertJSONValue(value, pointer.concat(key)); | ||
}); | ||
} | ||
|
||
function assertContainsFilter( | ||
|
@@ -371,9 +372,10 @@ function assertContainsFilter( | |
if (typeof filter.contains !== 'object' || filter.contains == null) { | ||
throw new Error(`${pointer.join('/') || '/'}: contains must be an object`); | ||
} | ||
Object.entries(filter.contains).every(([key, value]) => | ||
assertJSONValue(value, pointer.concat(key)), | ||
); | ||
Object.entries(filter.contains).forEach(([key, value]) => { | ||
assertKey(key, pointer); | ||
assertJSONValue(value, pointer.concat(key)); | ||
}); | ||
} | ||
|
||
function assertRangeFilter( | ||
|
@@ -419,3 +421,15 @@ function assertRangeFilter( | |
}); | ||
}); | ||
} | ||
|
||
export function assertKey(key: string, pointer: string[]) { | ||
if (key.startsWith('[') && key.endsWith(']')) { | ||
throw new Error( | ||
`${pointer.join('/')}: field names cannot be wrapped in brackets: ${key}`, | ||
); | ||
} | ||
} | ||
|
||
export const parseQuery = (queryString: string) => { | ||
return qs.parse(queryString, { depth: 10, strictDepth: true }); | ||
}; |
This file contains 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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
.every
is problematic because it expects to return a boolean. So, not all assertions are being run thru