Skip to content

Commit

Permalink
fix: deal with payload field value with undefined (#778)
Browse files Browse the repository at this point in the history
  • Loading branch information
ymc9 authored Oct 22, 2023
1 parent 876e013 commit e41fc74
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 8 deletions.
14 changes: 8 additions & 6 deletions packages/runtime/src/enhancements/nested-write-visitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -323,12 +323,14 @@ export class NestedWriteVisitor {
}

if (fieldInfo.isDataModel) {
// recurse into nested payloads
for (const [subAction, subData] of Object.entries<any>(payload[field])) {
if (this.isPrismaWriteAction(subAction) && subData) {
await this.doVisit(fieldInfo.type, subAction, subData, payload[field], fieldInfo, [
...nestingPath,
]);
if (payload[field]) {
// recurse into nested payloads
for (const [subAction, subData] of Object.entries<any>(payload[field])) {
if (this.isPrismaWriteAction(subAction) && subData) {
await this.doVisit(fieldInfo.type, subAction, subData, payload[field], fieldInfo, [
...nestingPath,
]);
}
}
}
} else {
Expand Down
6 changes: 4 additions & 2 deletions packages/runtime/src/enhancements/policy/policy-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -466,8 +466,10 @@ export class PolicyUtil {
) {
// multi-field unique constraint, flatten it
delete args[field];
for (const [f, v] of Object.entries(value)) {
args[f] = v;
if (value) {
for (const [f, v] of Object.entries(value)) {
args[f] = v;
}
}
}
}
Expand Down
37 changes: 37 additions & 0 deletions tests/integration/tests/regression/issue-765.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { loadSchema } from '@zenstackhq/testtools';

describe('Regression: issue 765', () => {
it('regression', async () => {
const { enhance } = await loadSchema(
`
model User {
id Int @id @default(autoincrement())
name String
post Post? @relation(fields: [postId], references: [id])
postId Int?
@@allow('all', true)
}
model Post {
id Int @id @default(autoincrement())
title String
User User[]
@@allow('all', true)
}
`
);

const db = enhance();
const r = await db.user.create({
data: {
name: 'Me',
post: undefined,
},
});
expect(r.name).toBe('Me');
expect(r.post).toBeUndefined();
});
});

0 comments on commit e41fc74

Please sign in to comment.