Fix title ignored when creating enums from merging allOf's or anyOf's objects#2975
Conversation
…eaad of its alias
…an be inferred and used as name
📝 WalkthroughWalkthroughThe parser stops adding a generated varname when a title is absent during const-based oneOf/anyOf extraction, and synthetic enum objects now set Changes
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Suggested labels
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 |
Merging this PR will not alter performance
|
There was a problem hiding this comment.
Actionable comments posted: 0
Caution
Some comments are outside the diff and can’t be posted inline due to platform limitations.
⚠️ Outside diff range comments (1)
src/datamodel_code_generator/parser/jsonschema.py (1)
855-868:⚠️ Potential issue | 🟠 MajorMixed title/no-title enums can lose all titles.
Line 855-868: if any item lacks a title and
inferred_typeis set,varnamesbecomes shorter thanenum_values, so all titles are dropped. This contradicts “use title when available” for mixed cases.✅ Suggested fix (preserve titles when present, keep legacy behavior when none)
@@ - inferred_type: str | None = None + inferred_type: str | None = None + has_title = False @@ - if item.title: - varnames.append(item.title) - elif inferred_type is None and const_value is not None: - match const_value: - case str(): - inferred_type = "string" - case bool(): # bool must come before int (bool is subclass of int) - inferred_type = "boolean" - case int(): - inferred_type = "integer" - case float(): - inferred_type = "number" - else: - varnames.append(str(const_value)) + if item.title: + has_title = True + varnames.append(item.title) + else: + if inferred_type is None and const_value is not None: + match const_value: + case str(): + inferred_type = "string" + case bool(): # bool must come before int (bool is subclass of int) + inferred_type = "boolean" + case int(): + inferred_type = "integer" + case float(): + inferred_type = "number" + varnames.append(str(const_value)) @@ - if not enum_values: # pragma: no cover + if not enum_values: # pragma: no cover return None + if not has_title and inferred_type is not None: + varnames = []
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #2975 +/- ##
=========================================
Coverage 100.00% 100.00%
=========================================
Files 94 94
Lines 17781 17780 -1
Branches 2044 2044
=========================================
- Hits 17781 17780 -1
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
There was a problem hiding this comment.
Pull request overview
Fixes enum member naming for enums synthesized from oneOf/anyOf const-combinations by correctly propagating x-enum-varnames into the synthetic JsonSchemaObject, allowing per-item title values to be used for enum member names.
Changes:
- Update synthetic enum schema creation to populate
x-enum-varnamesvia its alias so Pydantic recognizes it. - Adjust const-enum extraction logic around how varnames and inferred enum type are derived.
- Update expected JSON Schema output for an integer const
oneOfenum to use titles as member names.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 4 comments.
| File | Description |
|---|---|
| tests/data/expected/main/jsonschema/oneof_const_enum_int.py | Updates expected enum member names to reflect per-item title usage. |
| src/datamodel_code_generator/parser/jsonschema.py | Fixes passing x-enum-varnames into synthetic enum schema objects and tweaks const-enum extraction behavior. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Breaking Change AnalysisResult: Breaking changes detected Reasoning: This PR fixes a bug where the Content for Release NotesCode Generation Changes
This analysis was performed by Claude Code Action |
|
🎉 Released in 0.54.0 This PR is now available in the latest release. See the release notes for details. |
Fixes: #2974
The issue:
x_enum_varnameswas passed toJsonSchemaObjectusing the variable name instead of its alias.The consequence:
x_enum_varnameswas not correctly recognized, and therefore ignoredThe solution:
x_enum_varnamesis now passed asx-enum-varnames(its alias) and it's no longer ignoredThe fix above unveiled an incorrect behavior when no title is set but type can be inferred and used as title. A fix has been applied about that.
Summary by CodeRabbit
✏️ Tip: You can customize this high-level summary in your review settings.