Skip to content

Fix const in anyOf/oneOf to generate Literal type#2864

Merged
koxudaxi merged 2 commits intomainfrom
fix/const-literal-in-anyof
Dec 30, 2025
Merged

Fix const in anyOf/oneOf to generate Literal type#2864
koxudaxi merged 2 commits intomainfrom
fix/const-literal-in-anyof

Conversation

@koxudaxi
Copy link
Copy Markdown
Owner

Fixes: #2085

@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented Dec 30, 2025

Warning

Rate limit exceeded

@koxudaxi has exceeded the limit for the number of commits that can be reviewed per hour. Please wait 2 minutes and 28 seconds before requesting another review.

⌛ How to resolve this issue?

After the wait time has elapsed, a review can be triggered using the @coderabbitai review command as a PR comment. Alternatively, push new commits to this PR.

We recommend that you space out your commits to avoid hitting the rate limit.

🚦 How do rate limits work?

CodeRabbit enforces hourly rate limits for each developer per organization.

Our paid plans have higher rate limits than the trial, open-source and free plans. In all cases, we re-allow further reviews after a brief timeout.

Please see our FAQ for further information.

📥 Commits

Reviewing files that changed from the base of the PR and between 602c45a and 5f78c55.

⛔ Files ignored due to path filters (1)
  • tests/data/jsonschema/anyof_const_with_constraints.json is excluded by !tests/data/**/*.json and included by none
📒 Files selected for processing (12)
  • src/datamodel_code_generator/parser/jsonschema.py
  • tests/data/expected/main/jsonschema/all_of_any_of_base_class_ref.py
  • tests/data/expected/main/jsonschema/anyof_const_with_constraints.py
  • tests/data/expected/main/jsonschema/discriminator_with_external_references_folder/inner_folder/artificial_folder/type_1.py
  • tests/data/expected/main/jsonschema/discriminator_with_external_references_folder/inner_folder/schema.py
  • tests/data/expected/main/jsonschema/discriminator_with_external_references_folder/inner_folder/type_2.py
  • tests/data/expected/main/jsonschema/discriminator_with_external_references_folder/subfolder/type_5.py
  • tests/data/expected/main/jsonschema/discriminator_with_external_references_folder/type_4.py
  • tests/data/expected/main/jsonschema/oneof_const_mixed_with_ref.py
  • tests/data/expected/main/jsonschema/oneof_const_with_properties.py
  • tests/data/expected/main/openapi/const.py
  • tests/data/expected/main/openapi/const_field.py

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.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented Dec 30, 2025

📚 Docs Preview: https://pr-2864.datamodel-code-generator.pages.dev

@codspeed-hq
Copy link
Copy Markdown

codspeed-hq Bot commented Dec 30, 2025

CodSpeed Performance Report

Merging #2864 will not alter performance

Comparing fix/const-literal-in-anyof (5f78c55) with main (602c45a)

⚠️ Unknown Walltime execution environment detected

Using the Walltime instrument on standard Hosted Runners will lead to inconsistent data.

For the most accurate results, we recommend using CodSpeed Macro Runners: bare-metal machines fine-tuned for performance measurement consistency.

Summary

✅ 11 untouched
⏩ 98 skipped1

Footnotes

  1. 98 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports.

@codecov
Copy link
Copy Markdown

codecov Bot commented Dec 30, 2025

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 99.38%. Comparing base (602c45a) to head (5f78c55).
⚠️ Report is 1 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #2864   +/-   ##
=======================================
  Coverage   99.38%   99.38%           
=======================================
  Files          91       91           
  Lines       15555    15555           
  Branches     1827     1827           
=======================================
  Hits        15460    15460           
  Misses         50       50           
  Partials       45       45           
Flag Coverage Δ
unittests 99.38% <100.00%> (ø)

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@koxudaxi koxudaxi merged commit 6dd1dbe into main Dec 30, 2025
38 checks passed
@koxudaxi koxudaxi deleted the fix/const-literal-in-anyof branch December 30, 2025 12:42
@github-actions
Copy link
Copy Markdown
Contributor

Breaking Change Analysis

Result: Breaking changes detected

Reasoning: This PR changes the generated code output for schemas with const values in anyOf/oneOf constructs. Previously, const values generated inferred base types (str, int, bool) but now they generate Literal types with the exact const value. This is visible in multiple test file changes where types like str became Literal["value"]. While this is technically a bug fix that improves type safety, it changes the generated code output which existing users may depend on, making it a breaking change for code generation.

Content for Release Notes

Code Generation Changes

  • Fields with const values in anyOf/oneOf now generate Literal types instead of inferred base types - Previously, a const value like "MODE_2D" in an anyOf/oneOf schema would generate str type. Now it generates Literal["MODE_2D"]. This change affects type hints in generated models and may require updates to code that type-checks against the generated output. For example:
    # Before (v0.x)
    map_view_mode: str = Field("MODE_2D", alias="mapViewMode", const=True)
    apiVersion: str = Field('v1', const=True)
    
    # After (this PR)
    map_view_mode: Literal["MODE_2D"] = Field("MODE_2D", alias="mapViewMode", const=True)
    apiVersion: Literal['v1'] = Field('v1', const=True)
    This is a bug fix that makes the generated code more type-safe, but downstream code performing type comparisons or using isinstance(field, str) checks may need adjustment. (Fix const in anyOf/oneOf to generate Literal type #2864)

This analysis was performed by Claude Code Action

@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented Jan 1, 2026

🎉 Released in 0.51.0

This PR is now available in the latest release. See the release notes for details.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Using const inside anyOf combined with minimum and maximum not working

1 participant