-
-
Notifications
You must be signed in to change notification settings - Fork 12
fix(policy): relation field resolution issue #263
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
Conversation
WalkthroughAdjusted normalization of binary expressions involving relation fields to derive related types via getFieldDefFromFieldRef, use the related model’s id fields for comparisons, and add invariants ensuring valid relation field definitions before appending the first related id field to the expression. Changes
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Possibly related PRs
Poem
Pre-merge checks and finishing touches✅ Passed checks (3 passed)
✨ Finishing touches
🧪 Generate unit tests
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 |
|
Claude finished @ymc9's task —— View job Code Review Complete ✅I've analyzed the changes in this PR which fixes a relation field resolution issue in the policy expression transformer. Todo List:
SummaryThe fix addresses a bug in Code Quality & Best Practices ✅
Bug Fix Analysis ✅Root Cause: When comparing relation fields (e.g., Before: const idFields = requireIdFields(this.schema, context.model); // Wrong: uses current modelAfter: const leftRelDef = this.getFieldDefFromFieldRef(expr.left, context.model);
const idFields = requireIdFields(this.schema, leftRelDef.type); // Correct: uses relation target modelImpact: This would have caused incorrect SQL generation when relation fields are used in policy expressions, potentially leading to runtime errors or incorrect policy evaluation. Performance Considerations ✅
Security Concerns ✅
Test Coverage 📝Existing Coverage: The auth-equality tests in Recommendation: Consider adding a specific regression test that covers the exact scenario this fixes - comparing relation fields directly (e.g., Technical DetailsThe fix properly handles both directions of relation field comparisons:
The |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pull Request Overview
This PR fixes a relation field resolution issue in the policy expression transformer. The fix addresses incorrect ID field resolution when handling relation fields in expressions.
- Adds proper field definition lookup for relation fields before getting ID fields
- Replaces context.model with the actual relation field type for ID field resolution
Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 0
🧹 Nitpick comments (1)
packages/runtime/src/plugins/policy/expression-transformer.ts (1)
236-239: Mirror the left-side change; unify via helperThe right-side normalization mirrors the left. Refactor to the same helper to avoid drift.
- const rightRelDef = this.getFieldDefFromFieldRef(expr.right, context.model); - invariant(rightRelDef, 'failed to get relation field definition'); - const idFields = requireIdFields(this.schema, rightRelDef.type); - normalizedRight = this.makeOrAppendMember(normalizedRight, idFields[0]!); + const { relatedIdField } = this.requireRelatedIdFromRef(expr.right, context.model); + normalizedRight = this.makeOrAppendMember(normalizedRight, relatedIdField);
📜 Review details
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (1)
packages/runtime/src/plugins/policy/expression-transformer.ts(1 hunks)
🧰 Additional context used
📓 Path-based instructions (1)
{packages,samples,tests}/**
📄 CodeRabbit inference engine (CLAUDE.md)
Place packages only under
packages/,samples/, ortests/
Files:
packages/runtime/src/plugins/policy/expression-transformer.ts
🧬 Code graph analysis (1)
packages/runtime/src/plugins/policy/expression-transformer.ts (2)
packages/common-helpers/src/tiny-invariant.ts (1)
invariant(4-14)packages/runtime/src/client/query-utils.ts (1)
requireIdFields(63-70)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (3)
- GitHub Check: build-test (20.x, postgresql)
- GitHub Check: build-test (20.x, sqlite)
- GitHub Check: claude-review
🔇 Additional comments (1)
packages/runtime/src/plugins/policy/expression-transformer.ts (1)
228-231: Use related model IDs; add helper and verify null-checksReplace the current extraction with a small helper that returns the related id field (diff below).
- const leftRelDef = this.getFieldDefFromFieldRef(expr.left, context.model); - invariant(leftRelDef, 'failed to get relation field definition'); - const idFields = requireIdFields(this.schema, leftRelDef.type); - normalizedLeft = this.makeOrAppendMember(normalizedLeft, idFields[0]!); + const { relatedIdField } = this.requireRelatedIdFromRef(expr.left, context.model); + normalizedLeft = this.makeOrAppendMember(normalizedLeft, relatedIdField);Add helper near getFieldDefFromFieldRef():
+ private requireRelatedIdFromRef(expr: Expression, model: GetModels<Schema>) { + const relDef = this.getFieldDefFromFieldRef(expr, model); + invariant(relDef?.relation, `failed to resolve relation field: ${JSON.stringify(expr)}`); + const idFields = requireIdFields(this.schema, relDef.type); + return { relatedType: relDef.type, relatedIdField: idFields[0]! }; + }Ran a repository grep for null comparisons; multiple matches exist (e.g. packages/runtime/src/plugins/policy/expression-transformer.ts:208–212, packages/runtime/src/client/crud/dialects/base-dialect.ts, packages/runtime/src/client/crud/operations/base.ts, client/result-processor.ts, and tests). Manually confirm null-comparisons against relation fields are only used for to-one relations to avoid multi-row scalar subquery errors.
Summary by CodeRabbit