Skip to content

Commit aee8f57

Browse files
authored
fix: fix test JSON repr issues (#5719)
The `safeJsonStringify` we use in tests doesn't handle `Buffer`s correctly. We normally would not convert them to JSON in production, but the JSON representation is used for tests snapshots, so I replaced `safeJsonStringify` with a custom one. D1 doesn't actually return a Buffer so I just updated its snapshot.
1 parent 9d980ce commit aee8f57

File tree

2 files changed

+38
-2
lines changed

2 files changed

+38
-2
lines changed

libs/driver-adapters/executor/src/qc-test-worker/worker-query.ts

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ import {
1212
type QueryInterpreterTransactionManager,
1313
QueryPlanNode,
1414
RawResponse,
15-
safeJsonStringify,
1615
type TransactionManager,
1716
UserFacingError,
1817
} from '@prisma/client-engine-runtime'
@@ -302,3 +301,36 @@ function getFullOperationName(query: JsonProtocolQuery): string {
302301
}
303302
}
304303
}
304+
305+
/**
306+
* `JSON.stringify` wrapper with custom replacer function that handles nested
307+
* BigInt and Buffer values.
308+
*/
309+
export function safeJsonStringify(obj: unknown): string {
310+
// Recursively convert any values that we cannot pass
311+
// to JSON.stringify to a serializable format.
312+
// We cannot use the JSON.serialize(obj, transform), because it calls
313+
// `toJSON` methods on objects, which we want to avoid.
314+
function transformValue(value: unknown): unknown {
315+
if (typeof value === 'bigint') {
316+
return value.toString()
317+
} else if (ArrayBuffer.isView(value)) {
318+
return Buffer.from(
319+
value.buffer,
320+
value.byteOffset,
321+
value.byteLength,
322+
).toString('base64')
323+
} else if (Array.isArray(value)) {
324+
return value.map(transformValue)
325+
} else if (value !== null && typeof value === 'object') {
326+
const result: Record<string, unknown> = {}
327+
for (const [key, val] of Object.entries(value)) {
328+
result[key] = transformValue(val)
329+
}
330+
return result
331+
}
332+
return value
333+
}
334+
335+
return JSON.stringify(transformValue(obj))
336+
}

query-engine/connector-test-kit-rs/query-engine-tests/tests/raw/sql/typed_output.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -661,7 +661,11 @@ mod typed_output {
661661
42,
662662
92233720368,
663663
1.5432,
664-
"AQID",
664+
[
665+
1,
666+
2,
667+
3
668+
],
665669
1,
666670
"1900-10-10T01:10:10.001+00:00",
667671
123.4567891

0 commit comments

Comments
 (0)