From cb72ed5bacbf08b4981c55761c6bead6257df7d4 Mon Sep 17 00:00:00 2001 From: krist7599555 <19445033+krist7599555@users.noreply.github.com> Date: Mon, 16 Nov 2026 16:11:49 +0700 Subject: [PATCH] fix(policy): unwrap CastNode for Json values in non-constant create policy Fixes #2920 Co-Authored-By: Claude Sonnet 5.5 --- packages/plugins/policy/src/policy-handler.ts | 9 +++-- tests/regression/test/issue-2920.test.ts | 36 +++++++++++++++++++ 2 files changed, 42 insertions(+), 3 deletions(-) create mode 100644 tests/regression/test/issue-2920.test.ts diff --git a/packages/plugins/policy/src/policy-handler.ts b/packages/plugins/policy/src/policy-handler.ts index 6b96c07e6..b08045a39 100644 --- a/packages/plugins/policy/src/policy-handler.ts +++ b/packages/plugins/policy/src/policy-handler.ts @@ -12,6 +12,7 @@ import { AliasNode, AndNode, BinaryOperationNode, + CastNode, ColumnNode, DefaultInsertValueNode, DeleteQueryNode, @@ -1161,12 +1162,14 @@ export class PolicyHandler extends OperationNodeTransf continue; } const fieldDef = QueryUtils.requireField(this.client.$schema, model, fields[i]!); - invariant(ValueNode.is(itemNode), `expecting a ValueNode, got ${itemNode.kind}`); + // some dialects wrap the value in a cast (e.g. MySQL's `CAST(? AS JSON)` for Json fields) + const valueNode = CastNode.is(itemNode) ? itemNode.expression : itemNode; + invariant(ValueNode.is(valueNode), `expecting a ValueNode, got ${valueNode.kind}`); result.push({ node: ValueNode.create( - this.dialect.transformInput(itemNode.value, fieldDef.type as BuiltinType, !!fieldDef.array), + this.dialect.transformInput(valueNode.value, fieldDef.type as BuiltinType, !!fieldDef.array), ), - raw: itemNode.value, + raw: valueNode.value, }); } else { let value: unknown = item; diff --git a/tests/regression/test/issue-2920.test.ts b/tests/regression/test/issue-2920.test.ts new file mode 100644 index 000000000..4a69ca7a0 --- /dev/null +++ b/tests/regression/test/issue-2920.test.ts @@ -0,0 +1,36 @@ +import { createPolicyTestClient } from '@zenstackhq/testtools'; +import { describe, expect, it } from 'vitest'; + +describe('Regression for issue 2920', () => { + it('creates a row with a Json field under a policy that needs the inserted row', async () => { + const db = await createPolicyTestClient( + ` +model Author { + id Int @id @default(autoincrement()) + open Boolean + posts Post[] + @@allow('create,read', true) + @@allow('update', true) +} + +model Post { + id Int @id @default(autoincrement()) + meta Json? + author Author @relation(fields: [authorId], references: [id]) + authorId Int + @@allow('read', true) + @@allow('create', author.open) +} + `, + { provider: 'mysql', usePrismaPush: true } as any, + ); + + await db.author.create({ data: { id: 1, open: true } }); + await db.author.create({ data: { id: 2, open: false } }); + + await expect(db.post.create({ data: { authorId: 1, meta: { a: 1 } } })).resolves.toMatchObject({ + meta: { a: 1 }, + }); + await expect(db.post.create({ data: { authorId: 2, meta: { a: 1 } } })).toBeRejectedByPolicy(); + }); +});