fix: use safeJsonStringify for nested Uint8Array in Json fields#29268
fix: use safeJsonStringify for nested Uint8Array in Json fields#29268jacek-prisma merged 1 commit intoprisma:mainfrom
Conversation
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review infoConfiguration used: Organization UI Review profile: ASSERTIVE Plan: Pro 📒 Files selected for processing (4)
WalkthroughThis change replaces Changes
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
Tip Try Coding Plans. Let us write the prompt for your AI agent so you can ship faster (with fewer bugs). Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
|
@jacek-prisma Hi again! Please review this PR. Thank you! |
Fixes #29267 ## Problem `Uint8Array` values nested inside objects or arrays are serialized as numeric-keyed objects (e.g. `{"0":72,"1":101,"2":108,"3":108,"4":111}`) instead of base64 strings when stored in a `Json` field. Top-level `Uint8Array` values are unaffected. This was introduced in #29182, which added `deserializeJsonObject()` before `JSON.stringify()` in the parameterization step to fix Date handling in Json fields. `deserializeJsonObject` correctly converts `{ $type: 'Bytes', value: 'SGVsbG8=' }` back to a `Uint8Array`, but `JSON.stringify` cannot natively serialize `Uint8Array` and falls back to `{"0":72,...}`. ## Fix Replace `JSON.stringify` with `safeJsonStringify` (already exported from `@prisma/client-engine-runtime`) in both the `#handleArray` and `#handleObject` methods of `Parameterizer`. `safeJsonStringify` uses a custom replacer that converts `ArrayBuffer.isView` values to base64 strings and `BigInt` to string representation. ## Tests Added a functional regression test in `packages/client/tests/functional/issues/29267-uint8array-in-json/` covering: - `Uint8Array` nested inside an object - `Uint8Array` nested inside an array - `Uint8Array` passed directly (already working, included for completeness) - Deeply nested `Uint8Array` <!-- This is an auto-generated comment: release notes by coderabbit.ai --> ## Summary by CodeRabbit ## Release Notes * **Bug Fixes** * Improved JSON serialization for parameter handling to properly convert Uint8Array values to base64 strings when stored in JSON fields, ensuring correct data handling across create operations. * **Tests** * Added comprehensive test coverage for Uint8Array serialization in JSON fields, including scenarios with nested objects, arrays, and various nesting depths across multiple database providers. <!-- end of auto-generated comment: release notes by coderabbit.ai -->
Fixes #29267
Problem
Uint8Arrayvalues nested inside objects or arrays are serialized as numeric-keyed objects (e.g.{"0":72,"1":101,"2":108,"3":108,"4":111}) instead of base64 strings when stored in aJsonfield. Top-levelUint8Arrayvalues are unaffected.This was introduced in #29182, which added
deserializeJsonObject()beforeJSON.stringify()in the parameterization step to fix Date handling in Json fields.deserializeJsonObjectcorrectly converts{ $type: 'Bytes', value: 'SGVsbG8=' }back to aUint8Array, butJSON.stringifycannot natively serializeUint8Arrayand falls back to{"0":72,...}.Fix
Replace
JSON.stringifywithsafeJsonStringify(already exported from@prisma/client-engine-runtime) in both the#handleArrayand#handleObjectmethods ofParameterizer.safeJsonStringifyuses a custom replacer that convertsArrayBuffer.isViewvalues to base64 strings andBigIntto string representation.Tests
Added a functional regression test in
packages/client/tests/functional/issues/29267-uint8array-in-json/covering:Uint8Arraynested inside an objectUint8Arraynested inside an arrayUint8Arraypassed directly (already working, included for completeness)Uint8ArraySummary by CodeRabbit
Release Notes
Bug Fixes
Tests