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] Using pydantic.Json[SomeModel] in a query param fails #1153

Open
way-dave opened this issue May 6, 2024 · 1 comment · May be fixed by #1242
Open

[BUG] Using pydantic.Json[SomeModel] in a query param fails #1153

way-dave opened this issue May 6, 2024 · 1 comment · May be fixed by #1242

Comments

@way-dave
Copy link

way-dave commented May 6, 2024

Describe the bug

I'd like to be able to decode JSON-encoded pydantic models from a query param like this:

from __future__ import annotations

from django.http import HttpRequest
from ninja import NinjaAPI, Query, Schema
from pydantic import Json

api = NinjaAPI()

class PetSchema(Schema):
    nicknames: list[str]

class PersonSchema(Schema):
    pet: PetSchema | None

class QuerySchema(Schema):
    q: Json[PersonSchema]

@api.get("/")
def my_endpoint(request: HttpRequest, query: Query[QuerySchema]) -> None:
    pass

Unfortunately, it crashes out with this error:

File "/workspaces/app/staff_v2/foo.py", line 22, in <module>
  @api.get("/")
  ^^^^^^^^^^^^
File "/workspaces/app/.venv/lib/python3.11/site-packages/ninja/router.py", line 240, in decorator
  self.add_api_operation(
File "/workspaces/app/.venv/lib/python3.11/site-packages/ninja/router.py", line 289, in add_api_operation
  path_view.add_operation(
File "/workspaces/app/.venv/lib/python3.11/site-packages/ninja/operation.py", line 357, in add_operation
  operation = OperationClass(
              ^^^^^^^^^^^^^^^
File "/workspaces/app/.venv/lib/python3.11/site-packages/ninja/operation.py", line 69, in __init__
  self.signature = ViewSignature(self.path, self.view_func)
                    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/workspaces/app/.venv/lib/python3.11/site-packages/ninja/signature/details.py", line 87, in __init__
  self.models: TModels = self._create_models()
                          ^^^^^^^^^^^^^^^^^^^^^
File "/workspaces/app/.venv/lib/python3.11/site-packages/ninja/signature/details.py", line 144, in _create_models
  flatten_map = self._args_flatten_map(args)
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/workspaces/app/.venv/lib/python3.11/site-packages/ninja/signature/details.py", line 181, in _args_flatten_map
  for name, path in self._model_flatten_map(arg.annotation, arg.alias):
File "/workspaces/app/.venv/lib/python3.11/site-packages/ninja/signature/details.py", line 205, in _model_flatten_map
  yield from self._model_flatten_map(field.annotation, name)  # type: ignore
  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/workspaces/app/.venv/lib/python3.11/site-packages/ninja/signature/details.py", line 205, in _model_flatten_map
  yield from self._model_flatten_map(field.annotation, name)  # type: ignore
  ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "/workspaces/app/.venv/lib/python3.11/site-packages/ninja/signature/details.py", line 201, in _model_flatten_map
  for attr, field in model.model_fields.items():
                      ^^^^^^^^^^^^^^^^^^
AttributeError: 'types.UnionType' object has no attribute 'model_fields'

This makes me think it's not aware of the Json[...] annotation and is still trying to unfurl the model into individual query params.

Versions:

  • Python version: 3.11.6
  • Django version: 4.2.10
  • Django-Ninja version: 1.1.0
  • Pydantic version: 2.7.1
@metheoryt
Copy link

Got same error while migrating to pydantic v2 - it seems that it does not support UnionType, or bool | str annotated fields. I just tried to get rid of all field: str | None unions, making them field: str = None instead, and it seems to be working

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

Successfully merging a pull request may close this issue.

2 participants