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] OpenAPI schema generator error with ManyToMany UUID #881

Closed
jam13 opened this issue Oct 18, 2023 · 2 comments · Fixed by #982
Closed

[BUG] OpenAPI schema generator error with ManyToMany UUID #881

jam13 opened this issue Oct 18, 2023 · 2 comments · Fixed by #982
Labels
bug Something isn't working v1

Comments

@jam13
Copy link

jam13 commented Oct 18, 2023

Describe the bug
I have a Project model with a many_to_many relationship to User called user. Both models use UUID as pk.

Created a schema like this:

class ProjectOut(ModelSchema):
    class Config:
        model = Project
        model_fields = "__all__"

and an endpoint like this:

@api.get("/projects", response=List[ProjectOut])
def list_projects(request):
    return get_list_or_404(Project, user=request.user)

This seems to work as expected, with a list of UUID strings returned for user. However when trying to load the OpenAPI docs I get an error in the brower and log output:

...
  File "/opt/venv/lib/python3.11/site-packages/ninja/orm/fields.py", line 88, in __get_pydantic_json_schema__
    json_type = {
                ^
KeyError: <class 'uuid.UUID'>

If I exclude the user field, then OpenAPI is fine, and I can return the list of UUIDs by using resolve:

user: List[UUID]
@staticmethod
def resolve_user(obj):
    return [str(u.id) for u in obj.user.all()]

Versions (please complete the following information):

  • Python version: 3.11
  • Django version: 4.2
  • Django-Ninja version: 1.0b2
  • Pydantic version: 2.4
@vitalik vitalik added v1 bug Something isn't working labels Oct 18, 2023
@nickmccullum
Copy link

I am experiencing this as well, any thoughts on when this could be patched?

@gokaykucuk
Copy link
Contributor

A simple addition of UUID type to ninja/orm/fields.py seems like working for me atm. But I haven't tested anything properly.

    json_type = {
        int: "integer",
        str: "string",
        float: "number",
        UUID: "string",
    }[type_]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working v1
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants