pgjones / quart-schema

Quart-Schema is a Quart extension that provides schema validation and auto-generated API documentation.
MIT License
76 stars 24 forks source link

Documentation page breaks with response dataclasses that has a type of another dataclass #9

Closed ajnieset closed 2 years ago

ajnieset commented 2 years ago

Hi,

There seems to be an issue with the documentation page generation when using @validate_response with a dataclass object that relies on another dataclass for one of the field types

ex

@dataclass
class UserData:
    id: int
    username: str
    email: str
    password: str

@dataclass
class Users:
    users: list[UserData]

@blueprint.get("/users/")
@validate_response(Users)
async def get_users():
    users = await User.objects.all()
    users = [UserData(**user.__dict__) for user in users]
    return Users(users=users), 200

the stack trace:

 File "pydantic/schema.py", line 185, in pydantic.schema.model_schema
  File "pydantic/schema.py", line 590, in pydantic.schema.model_process_schema
  File "pydantic/schema.py", line 631, in pydantic.schema.model_type_schema
  File "pydantic/schema.py", line 258, in pydantic.schema.field_schema
  File "pydantic/schema.py", line 471, in pydantic.schema.field_type_schema
  File "pydantic/schema.py", line 858, in pydantic.schema.field_singleton_schema
  File "pydantic/schema.py", line 710, in pydantic.schema.field_singleton_sub_fields_schema
  File "pydantic/schema.py", line 536, in pydantic.schema.field_type_schema
  File "pydantic/schema.py", line 933, in pydantic.schema.field_singleton_schema
KeyError: <class 'pydantic.dataclasses.UserData'>

The stack trace doesn't tell me much here. I know that my api actually tests fine and returns responses, but the documentation wont generate for the get all users endpoint

other relevant info

pgjones commented 2 years ago

I think this is a pydantic bug, although I can't figure out what the actual bug is. If you switch to using a pydantic dataclass (from pydantic.dataclasses import dataclass) this is usually fixed.