Skip to content
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

fix(pydantic v1): field not optional if default value #3476

Merged
merged 1 commit into from
May 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion litestar/contrib/pydantic/pydantic_schema_plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,8 @@ def for_pydantic_model(cls, field_definition: FieldDefinition, schema_creator: S
else:
# pydantic v1 requires some workarounds here
model_annotations = {
k: f.outer_type_ if f.required else Optional[f.outer_type_] for k, f in model.__fields__.items()
k: f.outer_type_ if f.required or f.default else Optional[f.outer_type_]
for k, f in model.__fields__.items()
}

if is_generic_model:
Expand Down
80 changes: 79 additions & 1 deletion tests/e2e/test_pydantic.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import pydantic
from pydantic import v1 as pydantic_v1

from litestar import get
from litestar import get, post
from litestar.testing import create_test_client


Expand All @@ -22,3 +23,80 @@ def handler_v2() -> ModelV2:
with create_test_client([handler_v1, handler_v2]) as client:
assert client.get("/v1").json() == {"foo": "bar"}
assert client.get("/v2").json() == {"foo": "bar"}


def test_pydantic_v1_model_with_field_default() -> None:
# https://github.com/litestar-org/litestar/issues/3471

class TestDto(pydantic_v1.BaseModel):
test_str: str = pydantic_v1.Field(default="some_default", max_length=100)

@post(path="/test")
async def test(data: TestDto) -> str:
return "success"

with create_test_client(route_handlers=[test]) as client:
response = client.get("/schema/openapi.json")
assert response.status_code == 200
assert response.json() == {
"components": {
"schemas": {
"test_pydantic_v1_model_with_field_default.TestDto": {
"properties": {"test_str": {"default": "some_default", "maxLength": 100, "type": "string"}},
"required": [],
"title": "TestDto",
"type": "object",
}
}
},
"info": {"title": "Litestar API", "version": "1.0.0"},
"openapi": "3.1.0",
"paths": {
"/test": {
"post": {
"deprecated": False,
"operationId": "TestTest",
"requestBody": {
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/test_pydantic_v1_model_with_field_default.TestDto"
}
}
},
"required": True,
},
"responses": {
"201": {
"content": {"text/plain": {"schema": {"type": "string"}}},
"description": "Document " "created, " "URL " "follows",
"headers": {},
},
"400": {
"content": {
"application/json": {
"schema": {
"description": "Validation " "Exception",
"examples": [{"detail": "Bad " "Request", "extra": {}, "status_code": 400}],
"properties": {
"detail": {"type": "string"},
"extra": {
"additionalProperties": {},
"type": ["null", "object", "array"],
},
"status_code": {"type": "integer"},
},
"required": ["detail", "status_code"],
"type": "object",
}
}
},
"description": "Bad " "request " "syntax or " "unsupported " "method",
},
},
"summary": "Test",
}
}
},
"servers": [{"url": "/"}],
}
Loading