Open p3rs1st opened 10 months ago
You can do this:
https://docs.pydantic.dev/latest/concepts/models/#rootmodel-and-custom-root-types
from pydantic import RootModel
class Resp(BaseModel):
a: int
class Resp1(BaseModel):
b: int
class UnionRespResp2(RootModel):
root:Union[Resp, Resp1]
@app.get("/", responses={200: UnionRespResp2})
def book():
pass
Thanks, now I can see the schema of the responses, but I want the example value to be shown both of them.
However, I can only see one of the example value like
If I want to see the both example value of the responses, how should I do?
When you don't have a configuration example, the Swagger UI automatically generates one. You can do this by configuring openapi_extra fields in the model_config
.
from typing import Union
from pydantic import BaseModel, RootModel
from flask_openapi3 import OpenAPI
app = OpenAPI(__name__)
class Resp(BaseModel):
a: str
class Resp1(BaseModel):
b: int
class UnionRespResp2(RootModel):
root: Union[Resp, Resp1]
model_config = {
"openapi_extra": {
"examples": {
"Success Example": {
"summary": "Success Example",
"value": {
"a": "sss"
}
},
"Fail Example": {
"summary": "Fail Example",
"value": {
"b": -1
}
}
}
}
}
@app.get("/", responses={200: UnionRespResp2})
def book():
return "ok"
if __name__ == "__main__":
app.run(debug=True)
In current flask-openapi3, I can only map 200 to Resp, 201: Resp1.
But I want to map all of these to one HttpStatus, because they are both HttpStatus 200. like
Or allow the key of responses can be arbitrary str like