swagger-api / swagger-api/swagger-codegen

[JAVA] application/json is not the best choice for default content-type

Open
#6,819 0 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

The default java template (and the "library" java variants) apply a default content-type of application/json if it is not specified. :

https://github.com/swagger-api/swagger-codegen/blob/4f3c86aa145e040024ee4d539947cf3b69c60d21/modules/swagger-codegen/src/main/resources/Java/ApiClient.mustache#L526

I don't think it is correct to assume this content-type. This may cause parsing errors on the server side if the content is in fact not valid JSON. A safer, more generic "default" content-type would be application/octet-stream, as it is less likely to cause problems with parsing.

There is also some indication in the HTTP spec that application/octet-stream would be a better default:

If the media type remains unknown, the recipient SHOULD treat it as type "application/octet-stream".

So the generated swagger client would be explicitly setting the content-type that a correct server would also assume.

Swagger-codegen version

2.3.0 @ 6814530d690db7b88e91fc1aab50f10472f707bd

Swagger declaration file content or url
swagger: '2.0'
basePath: /
schemes:
  - http
paths:
  '/toggle':
    post:
      summary: Toggle the switch
      description: If switch is off, toggle to on, and vice versa.
      responses:
        200:
          description: Success
Command line used for generation

java -jar modules/swagger-codegen-cli/target/swagger-codegen-cli.jar generate -l java -i example.yaml

Steps to reproduce

Use the generated client to make a POST request, and observe that a header is added: "Content-Type: application/json", even though this is not specified in the YAML, and the request body does not contain JSON data.

Related issues/PRs

https://github.com/swagger-api/swagger-codegen/issues/6647

Suggest a fix/enhancement

Use application/octet-stream as the "default" content-type: #6820

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 at modules/swagger-codegen/src/main/resources/Java/ApiClient.mustache around line 526 and inspect how the generated client chooses a default Content-Type. Generate the Java client from the example.yaml using the command in the issue, then verify that the POST request no longer assumes JSON when no media type is specified and uses the intended generic default.

Written by the indexing model from the issue text.

Assessment

Tech stack
java
Domain
api
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.