diff --git a/drf_spectacular/contrib/pydantic.py b/drf_spectacular/contrib/pydantic.py index 2430136f..f03dda6a 100644 --- a/drf_spectacular/contrib/pydantic.py +++ b/drf_spectacular/contrib/pydantic.py @@ -34,7 +34,7 @@ def map_serializer(self, auto_schema, direction): warn("Only pydantic >= 2 is supported. defaulting to generic object.") return build_basic_type(OpenApiTypes.OBJECT) - schema = model_json_schema(self.target, ref_template="#/components/schemas/{model}") + schema = model_json_schema(self.target, ref_template="#/components/schemas/{model}", mode="serialization") # pull out potential sub-schemas and put them into component section for sub_name, sub_schema in schema.pop("$defs", {}).items(): diff --git a/tests/contrib/test_pydantic.py b/tests/contrib/test_pydantic.py index 11672fab..337d4ca4 100644 --- a/tests/contrib/test_pydantic.py +++ b/tests/contrib/test_pydantic.py @@ -1,4 +1,5 @@ import sys +from decimal import Decimal from typing import List import pytest @@ -31,6 +32,7 @@ class B(BaseModel): class A(BaseModel): id: int b: B + d: Decimal @pytest.mark.contrib('pydantic') diff --git a/tests/contrib/test_pydantic.yml b/tests/contrib/test_pydantic.yml index ac62fdc2..0680074a 100644 --- a/tests/contrib/test_pydantic.yml +++ b/tests/contrib/test_pydantic.yml @@ -40,9 +40,13 @@ components: type: integer b: $ref: '#/components/schemas/B' + d: + title: D + type: string required: - id - b + - d title: A type: object B: