luolingchun / flask-openapi3

Generate REST API and OpenAPI documentation for your Flask project.
https://luolingchun.github.io/flask-openapi3/
MIT License
203 stars 33 forks source link

Allow `default` key for error response like in Openapi 3.0+ #187

Open ddorian opened 1 month ago

ddorian commented 1 month ago

Environment:

Openapi allows to have default error response https://swagger.io/docs/specification/v3_0/describing-responses/#default-response.

This isn't supported on:

Openapi(validation_error_status="default")

Quick fix is in adding default to HTTP_STATUS:

flask_openapi3.utils.HTTP_STATUS["default"] = HTTPStatus(422).phrase
luolingchun commented 3 weeks ago

My understanding is that default can be set not only to an error response, but also to a correct response.

There are other ways to specify the default response:


default_response = {
    "default": {
        "description": "Unexpected error",
        "content": {
            "application/json": {
                "schema": {
                    "$ref": "#/components/schemas/Error"
                }
            }
        }
    }
}

app = OpenAPI(__name__, info=info,responses=default_response)

# in APIBlueprint
api = APIBlueprint(__name__, responses=default_response)

# in api
@app.get("/test", responses=default_response)
def get_test():
    ...
ddorian commented 3 weeks ago

but also to a correct response.

Can you link to a spec that mentions this? Two places that I've read mentioned only for error response.

Another way would be to use 1XX, 2XX, 3XX, 4XX, and 5XX like in https://spec.openapis.org/oas/latest.html#patterned-fields-0

ddorian commented 3 weeks ago

There are other ways to specify the default response:

Yes you can create the responses there. But you still link to the default error response (fixed in https://github.com/luolingchun/flask-openapi3/pull/190)

luolingchun commented 3 weeks ago

Sorry, I understand that #190 just encapsulates a function(_register_default_error_responses) and doesn't change substantially.

I mean we don't need to make any adjustments for this, because the way to set the default response already exists.

ddorian commented 3 weeks ago

I want to set the default error response to be "default" (i think it's by the spec). But it breaks in https://github.com/luolingchun/flask-openapi3/blob/0b072ebfa3aee3dfbb60149abf742b8cf97bfe63/flask_openapi3/utils.py#L559 Because it returns a http response with status_code="default".

But that is not needed if I override OpenAPI.validation_error_callback, which I have.

I think merging #190 will make this to not be needed since I can override that function too. I may be a bit confused because I'm trying to do multiple things at once. But this issue was to also get your opinion.

So merging #190 will also close this I believe.