Skip to content

Bug: OpenAPI Schema generator incorrect type for fields with default_factory #4294

@raidzin

Description

@raidzin

Description

OpenAPI schema generator create incorrect type for fields with default_factory.
In MCVE Foo.foo type is str, but in generated openapi (string | null), which is incorrect

URL to code causing the issue

No response

MCVE

from litestar import Litestar, get, post
from pydantic import BaseModel, Field


def foo_factory() -> str:
    return 'foo'


class Foo(BaseModel):
    foo: str = Field(default_factory=foo_factory)
    bar: str = Field(default='bar')
    spam: str


@post('/')
def create_foo(data: Foo) -> None:
    pass


@get('/')
def foo() -> Foo:
    return Foo(spam='spam')


app = Litestar(route_handlers=[create_foo, foo])

Steps to reproduce

 uv run --with "litestar[pydantic,standard]" uvicorn mcve:app

Screenshots

Image

Litestar Version

2.17.0

Platform

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Bug 🐛This is something that is not working as expected

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions