swagger-api / swagger-api/swagger-codegen

global "consumes" flag should be ignored when method is GET or DELETE

Open
#4,668 4 comments 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

General: Suggestion
Dominant language
Mustache
Stars
17.8k
Forks
6k
PR merge metrics
No merged PRs in 30d

Description

Description

In the Swagger UI if you have a global consumes set then all of the controllers (for example in spring) will have the consumes annotation filled in. This makes the swagger-ui not work for GET and DELETE.

You can read the discussion as to why here: https://github.com/swagger-api/swagger-ui/issues/2408

Basically it comes down to this:

Yes, you can send a request body with GET but it should not have any meaning. If you give it meaning by parsing it on the server and changing your response based on its contents, then you are ignoring this recommendation in the HTTP/1.1 spec, section 4.3:

[...] if the request method does not include defined semantics for an entity-body, then the message-body SHOULD be ignored when handling the request.

And the description of the GET method in the HTTP/1.1 spec, section 9.3:

The GET method means retrieve whatever information ([...]) is identified by the Request-URI.

- http://stackoverflow.com/a/983458/3708426

Swagger-codegen version

Latest master

Swagger declaration file content or url
swagger: '2.0'

info:
  # .... ect

consumes:
  - application/json
Command line used for generation
// Configuration using a gradle plugin in gradle script kotlin
configure<CodegenConfigurator> {
    // Much of the irrelevant configuration has been removed.
    lang = "io.swagger.codegen.languages.SpringCodegen"
    additionalProperties.apply {
        put("java8", "true")
    }
}
Steps to reproduce
  1. Have your swagger spec declare that it consumes some mime type

  2. Try to send a GET request with the swagger-ui inside of spring with your API being generated with spring fox.

You will get a 415 error from spring because you haven't passed the Content-Type header.

Suggest a Fix

Options:

  • The consumes field in the spring (and probably other) controllers should not be populated on get and delete requests.
  • The consumes global field should be ignored but the one directly on the get request should be respected. Eg:
    paths:
      /cats:
        get:
          consumes:
            - application/x-www-form-urlencoded
          # ....
    
  • Some configuration flag should be introduced to allow you to chose to not generate consumes on GET and DELETE methods.
  • An issue should be opened with the Open API specification for 3.0

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by tracing SpringCodegen's handling of the global consumes declaration and how it populates generated controller annotations. Reproduce the reported 415 response with a GET or DELETE operation, then verify that global consumes is omitted for those methods while an operation-level consumes value remains respected.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, spring
Domain
api, backend, tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.