Skip to content

Bad 404 & 405 content-type #372

@maxatome

Description

@maxatome

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-stream

then 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:

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions