Skip to content

fix: deal with payload field value with undefined #778

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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();
});
});