jhthorsen / mojolicious-plugin-openapi

OpenAPI / Swagger plugin for Mojolicious
53 stars 41 forks source link

Wrong error description if status not defined in the spec #237

Closed tomascohen closed 1 year ago

tomascohen commented 2 years ago

Context:

If we have a path that should return a 400 in the controller, but the definition is missing in the spec, then we get the usual 500. But the information that is presented is misleading, as it refers to some internal default for the 400:

in the logs: [warn] OpenAPI >>> GET api/v1/items [{"message":"Missing property.","path":"\/body\/errors"}]

in the API tool (Postman):

{
    "errors": [
        {
            "message": "Missing property.",
            "path": "/body/errors"
        }
    ],
    "status": 500
}

We are facing this issue after we upgraded to the latest versions of the lib (we used an ancient version).

jhthorsen commented 1 year ago

That is very strange. If the spec is missing then you should get a 404, since the route does not exist. A "500" is usually when your Mojolicious application produces the wrong output. And that's also how it looks from the error message above.

jhthorsen commented 1 year ago

After looking at this again, I think the reason is that you actually generate a response, but it does not correspond with the default response added by M::P::OpenAPI (or https://metacpan.org/pod/JSON::Validator::Schema::OpenAPIv2#add_default_response)

Maybe you want to disable that feature all together in your case? I'm not sure how that would work, since it's used on invalid input and internal server error, so there might be some unexpected consequences... Please have a look at https://metacpan.org/pod/Mojolicious::Plugin::OpenAPI#default_response for how you can override the defaults.

Here are some examples:

$app->plugin(OpenAPI => {spec => ..., default_response => {status => []}});
$app->plugin(OpenAPI => {spec => ..., default_response => {status => [404, 500]}});