Skip to content

fix(adapter-pg): properly serialize string values for JSON fields#29351

Open
buyuan-dev wants to merge 3 commits intoprisma:mainfrom
buyuan-dev:fix/issue-29330
Open

fix(adapter-pg): properly serialize string values for JSON fields#29351
buyuan-dev wants to merge 3 commits intoprisma:mainfrom
buyuan-dev:fix/issue-29330

Conversation

@buyuan-dev
Copy link
Copy Markdown

@buyuan-dev buyuan-dev commented Mar 18, 2026

Summary

Fixes a bug where plain string values could not be written to JSON fields in PostgreSQL.

Problem

When writing a plain string to a JSON field:

prisma.simple.create({data: {value: "hello"}})

PostgreSQL would reject with:

invalid input syntax for type json

This happened because the adapter was passing the string directly to node-postgres without JSON.stringify.

Fix

Ensure string values for JSON fields are properly serialized using JSON.stringify before being passed to the postgres driver.

Testing

  • Verified plain strings work in JSON fields
  • All existing adapter-pg tests pass
  • Numbers and booleans still work correctly

Fixes #29330

Summary by CodeRabbit

  • Bug Fixes

    • JSON values are now correctly formatted when sent to PostgreSQL, preventing invalid JSON input errors.
  • Tests

    • Added functional tests for JSON string-serialization behavior, a test matrix that excludes SQLite, and a schema setup used by the new tests.

Fixes prisma#29330

When writing plain string values to JSON fields in PostgreSQL, the adapter was passing the string directly to node-postgres without JSON.stringify. PostgreSQL expects valid JSON, so plain strings would fail with invalid input syntax for type json.

The fix ensures string values for JSON fields are properly serialized using JSON.stringify before being passed to the postgres driver.
@CLAassistant
Copy link
Copy Markdown

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai bot commented Mar 18, 2026

Walkthrough

PostgreSQL adapter now stringifies values for JSON scalar types so PostgreSQL receives valid JSON; new functional tests plus test matrix and Prisma schema files are added to validate JSON string-serialization behavior (SQLite excluded).

Changes

Cohort / File(s) Summary
Postgres adapter conversion
packages/adapter-pg/src/conversion.ts
Updates mapArg to stringify values when argType.scalarType === 'json', preventing invalid input syntax for Postgres json; includes minor non-functional formatting tweaks.
Functional tests & matrix
packages/client/tests/functional/issues/29330-json-string-serialization/_matrix.ts, packages/client/tests/functional/issues/29330-json-string-serialization/tests.ts
Adds a test matrix that excludes SQLite and a test suite verifying writing/reading plain strings and various JSON-typed values to JSON fields.
Prisma test schema
packages/client/tests/functional/issues/29330-json-string-serialization/prisma/_schema.ts
Adds a Prisma schema defining a Document model with content: Json and metadata: Json? for test setup.
🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title clearly describes the main change: fixing proper serialization of string values for PostgreSQL JSON fields, which is the core objective of this PR.
Linked Issues check ✅ Passed The PR addresses all coding requirements from issue #29330: implementing JSON.stringify for string values in JSON fields to fix 'invalid input syntax' errors, with comprehensive test coverage for various JSON types excluding SQLite.
Out of Scope Changes check ✅ Passed All changes are directly scoped to the linked issue: the adapter-pg fix for JSON serialization, test matrix configuration, schema setup, and functional tests for issue #29330.

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

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests
📝 Coding Plan
  • Generate coding plan for human review comments

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.

Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1


ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: ASSERTIVE

Plan: Pro

Run ID: f9a71a27-97ad-495e-8db9-a4872bd321ed

📥 Commits

Reviewing files that changed from the base of the PR and between 33667c3 and 0e611fa.

📒 Files selected for processing (1)
  • packages/adapter-pg/src/conversion.ts

@@ -1,4 +1,4 @@
import { ArgType, type ColumnType, ColumnTypeEnum } from '@prisma/driver-adapter-utils'
import { ArgType, type ColumnType, ColumnTypeEnum } from '@prisma/driver-adapter-utils'
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟡 Minor

Remove BOM character from file.

There appears to be a Byte Order Mark (U+FEFF) character at the beginning of the file before import. This invisible character can cause issues with some build tools and linters.

