Skip to content
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
4 changes: 4 additions & 0 deletions packages/plugins/policy/src/policy-handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -920,6 +920,10 @@ export class PolicyHandler<Schema extends SchemaDef> extends OperationNodeTransf
for (let i = 0; i < data.length; i++) {
const item = data[i]!;
if (typeof item === 'object' && item && 'kind' in item) {
if (item.kind === 'DefaultInsertValueNode') {
result.push({ node: ValueNode.create(null), raw: null });
continue;
}
const fieldDef = QueryUtils.requireField(this.client.$schema, model, fields[i]!);
invariant(item.kind === 'ValueNode', 'expecting a ValueNode');
result.push({
Expand Down
37 changes: 37 additions & 0 deletions tests/regression/test/issue-2460.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { createPolicyTestClient } from '@zenstackhq/testtools';
import { describe, expect, it } from 'vitest';

// createManyAndReturn fails with "Invariant failed: expecting a ValueNode"
// when rows in the batch have asymmetric columns (one row provides a field the other omits)
describe('Regression for issue #2460', () => {
it('createManyAndReturn with asymmetric optional fields across rows', async () => {
const db = await createPolicyTestClient(
`
model User {
id Int @id @default(autoincrement())
role String
@@allow('all', true)
}

model Item {
id Int @id @default(autoincrement())
key String
note String?
@@allow('all', auth().role == 'admin')
}
`,
{ provider: 'postgresql' },
);

const user = await db.user.create({ data: { role: 'admin' } });

const result = await db.$setAuth(user).item.createManyAndReturn({
data: [
{ key: 'a', note: 'hello' },
{ key: 'b' },
],
});

expect(result).toHaveLength(2);
});
});
Loading