Skip to content

Commit 2a26b5a

Browse files
fix: parse JSON string where query param (#15745)
### What? Support `where` sent as a JSON string in REST API requests (e.g. `?where={"read":{"equals":false}}`). Previously the filter was silently ignored; now it's parsed correctly. ### Why? GET query params are always strings. `parseParams` parsed `data` this way already but skipped `where`, so string-encoded filters had no effect. ### How? After the `data` block in `parseParams`: if `where` is a non-empty string, `JSON.parse` it. If it's already an object (e.g. from bracket notation), leave it alone. --------- Co-authored-by: Patrik Kozak <35232443+PatrikKozak@users.noreply.github.com>
1 parent 2b10973 commit 2a26b5a

File tree

2 files changed

+22
-1
lines changed

2 files changed

+22
-1
lines changed

packages/payload/src/utilities/parseParams/index.spec.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,23 @@ describe('parseParams', () => {
220220
const result = parseParams({ where })
221221
expect(result.where).toBe(where)
222222
})
223+
224+
it('should parse where when it is a JSON string', () => {
225+
const where = { read: { equals: false } }
226+
const result = parseParams({ where: JSON.stringify(where) })
227+
expect(result.where).toEqual(where)
228+
})
229+
230+
it('should not process empty string where', () => {
231+
const result = parseParams({ where: '' })
232+
expect(result.where).toBe('')
233+
})
234+
235+
it('should throw error for invalid JSON where', () => {
236+
expect(() => {
237+
parseParams({ where: 'invalid-json' })
238+
}).toThrow()
239+
})
223240
})
224241

225242
describe('edge cases', () => {

packages/payload/src/utilities/parseParams/index.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ type RawParams = {
2929
sort?: string | string[]
3030
trash?: string
3131
unpublishAllLocales?: string
32-
where?: Where
32+
where?: string | Where
3333
}
3434

3535
type ParsedParams = {
@@ -111,5 +111,9 @@ export const parseParams = (params: RawParams): ParsedParams => {
111111
parsedParams.data = JSON.parse(params.data)
112112
}
113113

114+
if ('where' in params && typeof params.where === 'string' && params.where.length > 0) {
115+
parsedParams.where = JSON.parse(params.where) as Where
116+
}
117+
114118
return parsedParams
115119
}

0 commit comments

Comments
 (0)