🔧 Proposed fix
-import { ArgType, type ColumnType, ColumnTypeEnum } from '@prisma/driver-adapter-utils'
+import { ArgType, type ColumnType, ColumnTypeEnum } from '@prisma/driver-adapter-utils'
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
import { ArgType, type ColumnType, ColumnTypeEnum } from '@prisma/driver-adapter-utils'
import { ArgType, type ColumnType, ColumnTypeEnum } from '@prisma/driver-adapter-utils'

Copy link
Copy Markdown
Contributor

@jacek-prisma jacek-prisma left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This needs a regression test based on the issue that is being addressed, you can have a look at packages/client/tests/functional/issues/29309-datetime-cursor for reference

@buyuan-dev
Copy link
Copy Markdown
Author

@jacek-prisma ✅ Regression tests have been added, following the structure of #29309.

Test coverage:

  • String values correctly serialized to JSON fields
  • Various JSON types (string/number/boolean/null/object/array)
  • Uses PostgreSQL and other SQL databases (SQLite excluded)

Test files:

  • packages/client/tests/functional/issues/29330-json-string-serialization/_matrix.ts
  • packages/client/tests/functional/issues/29330-json-string-serialization/prisma/_schema.ts
  • packages/client/tests/functional/issues/29330-json-string-serialization/tests.ts

Please let me know if the tests meet the requirements. Thanks!

Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 2


ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: ASSERTIVE

Plan: Pro

Run ID: 6143bed5-1db9-4700-862a-0921decdd0f9

📥 Commits

Reviewing files that changed from the base of the PR and between 0e611fa and 1ceb8d4.

📒 Files selected for processing (3)
  • packages/client/tests/functional/issues/29330-json-string-serialization/_matrix.ts
  • packages/client/tests/functional/issues/29330-json-string-serialization/prisma/_schema.ts
  • packages/client/tests/functional/issues/29330-json-string-serialization/tests.ts

Comment on lines +10 to +22
// Test plain string value in JSON field
const document = await prisma.document.create({
data: {
content: 'hello world',
metadata: { author: 'test' },
},
})

expect(document.content).toBe('hello world')
expect(document.metadata).toEqual({ author: 'test' })

// Test with findMany
const found = await prisma.document.findFirst({
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 Nitpick | 🔵 Trivial

Remove stale/non-informative inline comments.

Line 21 says “findMany” but the code calls findFirst, and both comments here describe what the code does rather than why.

As per coding guidelines: “Avoid adding useless code comments that do not add new information. Only write inline comments explaining Why (context, background, GitHub issues, decisions), not What or How.”

Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

♻️ Duplicate comments (1)
packages/client/tests/functional/issues/29330-json-string-serialization/tests.ts (1)

10-22: ⚠️ Potential issue | 🟡 Minor

Remove stale/non-informative inline comments.

These comments describe what the code does, and Line 21 is stale (findFirst is used, not findMany).

Proposed cleanup
-      // Test plain string value in JSON field
       const document = await prisma.document.create({
         data: {
           content: 'hello world',
           metadata: { author: 'test' },
         },
       })

       expect(document.content).toBe('hello world')
       expect(document.metadata).toEqual({ author: 'test' })

-      // Test with findMany
       const found = await prisma.document.findFirst({
         where: { id: document.id },
       })
As per coding guidelines: “Avoid adding useless code comments that do not add new information. Only write inline comments explaining Why (context, background, GitHub issues, decisions), not What or How.”

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: ASSERTIVE

Plan: Pro

Run ID: e8cc6b10-ae3c-4de6-997b-37ca7183802e

📥 Commits

Reviewing files that changed from the base of the PR and between 1ceb8d4 and be38c47.

📒 Files selected for processing (1)
  • packages/client/tests/functional/issues/29330-json-string-serialization/tests.ts

@buyuan-dev
Copy link
Copy Markdown
Author

? Automated check: Token is configured correctly. I can now post comments and push fixes automatically.

@jacek-prisma
Copy link
Copy Markdown
Contributor

Looks like mongodb is missing from the opt out list: https://github.com/prisma/prisma/actions/runs/23278580526/job/68178478878?pr=29351

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.

plain string value for json field can't be written to postgresql

3 participants