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
Support multiple responses with different response codes and schemes #327
Comments
Solution in PR may leads to the call chaining when decorator is being wrapped in itself several times. But type check happen early so it should be pretty shallow call chaining. |
Can I currently add a view that would return either a 200 or 422 response ? |
I managed to implement a workaround for now. I introduced an @blueprint.doc(summary="Get a thingy.")
@blueprint.get("/<thingy_id:thingy_id>")
@blueprint.output(GetThingyResponse)
@blueprint.expect([
InvalidRequestError, # raised by URL converter and request DTO validation.
ThingyNotFoundError
])
def get_thingy(
thingy_id: ThingyId,
) -> GetThingyResponseDto:
... This decorator basically adds a One caveat was I had to wrap the All my errors inherit from an Unfortunately I can't easily share the code as it's mixed with other changes that I've done to support Side note: A simpler solution to adding a new expect decorator, is to use @blueprint.doc(responses={
999: [
{ "status_code": 422, schema=... },
{ "status_code": 404, schema=... },
]
} And then create a def my_spec_processor(spec: dict[str, Any]) -> dict[str, Any]:
# Find the 999 responses and get the list of objects from the "description" and modify the spec as needed.
...
app.spec_processor(my_spec_processor) |
@victorcrimea @BEllis Hi guys, I'm trying to implement this in the 2.0 version. Could you please provide some complete examples of this usage? Just the example you gave me but with the main implementation inside the view function body (including all the schema definitions). I'm currently considering support passing a complete response spec through the @app.get('/')
@app.doc(responses={
400: {
'description': 'Error',
'content': {
'application/json': {
'schema': ErrorSchema
}
}
},
422: {
'description': 'Another error',
'content': {
'application/json': {
'schema': AnotherErrorSchema
}
}
}
})
def say_hello():
... Will this meet your needs? |
The 422 error is added automatically when you added an ...
"422": {
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/ValidationError"
}
}
},
"description": "Validation error"
} |
In this example we return either InstructorResponseGetSchema, or structured error(it has error_code, error explanation, etc). Note that this is syntax of modified APIFlask that we forked some time ago.
I do not like how JSON-like decorator looks, but I think it may provide even more flexibility, beyond the cases we are looking at now. |
Now @app.doc decorator now accepts responses in a form of a dict {status_code: description} which is not enough to describe an endpoint wthat has multiple possible status codes each one one with it's one Schema and examples.
Suggested use-case
The text was updated successfully, but these errors were encountered: