django-ninja icon indicating copy to clipboard operation
django-ninja copied to clipboard

[BUG] Optional Enum labelled as required in OpenAPI Schema

Open OtherBarry opened this issue 3 years ago • 2 comments

Problem When using an Enum (or django Choices) class as a path, query, or form value, it is marked as required in the OpenAPI docs, despite being typed with Optional or | None, and having None set as the default.

The endpoint itself works as it should though, if the enum is not present the value is None.

Example Code

class SomeChoice(str, Enum):
    FOO = "foo"
    BAR = "bar"


@api.get("/example")
def example(
    request: HttpRequest,
    choice: Optional[SomeChoice] = Query(None, description="Some Choice"),
) -> str:
    return choice or "none"

Versions Python 3.10.4 django 3.2.14 django-ninja 0.19.1

OtherBarry avatar Aug 17 '22 06:08 OtherBarry

Hi @OtherBarry

Yeah, that's a weird one - thank you for catching :)

from my tests it looks like description does some influence for some reason - so for now the workaround is do not use description:

@api.get("/example")
def example(
    request,
    choice: Optional[SomeChoice] = Query(None),
) -> str:
    return choice or "none"
CleanShot 2022-08-17 at 15 59 37@2x

vitalik avatar Aug 17 '22 12:08 vitalik

Thanks @vitalik, dropping the description is a good workaround.

I can take a crack at fixing it if needed, but might need to be pointed in the right direction.

OtherBarry avatar Aug 18 '22 02:08 OtherBarry