From 3612baceb1323c28574a7d3be89d672fdc346879 Mon Sep 17 00:00:00 2001 From: ymc9 <104139426+ymc9@users.noreply.github.com> Date: Thu, 20 Mar 2025 21:33:49 -0700 Subject: [PATCH] fix: incorrect value type when using `@default` with boolean field from `auth()` fixes #2038 --- .../src/plugins/prisma/schema-generator.ts | 2 +- tests/regression/tests/issue-2038.test.ts | 26 +++++++++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) create mode 100644 tests/regression/tests/issue-2038.test.ts diff --git a/packages/schema/src/plugins/prisma/schema-generator.ts b/packages/schema/src/plugins/prisma/schema-generator.ts index 8df59941c..1194099d2 100644 --- a/packages/schema/src/plugins/prisma/schema-generator.ts +++ b/packages/schema/src/plugins/prisma/schema-generator.ts @@ -893,7 +893,7 @@ export class PrismaSchemaGenerator { const dummyDefaultValue = match(field.type.type) .with('String', () => new AttributeArgValue('String', '')) .with(P.union('Int', 'BigInt', 'Float', 'Decimal'), () => new AttributeArgValue('Number', '0')) - .with('Boolean', () => new AttributeArgValue('Boolean', 'false')) + .with('Boolean', () => new AttributeArgValue('Boolean', false)) .with('DateTime', () => new AttributeArgValue('FunctionCall', new PrismaFunctionCall('now'))) .with('Json', () => new AttributeArgValue('String', '{}')) .with('Bytes', () => new AttributeArgValue('String', '')) diff --git a/tests/regression/tests/issue-2038.test.ts b/tests/regression/tests/issue-2038.test.ts new file mode 100644 index 000000000..02218eaab --- /dev/null +++ b/tests/regression/tests/issue-2038.test.ts @@ -0,0 +1,26 @@ +import { loadSchema } from '@zenstackhq/testtools'; + +describe('issue 2038', () => { + it('regression', async () => { + const { enhance } = await loadSchema( + ` + model User { + id Int @id @default(autoincrement()) + flag Boolean + @@allow('all', true) + } + + model Post { + id Int @id @default(autoincrement()) + published Boolean @default(auth().flag) + @@allow('all', true) + } + ` + ); + + const db = enhance({ id: 1, flag: true }); + await expect(db.post.create({ data: {} })).resolves.toMatchObject({ + published: true, + }); + }); +});