Skip to content

Fix Pydantic v2 discriminated unions in array fields#2907

Merged
koxudaxi merged 3 commits intomainfrom
fix/discriminator-in-list-pydantic-v2
Jan 2, 2026
Merged

Fix Pydantic v2 discriminated unions in array fields#2907
koxudaxi merged 3 commits intomainfrom
fix/discriminator-in-list-pydantic-v2

Conversation

@koxudaxi
Copy link
Copy Markdown
Owner

@koxudaxi koxudaxi commented Jan 2, 2026

Fixes: #2904

@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented Jan 2, 2026

Warning

Rate limit exceeded

@koxudaxi has exceeded the limit for the number of commits that can be reviewed per hour. Please wait 22 minutes and 9 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 3db6667 and ffcd38b.

⛔ Files ignored due to path filters (1)
  • tests/data/openapi/discriminator_in_array_underscore.yaml is excluded by !tests/data/**/*.yaml and included by none
📒 Files selected for processing (6)
  • src/datamodel_code_generator/model/pydantic_v2/base_model.py
  • src/datamodel_code_generator/parser/base.py
  • src/datamodel_code_generator/types.py
  • tests/data/expected/main/openapi/discriminator/in_array_collapse_root_models_pydantic_v2.py
  • tests/data/expected/main/openapi/discriminator/in_array_underscore_pydantic_v2.py
  • tests/main/openapi/test_main_openapi.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 Jan 2, 2026

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

@codspeed-hq
Copy link
Copy Markdown

codspeed-hq Bot commented Jan 2, 2026

CodSpeed Performance Report

Merging #2907 will not alter performance

Comparing fix/discriminator-in-list-pydantic-v2 (ffcd38b) with main (3db6667)

⚠️ 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 Jan 2, 2026

Codecov Report

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

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #2907   +/-   ##
=======================================
  Coverage   99.38%   99.38%           
=======================================
  Files          92       92           
  Lines       16374    16400   +26     
  Branches     1940     1947    +7     
=======================================
+ Hits        16273    16299   +26     
  Misses         52       52           
  Partials       49       49           
Flag Coverage Δ
unittests 99.38% <100.00%> (+<0.01%) ⬆️

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
Copy link
Copy Markdown
Owner Author

koxudaxi commented Jan 2, 2026

@coderabbitai review

@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented Jan 2, 2026

✅ Actions performed

Review triggered.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

@koxudaxi koxudaxi merged commit ccf8794 into main Jan 2, 2026
38 checks passed
@koxudaxi koxudaxi deleted the fix/discriminator-in-list-pydantic-v2 branch January 2, 2026 19:26
@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented Jan 2, 2026

Breaking Change Analysis

Result: No breaking changes detected

Reasoning: This PR is a bug fix for Pydantic v2 discriminated unions in array fields (#2904). The changes modify how discriminated unions are rendered when they appear inside array fields - now correctly generating Annotated[Union, Field(discriminator='...')] syntax. While the generated code output changes for this specific scenario, this is fixing incorrect behavior rather than breaking working functionality. Users who had schemas with discriminated unions in arrays were previously getting invalid or non-functional output; now they get correct Pydantic v2 discriminator syntax. The addition of a discriminator field to the DataType class is an additive change that doesn't affect existing custom templates unless they explicitly handle the new field. No CLI options, APIs, default behaviors, Python version requirements, or error handling patterns were changed.


This analysis was performed by Claude Code Action

@github-actions
Copy link
Copy Markdown
Contributor

github-actions Bot commented Jan 2, 2026

🎉 Released in 0.52.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.

Discriminator in array properties - wrong pydantic code generator

1 participant