Skip to content

Commit

Permalink
fix(delegate): null reference when reading an optional relation (#1491)
Browse files Browse the repository at this point in the history
  • Loading branch information
ymc9 authored Jun 7, 2024
1 parent ab9d27f commit 41880f3
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 0 deletions.
8 changes: 8 additions & 0 deletions packages/runtime/src/enhancements/delegate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1101,6 +1101,10 @@ export class DelegateProxyHandler extends DefaultPrismaProxyHandler {
}

private assembleUp(model: string, entity: any) {
if (!entity) {
return entity;
}

const result: any = {};
const base = this.getBaseModel(model);

Expand Down Expand Up @@ -1148,6 +1152,10 @@ export class DelegateProxyHandler extends DefaultPrismaProxyHandler {
}

private assembleDown(model: string, entity: any) {
if (!entity) {
return entity;
}

const result: any = {};
const modelInfo = getModelInfo(this.options.modelMeta, model, true);

Expand Down
68 changes: 68 additions & 0 deletions tests/regression/tests/issue-1483.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
import { loadSchema } from '@zenstackhq/testtools';
describe('issue 1483', () => {
it('regression', async () => {
const { enhance } = await loadSchema(
`
model User {
@@auth
id String @id
edits Edit[]
@@allow('all', true)
}
model Entity {
id String @id @default(cuid())
name String
edits Edit[]
type String
@@delegate(type)
@@allow('all', true)
}
model Person extends Entity {
}
model Edit {
id String @id @default(cuid())
authorId String?
author User? @relation(fields: [authorId], references: [id], onDelete: Cascade, onUpdate: NoAction)
entityId String
entity Entity @relation(fields: [entityId], references: [id], onDelete: Cascade, onUpdate: NoAction)
@@allow('all', true)
}
`
);

const db = enhance();
await db.edit.deleteMany({});
await db.person.deleteMany({});
await db.user.deleteMany({});

const person = await db.person.create({
data: {
name: 'test',
},
});

await db.edit.create({
data: {
entityId: person.id,
},
});

await expect(
db.edit.findMany({
include: {
author: true,
entity: true,
},
})
).resolves.toHaveLength(1);
});
});

0 comments on commit 41880f3

Please sign in to comment.