turner-townsend / flask-pydantic-spec

An Flask OpenAPI library using Pydantic
Apache License 2.0
99 stars 19 forks source link

[Feature] - Add support for unions in validation #68

Open cgearing opened 4 months ago

cgearing commented 4 months ago

It would be fairly easy to provide support for unions in the validator:

e.g

@api.post("/building")
@spec_api.validate(body=Apartment | House, resp=Response(HTTP_200=SuccessModel))
def create_building() -> Any:
    building = request.context.body
    ...

We could probably follow the approach taken by FastAPI and generate a temporary schema for the OpenAPI docs that just concatenates the name of the inputs together:

"schema": {
    "anyOf": [
        {
            "$ref": "#/components/schemas/Apartment"
        },
        {
            "$ref": "#/components/schemas/House"
        },
    ],
    "title": "ApartmentHouseUnion"
}