Skip to content

Commit 69c51fd

Browse files
authored
Add deprecated field support for Pydantic v2 (#2915)
1 parent b5a361d commit 69c51fd

File tree

5 files changed

+52
-0
lines changed

5 files changed

+52
-0
lines changed

src/datamodel_code_generator/model/pydantic_v2/base_model.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,7 @@ class DataModelField(DataModelFieldV1):
120120
"min_length",
121121
"max_length",
122122
"union_mode",
123+
"deprecated",
123124
}
124125
constraints: Optional[Constraints] = None # pyright: ignore[reportIncompatibleVariableOverride] # noqa: UP045
125126
_PARSE_METHOD: ClassVar[str] = "model_validate"

src/datamodel_code_generator/parser/jsonschema.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -530,6 +530,7 @@ def _get_type(type_: str, format__: str | None = None) -> Types:
530530
"title",
531531
"const",
532532
"default_factory",
533+
"deprecated",
533534
}
534535

535536
EXCLUDE_FIELD_KEYS_IN_JSON_SCHEMA: set[str] = {
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# generated by datamodel-codegen:
2+
# filename: deprecated_field.yaml
3+
# timestamp: 2019-07-26T00:00:00+00:00
4+
5+
from __future__ import annotations
6+
7+
from pydantic import BaseModel, Field
8+
9+
10+
class Service(BaseModel):
11+
name: str = Field(..., description='Name of the service')
12+
location: str | None = Field(
13+
None, deprecated=True, description='Location of the service (deprecated)'
14+
)
15+
old_id: int | None = Field(None, deprecated=True)
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
openapi: "3.0.0"
2+
info:
3+
version: 1.0.0
4+
title: Deprecated Field Test
5+
paths: {}
6+
components:
7+
schemas:
8+
Service:
9+
type: object
10+
properties:
11+
name:
12+
type: string
13+
description: Name of the service
14+
location:
15+
type: string
16+
description: Location of the service (deprecated)
17+
deprecated: true
18+
old_id:
19+
type: integer
20+
deprecated: true
21+
required:
22+
- name

tests/main/openapi/test_main_openapi.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4856,3 +4856,16 @@ def test_main_openapi_include_paths_warning_without_paths_scope() -> None:
48564856
assert any(
48574857
"--openapi-include-paths has no effect without --openapi-scopes paths" in msg for msg in warning_messages
48584858
)
4859+
4860+
4861+
@SKIP_PYDANTIC_V1
4862+
def test_main_openapi_deprecated_field(output_file: Path) -> None:
4863+
"""Test OpenAPI generation with deprecated field property."""
4864+
run_main_and_assert(
4865+
input_path=OPEN_API_DATA_PATH / "deprecated_field.yaml",
4866+
output_path=output_file,
4867+
input_file_type="openapi",
4868+
assert_func=assert_file_content,
4869+
expected_file="deprecated_field.py",
4870+
extra_args=["--output-model-type", "pydantic_v2.BaseModel"],
4871+
)

0 commit comments

Comments
 (0)