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

Figure out why optional function arguments become query parameters instead of body payload in ninja openapi schema #291

Open
tykling opened this issue Dec 23, 2024 · 1 comment
Assignees

Comments

@tykling
Copy link
Member

tykling commented Dec 23, 2024

thumbnail_data and thumbnail_metadata in the /files/upload/ api endpoint are optional and default to None. The type annotation should be UploadedFile | None but including None makes thumbnail_data and thumbnail_metadata become parameters instead of body payload in the openapi schema.

The workaround of not including None in the annotation shows thumbnail_data and thumbnail_metadata as part of request body as expected, but the annotation is wrong:

    thumbnail_data: File[UploadedFile] = None,
    thumbnail_metadata: Body[ImageMetadataSchema] = None,

Shows thumbnail_data and thumbnail_metadata as part of query parameters, but the annotation is correct:

    thumbnail_data: File[UploadedFile] | None = None,
    thumbnail_metadata: Body[ImageMetadataSchema] | None = None,

The buggy code is in https://github.com/vitalik/django-ninja/blob/be55f63368d6ece4701c356ebec890476691e7fd/ninja/signature/details.py#L218 so create a minimal reproducer and open a ninja issue

@tykling tykling self-assigned this Dec 23, 2024
@tykling tykling changed the title Figure out why optional function arguments become parameters instead of body payload in ninja Figure out why optional function arguments become query parameters instead of body payload in ninja Dec 23, 2024
@tykling tykling changed the title Figure out why optional function arguments become query parameters instead of body payload in ninja Figure out why optional function arguments become query parameters instead of body payload in ninja openapi schema Dec 23, 2024
@tykling
Copy link
Member Author

tykling commented Dec 23, 2024

Workaround makes mypy mad ofc:

src/files/api.py:75: error: Incompatible default for argument "thumbnail_data" (default has type "None", argument has type "UploadedFile")  [assignment]
src/files/api.py:75: note: PEP 484 prohibits implicit Optional. Accordingly, mypy has changed its default to no_implicit_optional=True
src/files/api.py:75: note: Use https://github.com/hauntsaninja/no_implicit_optional to automatically upgrade your codebase
src/files/api.py:76: error: Incompatible default for argument "thumbnail_metadata" (default has type "None", argument has type "ImageMetadataSchema")  [assignment]
src/files/api.py:76: note: PEP 484 prohibits implicit Optional. Accordingly, mypy has changed its default to no_implicit_optional=True
src/files/api.py:76: note: Use https://github.com/hauntsaninja/no_implicit_optional to automatically upgrade your codebase
Found 2 errors in 1 file (checked 141 source files)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant