swagger-api / swagger-api/swagger-codegen

[JAVA] Spring template removes default MessageConverters causing Response Code 406

Open
#6,083 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

Description

In the current Spring MVC template, the generated SwaggerUIConfiguration extends WebMvcConfigurerAdapter and overrides configureMessageConverters() to add a Jackson2 converter.

Due to the expectations Spring has, this has a seemingly unintentional side-effect of removing all the default Message Converters known to Spring, which results in unexpected status code 406 for mime types other than application/json (in my case text/csv) when SwaggerUIConfiguration is used.

I believe this is not intentional since the method then calls super.configureMessageConverters(), however the super implementation is empty so this does nothing. If you look in https://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/web/servlet/config/annotation/WebMvcConfigurationSupport.html it has a slightly odd approach where a null response from configureMessageConverters causes the defaults to be registered, but any response from configureMessageConverters causes all the defaults to be lost.

In the same file, the docs recommend instead overriding extendMessageConverters in order to register custom converters without preventing the defaults being registered.

To work around this I had to explicitly re-add the defaults. I believe instead the SwaggerUIConfiguration class should override extendMessageConverters so the default converters will still be registered.

Swagger-codegen version

swagger-codegen-2.2.2 (through maven plugin). Not a regression that I know of.

Swagger declaration file content or url

Relevant portion of the swagger file below:

  /csv:
    get:
      tags:
      - "test"
      summary: "CSV test"
      description: ""
      operationId: "getCSV"
      produces:
      - "text/csv"
      responses:
        200:
          description: "successful operation"
          schema:
            type: string
Command line used for generation

spring-mvc language, generated using Maven plugin

Steps to reproduce

Generate API including above method and spring-mvc language. Implement the API and have it return any random string. Call the method.

Expected: correct response with code 200

Actual: response code 406 (not acceptable)

Related issues/PRs

None I can find.

Suggest a fix/enhancement

I can make a PR to act on the suggestion above, but wanted to 1) get this recorded for anyone hitting the same problem on the current version and 2) ask for input on the proposed approach.

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 with the generated Spring MVC template's SwaggerUIConfiguration and inspect how configureMessageConverters is implemented. Reproduce the issue with an endpoint producing text/csv, then verify that the generated configuration preserves Spring's default converters and the request returns 200 instead of 406.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, spring
Domain
api, backend
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.