feat(core): resolveDiscriminators in cycle - fix - #3523
Conversation
📝 WalkthroughWalkthroughThe PR broadens resolveDiscriminators to handle discriminator parents that use ChangesDiscriminator Resolution Without Mapping
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Possibly related PRs
Suggested labels
Suggested reviewers
Poem
🚥 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: 1
🤖 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/core/src/getters/discriminators.ts`:
- Around line 124-131: The current logic that builds variantRefs only uses
mapping when present, which omits implicit parentSchema.oneOf refs not covered
by discriminator.mapping; update the construction of variantRefs in
discriminators.ts to combine both sources: take Object.values(mapping) (if
mapping exists) and also include any refs from parentSchema.oneOf that are
reference items (isReference(...) && typeof $ref === 'string') but not already
present in the mapping values, then deduplicate the result so all oneOf variants
(mapped and unmapped) get rewritten; refer to the existing symbols variantRefs,
mapping, parentSchema.oneOf and isReference when locating and changing the code.
🪄 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: Organization UI
Review profile: CHILL
Plan: Pro
Run ID: d9f96f34-297c-484f-8a42-fda499fcedda
📒 Files selected for processing (2)
packages/core/src/getters/discriminators.test.tspackages/core/src/getters/discriminators.ts
412a704 to
fa57d15
Compare
|
thanks @melloware 👍 |
Problem
OpenAPI specifications defining polymorphic interfaces permits a
discriminatorwith apropertyNameand aoneOf/anyOfarray, but omit the explicitmappingdictionary.Result of the bad code
Orval's
resolveDiscriminatorscycle-breaking logic was strictly guarded by a check fordiscriminator.mapping. When the mapping was missing, Orval skipped inlining the parent schema's properties into the variants.This resulted in the parent type being generated as a union of variants (
type Parent = VariantA | VariantB), while variants were generated as intersections extending the parent (type VariantA = Parent & { ... }).This circular dependency caused TypeScript compiler error TS2456: Type alias circularly references itself.
Example OpenAPI Spec causing the issue
How we fixed it
Relaxed the guard condition in
packages/core/src/getters/discriminators.tsto check for the presence ofdiscriminatorandoneOf/anyOf, rather than strictly requiringdiscriminator.mapping. Whenmappingis absent, the logic now dynamically extracts the variant$refstrings directly from theoneOf/anyOfarray to identify which schemas need the parent's properties inlined.Summary by CodeRabbit
Bug Fixes
Tests