django-ninja
django-ninja copied to clipboard
[BUG] Optional Enum labelled as required in OpenAPI Schema
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
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"
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.