ReviewOS

also looking at this

stacks/bun-query-builder

fix(client): whereJsonContains binds the document, not its JSON text (#1091)

#1100
Merged glennmichael123 wants to merge fix/1091-json-contains-double-encode into main
3 files +172 -3
packages/bun-query-builder/test/client.json-contains.test.tsmodified+33-0
Changes to packages/bun-query-builder/test/client.json-contains.test.ts
@@ -67,4 +67,37 @@ describe('whereJsonContains dialect handling (#1026)', () => {
6767 // (`whereJsonContains('tags', ['bun'])` against a seeded table returns the
6868 // matching row); an in-suite execution test is omitted because the shared
6969 // lazy-connection state across test files makes it flaky.
70
71 /**
72 * #1091. Postgres binds a number as int4 and a boolean as bool, neither of
73 * which `@>` accepts (`operator does not exist: jsonb @> integer`), so a
74 * scalar operand is lifted to a jsonb scalar. Documents are bound directly
75 * and must NOT be lifted — the driver already encodes those as jsonb.
76 *
77 * These assert the SQL shape only. The bug this guards against lived
78 * entirely in the bound parameter, which `toSQL()` does not expose, so the
79 * real guard is the execution test in client.json-contains.pg.test.ts.
80 */
81 it('Postgres: lifts a number or boolean operand with to_jsonb (#1091)', () => {
82 config.dialect = 'postgres' as any
83 config.sql = { ...config.sql, jsonContainsMode: 'operator' }
84 const num = String((qb() as any).selectFrom('posts').whereJsonContains('tags', 1).toSQL())
85 expect(num).toContain('tags @> to_jsonb($1)')
86 const bool = String((qb() as any).selectFrom('posts').whereJsonContains('tags', true).toSQL())
87 expect(bool).toContain('tags @> to_jsonb($1)')
88
89 config.sql = { ...config.sql, jsonContainsMode: 'function' }
90 const fn = String((qb() as any).selectFrom('posts').whereJsonContains('tags', 1).toSQL())
91 expect(fn).toContain('jsonb_contains(tags, to_jsonb($1))')
92 })
93
94 it('Postgres: binds a document operand directly, without to_jsonb (#1091)', () => {
95 config.dialect = 'postgres' as any
96 config.sql = { ...config.sql, jsonContainsMode: 'operator' }
97 for (const operand of [['bun'], { published: true }, 'bun']) {
98 const s = String((qb() as any).selectFrom('posts').whereJsonContains('tags', operand).toSQL())
99 expect(s).toContain('tags @> $1')
100 expect(s).not.toContain('to_jsonb')
101 }
102 })
70103})