Skip to content

Conversation

@koxudaxi
Copy link
Owner

@koxudaxi koxudaxi commented Jan 3, 2026

Fixes: #2476

Summary by CodeRabbit

  • New Features
    • Added support for deprecated fields in OpenAPI schema generation. Fields marked as deprecated in API specifications are now properly generated with the deprecated attribute in Pydantic models.

✏️ Tip: You can customize this high-level summary in your review settings.

@coderabbitai
Copy link

coderabbitai bot commented Jan 3, 2026

📝 Walkthrough

Walkthrough

The 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

Cohort / File(s) Summary
Pydantic v2 Model Support
src/datamodel_code_generator/model/pydantic_v2/base_model.py
Added "deprecated" to DataModelField._DEFAULT_FIELD_KEYS set, expanding recognized default field keys for Pydantic v2 BaseModel fields
JSON Schema Parser
src/datamodel_code_generator/parser/jsonschema.py
Added "deprecated" to DEFAULT_FIELD_KEYS constant, extending recognized metadata keys during JSON Schema parsing
Test Fixture & Test Case
tests/data/expected/main/openapi/deprecated_field.py, tests/main/openapi/test_main_openapi.py
Added test fixture file with Service model containing deprecated fields and new test function test_main_openapi_deprecated_field decorated with @SKIP_PYDANTIC_V1

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~8 minutes

Poem

🐰 A hop through deprecated fields we go,
Adding flags to let old features know,
Pydantic v2 now marks what's worn,
OpenAPI specs, gracefully reborn! ✨

Pre-merge checks and finishing touches

✅ Passed checks (5 passed)
Check name Status Explanation
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.
Title check ✅ Passed The title accurately reflects the main change: adding support for the deprecated field in Pydantic v2 models.
Linked Issues check ✅ Passed The PR fully addresses issue #2476 by adding deprecated field support to generated Pydantic models, including proper parsing and field generation for OpenAPI schemas.
Out of Scope Changes check ✅ Passed All changes directly support deprecated field handling: updating field key constants, adding test coverage, and generating expected output. No extraneous modifications detected.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.
✨ Finishing touches
  • 📝 Generate docstrings

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
Contributor

github-actions bot commented Jan 3, 2026

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

@codspeed-hq
Copy link

codspeed-hq bot commented Jan 3, 2026

CodSpeed Performance Report

Merging #2915 will not alter performance

Comparing feature/deprecated-field-support (895baa4) with main (b5a361d)

⚠️ 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.

Copy link

@coderabbitai coderabbitai bot left a 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

📥 Commits

Reviewing files that changed from the base of the PR and between b5a361d and 895baa4.

⛔ Files ignored due to path filters (1)
  • tests/data/openapi/deprecated_field.yaml is excluded by !tests/data/**/*.yaml and included by none
📒 Files selected for processing (4)
  • src/datamodel_code_generator/model/pydantic_v2/base_model.py
  • src/datamodel_code_generator/parser/jsonschema.py
  • tests/data/expected/main/openapi/deprecated_field.py
  • tests/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_KEYS correctly 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 in base_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_V1 decorator, which is correct since the deprecated parameter in Field() 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=True parameter is properly included in Field() 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
Copy link

codecov bot commented Jan 3, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 100.00%. Comparing base (b5a361d) to head (895baa4).
⚠️ Report is 1 commits behind head on main.

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     
Flag Coverage Δ
unittests 100.00% <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 69c51fd into main Jan 3, 2026
38 checks passed
@koxudaxi koxudaxi deleted the feature/deprecated-field-support branch January 3, 2026 17:38
@github-actions
Copy link
Contributor

github-actions bot commented Jan 3, 2026

Breaking Change Analysis

Result: No breaking changes detected

Reasoning: This PR adds support for the deprecated field property in Pydantic v2 models. It is a purely additive feature that adds deprecated=True to Field() calls when the input OpenAPI/JSON Schema has deprecated: true on a field. The change adds "deprecated" to DEFAULT_FIELD_KEYS in the JSON schema parser and _DEFAULT_FIELD_KEYS in the Pydantic v2 DataModelField class. No existing behavior is modified - existing schemas without deprecated fields will generate exactly the same output as before. No templates, CLI options, default values, or error handling were changed.


This analysis was performed by Claude Code Action

@github-actions
Copy link
Contributor

github-actions bot commented Jan 3, 2026

🎉 Released in 0.52.1

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.

Allow including deprecated into the pydantic model

2 participants