flask-openapi3
flask-openapi3 copied to clipboard
Allow 422 response to specified model not only array
In flask-openapi3, I can only specify the item of 422 response array, but I want the response of 422 is a object.
422 is caused by the ValidationError of Pydantic, which defaults to an array.
Why do you encounter situations where it is not an array?
Can you paste a code example?
For example, when I receive a ValidationError, I want to make the response as a JSON form like
{
"success": False,
"msg": "Validation Error detail message here"
}
But I can only change the array item which is the parameter 'validation_error_model' of OpenAPI like
[{
"success" False,
"msg": "one Validation Error detail message"
}]
flask-openapi3
generates a default ValidationError class from Pydantic. Of course you can change it, you can change the response body model of each API, and you can change the global response body model.
class Model422(BaseModel):
success: bool
msg: str
# global response body model
app = OpenAPI(__name__, responses={422:Model422})
# the response body model of each API
@app.post("/", responses={200: UnionRespResp2,422:Model422})
def book(body: Resp):
...
Here is more information for ValidationError.