quart-schema icon indicating copy to clipboard operation
quart-schema copied to clipboard

convert_model_result breaks on receiving GenericBaseModel as result

Open joeblackwaslike opened this issue 9 months ago • 0 comments

It's common to use Generics to reuse a wrapper schema to enclose many different payload schemas existing on the data attribute. This is what I'm currently doing but when I do that, make_response raises an exception when I hit the endpoint.

TypeError: The response value type (GenericResponseWrapper[LinkAccountResponse]) is not valid

It would seem after a little exploration of the code, the problem lies here: https://github.com/pgjones/quart-schema/blob/a2171e82cf20ae070c5471e4ae353a2cb3e55846/src/quart_schema/extension.py#L335C18-L335C18

elif isinstance(value, BaseModel):

should instead be

elif isinstance(value, (BaseModel, GenericModel)):

with the additional import

from pydantic.generics import GenericModel

For test case purposes I use the following model for the envelope "(any model for the actual response schema):

DataT = TypeVar("DataT")

class GenericResponseWrapper(GenericModel, Generic[DataT]):
    """Generic response wrapper using actual generics."""

    error_code: str = ""
    status: str = ""
    message: str = ""
    data: DataT = Field(default_factory=dict)

    @validator("status")
    def set_status_by_error_code(cls, v, values):
        error_code = values.get("error_code")
        if error_code:
            return "failed"
        return "ok"

I use the following style type hint for the view:

GenericResponseWrapper[DataResponseModel]

and return from the view like so

return GenericResponseWrapper[DataResponseModel](
    data=DataResponseModel(
        x=x,
        y=y,
    ),
)

joeblackwaslike avatar Sep 12 '23 17:09 joeblackwaslike