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.pg.test.tsadded+121-0
Changes to packages/bun-query-builder/test/client.json-contains.pg.test.ts
@@ -0,0 +1,121 @@
1/**
2 * whereJsonContains against a live Postgres — stacksjs/bun-query-builder#1091.
3 *
4 * The bug: `whereJsonContains` pushed `JSON.stringify(json)`, but Bun's driver
5 * already JSON-encodes a value bound to a jsonb parameter. The document was
6 * therefore encoded twice and Postgres received the jsonb *string*
7 * `"[\"bun\"]"` where the array `["bun"]` was meant. A jsonb string never
8 * `@>`-contains anything, so the predicate was vacuously false and every call
9 * returned zero rows, for every input, silently.
10 *
11 * This has to execute. The emitted SQL was correct throughout (`tags @> $1`) —
12 * only the bound parameter was wrong, and `toSQL()` does not expose parameters,
13 * so no SQL-text assertion can observe the difference. The existing #1026 suite
14 * asserted the text and stayed green across the entire lifetime of this bug.
15 *
16 * Note also that every negative case ("no row should match") passed while the
17 * bug was live, because the answer was always zero rows. Only positive cases —
18 * containment that genuinely holds — can fail here, so every case below asserts
19 * a row comes back except where it explicitly checks a non-match.
20 *
21 * Runs in a subprocess: `config.dialect` is global and other test files pin it
22 * to sqlite, which would mask the network-driver path entirely.
23 */
24
25import { mkdtempSync, rmSync, writeFileSync } from 'node:fs'
26import { tmpdir } from 'node:os'
27import { join, resolve } from 'node:path'
28import process from 'node:process'
29import { describe, expect, it } from 'bun:test'
30import { PG_URL, probePostgres } from './pg'
31
32const pgAvailable = await probePostgres()
33
34describe.skipIf(!pgAvailable)('whereJsonContains against live Postgres (#1091)', () => {
35 it('matches rows for documents and scalars, in both containment modes', () => {
36 const srcEntry = resolve(import.meta.dir, '../src/index.ts')
37 const dir = mkdtempSync(join(tmpdir(), 'qb-jc1091-'))
38 const scriptPath = join(dir, 'probe.ts')
39
40 writeFileSync(scriptPath, `
41import { SQL } from 'bun'
42import { setConfig, resetConnection, createQueryBuilder, buildDatabaseSchema, buildSchemaMeta } from ${JSON.stringify(srcEntry)}
43
44const URL = ${JSON.stringify(PG_URL)}
45const failures = []
46const check = (label, got, want) => {
47 const g = JSON.stringify(got), w = JSON.stringify(want)
48 if (g !== w) failures.push(label + ': got ' + g + ', want ' + w)
49}
50
51const raw = new SQL(URL)
52await raw.unsafe('DROP TABLE IF EXISTS _qb_jsoncontains')
53await raw.unsafe('CREATE TABLE _qb_jsoncontains (id int primary key, tags jsonb, meta jsonb)')
54await raw.unsafe(\`INSERT INTO _qb_jsoncontains VALUES
55 (1, '["bun","sql",1,true]', '{"published":true,"tier":"pro"}'),
56 (2, '["node"]', '{"published":false}')\`)
57await raw.end()
58
59setConfig({ dialect: 'postgres', database: { url: URL } })
60resetConnection()
61
62const models = { _qb_jsoncontains: { columns: {
63 id: { type: 'integer', isPrimaryKey: true }, tags: { type: 'json' }, meta: { type: 'json' },
64} } }
65const db = createQueryBuilder({
66 schema: buildDatabaseSchema(models),
67 meta: buildSchemaMeta(models),
68 autoMigration: { enabled: false },
69})
70
71const ids = async (column, operand) => {
72 const rows = await db.selectFrom('_qb_jsoncontains').select('id')
73 .whereJsonContains(column, operand).orderBy('id', 'asc').get()
74 return rows.map(r => Number(r.id))
75}
76
77// Documents. Each of these returned [] while #1091 was live.
78check('array', await ids('tags', ['bun']), [1])
79check('array multi-element', await ids('tags', ['bun', 'sql']), [1])
80check('object', await ids('meta', { published: true }), [1])
81check('object multi-key', await ids('meta', { published: true, tier: 'pro' }), [1])
82check('string', await ids('tags', 'bun'), [1])
83
84// Scalars. Bound as int4/bool, so these need the to_jsonb lift or Postgres
85// rejects the statement outright rather than returning nothing.
86check('number', await ids('tags', 1), [1])
87check('boolean', await ids('tags', true), [1])
88
89// Non-matches still do not match — the fix must not widen the predicate.
90check('array non-match', await ids('tags', ['nope']), [])
91check('object non-match', await ids('meta', { published: 'nope' }), [])
92check('string non-match', await ids('tags', 'zzz'), [])
93check('number non-match', await ids('tags', 99), [])
94
95// jsonb_contains() is the same containment through the function form.
96setConfig({ sql: { jsonContainsMode: 'function' } })
97check('function mode array', await ids('tags', ['bun']), [1])
98check('function mode number', await ids('tags', 1), [1])
99check('function mode non-match', await ids('tags', ['nope']), [])
100
101const cleanup = new SQL(URL)
102await cleanup.unsafe('DROP TABLE IF EXISTS _qb_jsoncontains')
103await cleanup.end()
104
105if (failures.length) {
106 console.error('FAILURES:\\n' + failures.join('\\n'))
107 process.exit(1)
108}
109console.log('OK')
110`)
111
112 const proc = Bun.spawnSync({ cmd: ['bun', scriptPath], stdout: 'pipe', stderr: 'pipe', cwd: process.cwd(), env: { ...process.env } })
113 const dec = new TextDecoder()
114 const out = dec.decode(proc.stdout).trim()
115 const err = dec.decode(proc.stderr).trim()
116 rmSync(dir, { recursive: true, force: true })
117
118 expect(proc.exitCode, `whereJsonContains pg probe failed.\nstdout: ${out}\nstderr: ${err}`).toBe(0)
119 expect(out).toContain('OK')
120 })
121})