Skip to content

fix: use safeJsonStringify for nested Uint8Array in Json fields#29268

Merged
jacek-prisma merged 1 commit intoprisma:mainfrom
jay-l-e-e:fix/fix-29267
Feb 27, 2026
Merged

fix: use safeJsonStringify for nested Uint8Array in Json fields#29268
jacek-prisma merged 1 commit intoprisma:mainfrom
jay-l-e-e:fix/fix-29267

Conversation

@jay-l-e-e
Copy link
Copy Markdown
Contributor

@jay-l-e-e jay-l-e-e commented Feb 27, 2026

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

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.

@CLAassistant
Copy link
Copy Markdown

CLAassistant commented Feb 27, 2026

CLA assistant check
All committers have signed the CLA.

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai bot commented Feb 27, 2026

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info

Configuration used: Organization UI

Review profile: ASSERTIVE

Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between de0ff31 and 4581fe9.

📒 Files selected for processing (4)
  • packages/client/src/runtime/core/engines/client/parameterization/parameterize.ts
  • packages/client/tests/functional/issues/29267-uint8array-in-json/_matrix.ts
  • packages/client/tests/functional/issues/29267-uint8array-in-json/prisma/_schema.ts
  • packages/client/tests/functional/issues/29267-uint8array-in-json/tests.ts

Walkthrough

This change replaces JSON.stringify with safeJsonStringify in the parameterization engine for JSON-typed placeholders, affecting how nested values are serialized. It includes a functional test suite for Uint8Array serialization in JSON fields across multiple database providers.

Changes

Cohort / File(s) Summary
Runtime Parameterization
packages/client/src/runtime/core/engines/client/parameterization/parameterize.ts
Replaces direct JSON.stringify calls with safeJsonStringify in two code paths: when handling Json-scalar arrays and when handling Json-typed objects. This alters serialization behavior for parameterized Json values while preserving placeholder creation and type inference logic.
Functional Tests for Uint8Array Serialization
packages/client/tests/functional/issues/29267-uint8array-in-json/_matrix.ts, packages/client/tests/functional/issues/29267-uint8array-in-json/prisma/_schema.ts, packages/client/tests/functional/issues/29267-uint8array-in-json/tests.ts
Adds test matrix configuration, dynamic Prisma schema generator, and functional tests covering Uint8Array serialization in JSON fields. Tests validate base64 string conversion for Uint8Array values nested in objects, arrays, and at top-level, excluding SQL Server provider.
🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title 'fix: use safeJsonStringify for nested Uint8Array in Json fields' directly describes the main code change and is specific to the solution implemented in the PR.
Linked Issues check ✅ Passed All code requirements from issue #29267 are met: the parameterization module now uses safeJsonStringify instead of JSON.stringify for Json-typed values, and comprehensive functional tests cover all regression cases (nested-in-object, nested-in-array, top-level, deeply-nested Uint8Array).
Out of Scope Changes check ✅ Passed All code changes are directly related to fixing issue #29267: modifying parameterization serialization behavior and adding regression tests; no unrelated changes present.
Docstring Coverage ✅ Passed No functions found in the changed files to evaluate docstring coverage. Skipping docstring coverage check.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Post copyable unit tests in a comment

Tip

Try Coding Plans. Let us write the prompt for your AI agent so you can ship faster (with fewer bugs).
Share your feedback on Discord.


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.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@jay-l-e-e
Copy link
Copy Markdown
Contributor Author

@jacek-prisma Hi again! Please review this PR. Thank you!

@jacek-prisma jacek-prisma merged commit 455853d into prisma:main Feb 27, 2026
238 of 241 checks passed
jacek-prisma pushed a commit that referenced this pull request Feb 27, 2026
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 -->
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Regression 7.4.1] Nested Uint8Array in Json fields serialized as numeric-keyed objects instead of base64 strings

3 participants