From 26292dc99b870ffee3477f0f3652f640dedadd1b Mon Sep 17 00:00:00 2001 From: ymc9 <104139426+ymc9@users.noreply.github.com> Date: Sat, 16 Nov 2024 11:32:19 -0800 Subject: [PATCH] fix(json): support recursive definitions --- packages/sdk/src/code-gen.ts | 1 + .../tests/enhancements/json/crud.test.ts | 45 ++++++++++++++++ .../tests/enhancements/json/typing.test.ts | 53 +++++++++++++++++++ 3 files changed, 99 insertions(+) diff --git a/packages/sdk/src/code-gen.ts b/packages/sdk/src/code-gen.ts index 7ed528aa..bc83b02e 100644 --- a/packages/sdk/src/code-gen.ts +++ b/packages/sdk/src/code-gen.ts @@ -15,6 +15,7 @@ export function createProject(options?: CompilerOptions) { strict: true, skipLibCheck: true, noEmitOnError: true, + noImplicitAny: false, ...options, }, }); diff --git a/tests/integration/tests/enhancements/json/crud.test.ts b/tests/integration/tests/enhancements/json/crud.test.ts index 9cd7ff8a..be1f218d 100644 --- a/tests/integration/tests/enhancements/json/crud.test.ts +++ b/tests/integration/tests/enhancements/json/crud.test.ts @@ -344,4 +344,49 @@ describe('Json field CRUD', () => { expect(u2.profile.ownerId).toBe(2); expect(u2.profile.nested.userId).toBe(3); }); + + it('works with recursive types', async () => { + const params = await loadSchema( + ` + type Content { + type String + content Content[]? + text String? + } + + model Post { + id Int @id @default(autoincrement()) + content Content @json + @@allow('all', true) + } + `, + { + provider: 'postgresql', + dbUrl, + } + ); + + prisma = params.prisma; + const db = params.enhance(); + const post = await db.post.create({ + data: { + content: { + type: 'text', + content: [ + { + type: 'text', + content: [ + { + type: 'text', + text: 'hello', + }, + ], + }, + ], + }, + }, + }); + + await expect(post.content.content[0].content[0].text).toBe('hello'); + }); }); diff --git a/tests/integration/tests/enhancements/json/typing.test.ts b/tests/integration/tests/enhancements/json/typing.test.ts index a73e04f0..a2053770 100644 --- a/tests/integration/tests/enhancements/json/typing.test.ts +++ b/tests/integration/tests/enhancements/json/typing.test.ts @@ -325,6 +325,59 @@ async function main() { dateTime: new Date(), json: { a: 1 }, } +} + `, + }, + ], + } + ); + }); + + it('supports recursive definition', async () => { + await loadSchema( + ` + type Content { + type String + content Content[]? + text String? + } + + model Post { + id Int @id @default(autoincrement()) + content Content @json + } + `, + { + provider: 'postgresql', + pushDb: false, + compile: true, + extraSourceFiles: [ + { + name: 'main.ts', + content: ` +import type { Content } from '.zenstack/models'; +import { enhance } from '.zenstack/enhance'; +import { PrismaClient } from '@prisma/client'; + +async function main() { + const content: Content = { + type: 'text', + content: [ + { + type: 'text', + content: [ + { + type: 'text', + text: 'hello', + }, + ], + }, + ], + } + + const db = enhance(new PrismaClient()); + const post = await db.post.create({ data: { content } }); + console.log(post.content.content?.[0].content?.[0].text); } `, },