Skip to content

Commit

Permalink
Merge pull request #1156 from codebutler/pydantic-mode
Browse files Browse the repository at this point in the history
fix: set pydantic json mode to serialization
  • Loading branch information
tfranzel authored Jul 1, 2024
2 parents 1dbb6c1 + 525475f commit cc91637
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 1 deletion.
2 changes: 1 addition & 1 deletion drf_spectacular/contrib/pydantic.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand Down
2 changes: 2 additions & 0 deletions tests/contrib/test_pydantic.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import sys
from decimal import Decimal
from typing import List

import pytest
Expand Down Expand Up @@ -31,6 +32,7 @@ class B(BaseModel):
class A(BaseModel):
id: int
b: B
d: Decimal


@pytest.mark.contrib('pydantic')
Expand Down
4 changes: 4 additions & 0 deletions tests/contrib/test_pydantic.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit cc91637

Please sign in to comment.