fix(policy): handle DefaultInsertValueNode in createManyAndReturn#2461
Conversation
createManyAndReturn with PolicyPlugin fails when rows have asymmetric optional fields (one row provides a field the other omits). Refs: zenstackhq#2460 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
📝 WalkthroughWalkthroughAdds handling for Changes
Estimated code review effort🎯 2 (Simple) | ⏱️ ~8 minutes Poem
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
🧹 Nitpick comments (1)
tests/regression/test/issue-2460.test.ts (1)
8-36: Always disconnect the test client in afinallyblock.A failure before Line 36 skips
$disconnect(), which can leak the DB connection and destabilize the regression suite.♻️ Suggested change
- 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); - await db.$disconnect(); + try { + 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); + } finally { + await db.$disconnect(); + }🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@tests/regression/test/issue-2460.test.ts` around lines 8 - 36, Ensure the test always closes the DB connection by moving the db.$disconnect() call into a finally block: after creating the client via createPolicyTestClient(...) and performing the operations (user creation, db.$setAuth(user).item.createManyAndReturn(...)), wrap the main test logic in try { ... } and put await db.$disconnect() inside finally { ... } so that db.$disconnect() runs even if an assertion or DB operation throws; reference the existing createPolicyTestClient call and the db.$disconnect invocation when making the change.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Nitpick comments:
In `@tests/regression/test/issue-2460.test.ts`:
- Around line 8-36: Ensure the test always closes the DB connection by moving
the db.$disconnect() call into a finally block: after creating the client via
createPolicyTestClient(...) and performing the operations (user creation,
db.$setAuth(user).item.createManyAndReturn(...)), wrap the main test logic in
try { ... } and put await db.$disconnect() inside finally { ... } so that
db.$disconnect() runs even if an assertion or DB operation throws; reference the
existing createPolicyTestClient call and the db.$disconnect invocation when
making the change.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Pro
Run ID: 436eae36-4b83-432f-8e18-b221fc69de55
📒 Files selected for processing (2)
packages/plugins/policy/src/policy-handler.tstests/regression/test/issue-2460.test.ts
When rows have asymmetric optional fields, Kysely pads missing columns with DefaultInsertValueNode. Treat it as null in the policy handler. Fixes zenstackhq#2460 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
9076fe6 to
22c0ad1
Compare
There was a problem hiding this comment.
🧹 Nitpick comments (1)
tests/regression/test/issue-2460.test.ts (1)
35-35: Consider adding assertions on the actual returned values.While the length check confirms the operation doesn't crash, asserting the actual field values would strengthen the regression test by verifying that the
DefaultInsertValueNodeis correctly treated asnull:♻️ Suggested enhancement
- expect(result).toHaveLength(2); + expect(result).toHaveLength(2); + expect(result[0].note).toBe('hello'); + expect(result[1].note).toBeNull();🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@tests/regression/test/issue-2460.test.ts` at line 35, The test currently only checks expect(result).toHaveLength(2); but should also assert the actual returned values to verify DefaultInsertValueNode is treated as null; update the test to assert the contents of result (for example, check each row/object fields and that the entry corresponding to the DefaultInsertValueNode is null or has the expected null-equivalent field) by adding expectations against result (e.g., expect(result[0].field).toBe(...), expect(result[1].field).toBeNull() or an appropriate shape) so the regression verifies value correctness rather than just length.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Nitpick comments:
In `@tests/regression/test/issue-2460.test.ts`:
- Line 35: The test currently only checks expect(result).toHaveLength(2); but
should also assert the actual returned values to verify DefaultInsertValueNode
is treated as null; update the test to assert the contents of result (for
example, check each row/object fields and that the entry corresponding to the
DefaultInsertValueNode is null or has the expected null-equivalent field) by
adding expectations against result (e.g., expect(result[0].field).toBe(...),
expect(result[1].field).toBeNull() or an appropriate shape) so the regression
verifies value correctness rather than just length.
ℹ️ Review info
⚙️ Run configuration
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Pro
Run ID: 84c427bc-f190-4d10-9e44-08bb0eeecca8
📒 Files selected for processing (2)
packages/plugins/policy/src/policy-handler.tstests/regression/test/issue-2460.test.ts
🚧 Files skipped from review as they are similar to previous changes (1)
- packages/plugins/policy/src/policy-handler.ts
ymc9
left a comment
There was a problem hiding this comment.
Thanks @pkudinov ! LGTM and it should resolve optional field issue.
For fields with default value and omitted during create, I think we need to back-fill default values during policy evaluation (so that policy rules can properly access them). I'll make a separate fix for that a bit later.
|
@claude review |
|
Claude encountered an error —— View job I'll analyze this and get back to you. |
Summary
Fixes #2460
createManyAndReturnwith PolicyPlugin crashes withInvariant failed: expecting a ValueNodewhen rows have asymmetric optional fields (one row provides a field, another omits it).Kysely pads missing columns with
DefaultInsertValueNode. The fix handles this node type inPolicyHandler.unwrapCreateValueRowby treating it asnull.Changes
packages/plugins/policy/src/policy-handler.ts— 4-line fix to handleDefaultInsertValueNodetests/regression/test/issue-2460.test.ts— regression testSummary by CodeRabbit
Bug Fixes
Tests