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

Bug: default_factory with PydanticDTO #3759

Open
2 of 4 tasks
FranzForstmayr opened this issue Sep 25, 2024 · 0 comments
Open
2 of 4 tasks

Bug: default_factory with PydanticDTO #3759

FranzForstmayr opened this issue Sep 25, 2024 · 0 comments
Labels
Bug 🐛 This is something that is not working as expected

Comments

@FranzForstmayr
Copy link

FranzForstmayr commented Sep 25, 2024

Description

Using PydanticDTO with a datetime field and default_factory does not work anymore since #3721.
The get request returns Bad Request

URL to code causing the issue

No response

MCVE

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()

Steps to reproduce

Execute the code above.

Litestar Version

2.12.0

Platform

  • Linux
  • Mac
  • Windows
  • Other (Please specify in the description above)

Note

While we are open for sponsoring on GitHub Sponsors and
OpenCollective, we also utilize Polar.sh to engage in pledge-based sponsorship.

Check out all issues funded or available for funding on our Polar.sh dashboard

  • If you would like to see an issue prioritized, make a pledge towards it!
  • We receive the pledge once the issue is completed & verified
  • This, along with engagement in the community, helps us know which features are a priority to our users.
Fund with Polar
@FranzForstmayr FranzForstmayr added the Bug 🐛 This is something that is not working as expected label Sep 25, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Bug 🐛 This is something that is not working as expected
Projects
None yet
Development

No branches or pull requests

1 participant