implement delegateMap attribute#2676
Conversation
📝 WalkthroughWalkthroughThis PR implements the ChangesDelegate Map Feature
🎯 4 (Complex) | ⏱️ ~60 minutes
🚥 Pre-merge checks | ✅ 4 | ❌ 1❌ Failed checks (1 warning)
✅ Passed checks (4 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.
Actionable comments posted: 2
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@packages/language/res/stdlib.zmodel`:
- Line 649: The @@delegateMap attribute must be marked single-use to prevent
multiple mappings being accepted but only one being resolved; update the
attribute declaration for @@delegateMap to include the single-use modifier
(e.g., add @@@once or the equivalent single-use flag) so the parser enforces
only one @@delegateMap per model and rejects additional entries, ensuring unique
mapping resolution for the model.
In `@packages/orm/src/client/query-utils.ts`:
- Around line 384-387: The fallback for the discriminator currently returns the
raw input parameter `model`, which can preserve incorrect casing; update
getDelegateDiscriminatorValue to use the canonical model name from the resolved
model definition instead of the input string when delegateMap is undefined: call
requireModel(schema, model) as already done, then return modelDef.delegateMap ??
modelDef.name (or the canonical name field on the modelDef) so the schema's
canonical casing is used; keep delegateMap precedence and only use the modelDef
canonical name as the default.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Pro
Run ID: f8f43335-fb2a-4f23-876c-e4a882be0f16
📒 Files selected for processing (12)
packages/language/res/stdlib.zmodelpackages/language/src/validators/datamodel-validator.tspackages/language/test/delegate.test.tspackages/orm/src/client/crud-types.tspackages/orm/src/client/crud/operations/base.tspackages/orm/src/client/query-utils.tspackages/schema/src/schema.tspackages/sdk/src/ts-schema-generator.tstests/e2e/orm/client-api/delegate.test.tstests/e2e/orm/schemas/delegate/schema.tstests/e2e/orm/schemas/delegate/schema.zmodeltests/e2e/orm/schemas/delegate/typecheck.ts
| * | ||
| * @param value: A string literal or enum member used as the discriminator. | ||
| */ | ||
| attribute @@delegateMap(_ value: Any) |
There was a problem hiding this comment.
Make @@delegateMap single-use to avoid ambiguous mappings.
Without @@@once, multiple @@delegateMap entries on one model are accepted, while downstream resolution reads only one and silently ignores others.
Proposed fix
-attribute @@delegateMap(_ value: Any)
+attribute @@delegateMap(_ value: Any) @@@once📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
| attribute @@delegateMap(_ value: Any) | |
| attribute @@delegateMap(_ value: Any) @@@once |
🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
In `@packages/language/res/stdlib.zmodel` at line 649, The @@delegateMap attribute
must be marked single-use to prevent multiple mappings being accepted but only
one being resolved; update the attribute declaration for @@delegateMap to
include the single-use modifier (e.g., add @@@once or the equivalent single-use
flag) so the parser enforces only one @@delegateMap per model and rejects
additional entries, ensuring unique mapping resolution for the model.
| export function getDelegateDiscriminatorValue(schema: SchemaDef, model: string) { | ||
| const modelDef = requireModel(schema, model); | ||
| return modelDef.delegateMap ?? model; | ||
| } |
There was a problem hiding this comment.
Use canonical model name for default discriminator fallback.
Line 386 falls back to the input model string. Because lookup is case-insensitive, passing "video" can produce "video" instead of schema model name "Video" when delegateMap is not set.
Proposed fix
export function getDelegateDiscriminatorValue(schema: SchemaDef, model: string) {
const modelDef = requireModel(schema, model);
- return modelDef.delegateMap ?? model;
+ return modelDef.delegateMap ?? modelDef.name;
}🤖 Prompt for AI Agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
In `@packages/orm/src/client/query-utils.ts` around lines 384 - 387, The fallback
for the discriminator currently returns the raw input parameter `model`, which
can preserve incorrect casing; update getDelegateDiscriminatorValue to use the
canonical model name from the resolved model definition instead of the input
string when delegateMap is undefined: call requireModel(schema, model) as
already done, then return modelDef.delegateMap ?? modelDef.name (or the
canonical name field on the modelDef) so the schema's canonical casing is used;
keep delegateMap precedence and only use the modelDef canonical name as the
default.
This PR implement custom delegate mapping as per option 3 described in #2558. This is my first "larger" PR to this project, so please let me know if there is anything that could be improved.
closes #2558
Summary by CodeRabbit
Release Notes
New Features
@@delegateMapattribute to enable mapping delegate sub-models to specific discriminator valuesImprovements