Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow 422 response to specified model not only array #137

Open
p3rs1st opened this issue Jan 12, 2024 · 3 comments
Open

Allow 422 response to specified model not only array #137

p3rs1st opened this issue Jan 12, 2024 · 3 comments
Labels

Comments

@p3rs1st
Copy link

p3rs1st commented Jan 12, 2024

In flask-openapi3, I can only specify the item of 422 response array, but I want the response of 422 is a object.

@luolingchun
Copy link
Owner

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?

@p3rs1st
Copy link
Author

p3rs1st commented Jan 14, 2024

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"
}]

@luolingchun
Copy link
Owner

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.

@github-actions github-actions bot added the Stale label Apr 1, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants