turner-townsend / flask-pydantic-spec

An Flask OpenAPI library using Pydantic
Apache License 2.0
98 stars 17 forks source link

Support Python types in Response #55

Closed matheusads closed 11 months ago

matheusads commented 1 year ago

Hello, I started using this library recently and notice that I can't use Python types in Response.

Example:

@api.validate(query=PropertyQueryModel, resp=Response(HTTP_200=list[PropertyModel]), tags=['properties'])

This raises an Assertion Error from Response init assert issubclass(value, BaseModel), "invalidpydantic.BaseModel"

I think could be a good improvement to accept Python types, makes sense? Quickly looking at the code I didn't find other points where it could affect.

Maybe I can help in this part. Best.

cgearing commented 1 year ago

Would you expect this to be a bare array of that type in the generated OpenAPI spec?

i.e:

{
  "description": "Some response",
  "content": {
    "application/json": {
      "schema": {
        "type": "array",
        "items": {
          "$ref": "#/components/schemas/PropertyModel"
        }
      }
    }
  }
}

If you want a quick way of doing this, I have done it elsewhere by creating a Pydantic compatible wrapper type:

class PropertyModels(BaseModel):
    __root__: list[ProperyModel]

We could definitely look at how that might work though!

matheusads commented 1 year ago

I use this solution with root too, but I think it's a good option to accept Python types directly like list[str] for example.

PeterNociar commented 1 year ago

I made it work here Its working for list[BaseModel]