-
-
Notifications
You must be signed in to change notification settings - Fork 426
Add deprecated field support for Pydantic v2 #2915
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
📝 WalkthroughWalkthroughThe changes add support for the "deprecated" field attribute in generated Pydantic v2 models. The "deprecated" key is added to default field keys in both the Pydantic v2 model handler and JSON Schema parser, enabling the generator to recognize and propagate deprecated attributes from OpenAPI specifications. A test fixture and test case validate this functionality. Changes
Estimated code review effort🎯 2 (Simple) | ⏱️ ~8 minutes Poem
Pre-merge checks and finishing touches✅ Passed checks (5 passed)
✨ Finishing touches
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 |
|
📚 Docs Preview: https://pr-2915.datamodel-code-generator.pages.dev |
CodSpeed Performance ReportMerging #2915 will not alter performanceComparing
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 1
📜 Review details
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Pro
⛔ Files ignored due to path filters (1)
tests/data/openapi/deprecated_field.yamlis excluded by!tests/data/**/*.yamland included by none
📒 Files selected for processing (4)
src/datamodel_code_generator/model/pydantic_v2/base_model.pysrc/datamodel_code_generator/parser/jsonschema.pytests/data/expected/main/openapi/deprecated_field.pytests/main/openapi/test_main_openapi.py
🧰 Additional context used
🧠 Learnings (2)
📓 Common learnings
Learnt from: koxudaxi
Repo: koxudaxi/datamodel-code-generator PR: 2890
File: tests/data/expected/main/jsonschema/ref_nullable_with_constraint.py:14-15
Timestamp: 2026-01-02T08:25:19.839Z
Learning: The datamodel-code-generator currently generates RootModel subclasses with an explicit `root` field annotation (e.g., `class StringType(RootModel[str]): root: str`). This is existing behavior of the code generator and should not be flagged as an issue introduced by new changes.
📚 Learning: 2026-01-02T08:25:19.839Z
Learnt from: koxudaxi
Repo: koxudaxi/datamodel-code-generator PR: 2890
File: tests/data/expected/main/jsonschema/ref_nullable_with_constraint.py:14-15
Timestamp: 2026-01-02T08:25:19.839Z
Learning: The datamodel-code-generator currently generates RootModel subclasses with an explicit `root` field annotation (e.g., `class StringType(RootModel[str]): root: str`). This is existing behavior of the code generator and should not be flagged as an issue introduced by new changes.
Applied to files:
src/datamodel_code_generator/model/pydantic_v2/base_model.py
🧬 Code graph analysis (1)
tests/data/expected/main/openapi/deprecated_field.py (3)
src/datamodel_code_generator/model/pydantic_v2/base_model.py (1)
BaseModel(214-448)tests/data/expected/parser/openapi/openapi_parser_parse_modular/bar.py (1)
Field(6-7)src/datamodel_code_generator/model/base.py (1)
name(828-830)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (10)
- GitHub Check: benchmarks
- GitHub Check: 3.10 on Windows
- GitHub Check: 3.12 on macOS
- GitHub Check: 3.13 on Windows
- GitHub Check: 3.10 on macOS
- GitHub Check: 3.12 on Windows
- GitHub Check: 3.14 on Windows
- GitHub Check: 3.11 on Windows
- GitHub Check: 3.14 on macOS
- GitHub Check: Analyze (python)
🔇 Additional comments (3)
src/datamodel_code_generator/parser/jsonschema.py (1)
533-533: LGTM! Enables deprecated metadata extraction.Adding "deprecated" to
DEFAULT_FIELD_KEYScorrectly enables the parser to extract the deprecated flag from OpenAPI/JSON Schema specifications and pass it to generated Pydantic field definitions. This change works in conjunction with the corresponding update inbase_model.py.tests/main/openapi/test_main_openapi.py (1)
4859-4871: LGTM! Well-structured test for deprecated field support.The test is properly gated for Pydantic v2 with the
@SKIP_PYDANTIC_V1decorator, which is correct since thedeprecatedparameter inField()is a Pydantic v2 feature. The test follows the established pattern in the file and will validate that the deprecated field attribute is correctly propagated from OpenAPI specs to generated Pydantic models.tests/data/expected/main/openapi/deprecated_field.py (1)
10-15: LGTM! Correct Pydantic v2 deprecated field syntax.The Service model correctly demonstrates the expected output when processing deprecated fields from OpenAPI specifications:
- The
deprecated=Trueparameter is properly included inField()calls for deprecated fields- The mix of required vs. optional and deprecated vs. non-deprecated fields provides good test coverage
- Field descriptions are preserved alongside the deprecated flag
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #2915 +/- ##
=========================================
Coverage 100.00% 100.00%
=========================================
Files 93 93
Lines 16841 16844 +3
Branches 1951 1951
=========================================
+ Hits 16841 16844 +3
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:
|
Breaking Change AnalysisResult: No breaking changes detected Reasoning: This PR adds support for the This analysis was performed by Claude Code Action |
|
🎉 Released in 0.52.1 This PR is now available in the latest release. See the release notes for details. |
Fixes: #2476
Summary by CodeRabbit
deprecatedattribute in Pydantic models.✏️ Tip: You can customize this high-level summary in your review settings.