Using PydanticDTO
with a datetime
field and default_factory
does not work anymore since #3721.
The get
request returns Bad Request
No response
from datetime import datetime
import pydantic as pydantic_v2
from litestar import post
from litestar.contrib.pydantic.pydantic_dto_factory import PydanticDTO
from litestar.testing import create_test_client
def test_pydanticdto_with_default_factory() -> None:
_now = datetime.fromisoformat("2024-01-01T12:42:42")
class User(pydantic_v2.BaseModel):
timestamp: datetime = pydantic_v2.Field(default_factory=lambda: _now)
@post("/", dto=PydanticDTO[User])
async def create_user(data: User) -> User:
return data
with create_test_client(create_user) as client:
response = client.post("/", json={})
assert response.status_code == 201
assert User.model_validate(response.json()) == User.model_validate({})
test_pydanticdto_with_default_factory()
Execute the code above.
2.12.0
Pay now to fund the work behind this issue.
Get updates on progress being made.
Maintainer is rewarded once the issue is completed.
You're funding impactful open source efforts
You want to contribute to this effort
You want to get funding like this too