-
Notifications
You must be signed in to change notification settings - Fork 150
Description
Describe the bug
If a specification contains a default producer content-type and at least one route with a specific (and different) producer content-type, the content-type of a 405 and 404 responses will be this specific content-type.
Steps to Reproduce
A specification with a default content-type:
produces:
- application/json and at least one route with a specific content-type:
/blah:
get:
produces:
- application/octet-streamthen GET a route that either does not exist (404 response) or that does not implement GET but POST (405 response).
Expected behavior
404 & 405 responses should use the default producer content-type.
Actual behavior
404 & 405 responses use everything except the default producer content-type (if at least one route with a specific content-type exists.)
Environment
N/A
Additional context
The problem is located here:
- 405 → https://github.com/go-openapi/runtime/blob/master/middleware/router.go#L73
- 404 → https://github.com/go-openapi/runtime/blob/master/middleware/router.go#L77
IMHO it should be:
// Not found, check if it exists in the other methods first
if others := ctx.AllowedMethods(r); len(others) > 0 {
- ctx.Respond(rw, r, ctx.analyzer.RequiredProduces(), nil, errors.MethodNotAllowed(r.Method, others))
+ ctx.Respond(rw, r, []string{ctx.api.DefaultProduces()}, nil, errors.MethodNotAllowed(r.Method, others))
return
}
- ctx.Respond(rw, r, ctx.analyzer.RequiredProduces(), nil, errors.NotFound("path %s was not found", r.URL.EscapedPath()))
+ ctx.Respond(rw, r, []string{ctx.api.DefaultProduces()}, nil, errors.NotFound("path %s was not found", r.URL.EscapedPath()))
})
}I can do a PR if you agree.