swagger-api / swagger-api/swagger-codegen
[JAVA] application/json is not the best choice for default content-type
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. :
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
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- 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