java: Content-Type: application/json on GET requests
Created by: lorenzleutgeb
Description
GET requests usually have no body, and therefore most of the time no Content-Type
header is transferred. The OpenAPI makes it especially hard (probably impossible) to define one, as I have noted in OAI/OpenAPI-Specification#1628
I only ran into this issue because I observed that the Java client generated by this generator used Content-Type: application/json
and I could not explain why. After some searching I think I found the cause for this rather strange behaviour.
In api.mustache
(line highlighted) you call ApiClient#selectHeaderContentType
(line highlighted) for all types of requests. Since (by validity of the input spec) there are no content types for GET requests, you will always default to application/json
. That's bad, and for example makes it impossible to describe JSON API and get a Java client in a straightforward way.
The workaround that I used is an interceptor like this:
if ("GET".equals(request.method()) && request.header("Content-Type") != null) {
System.err.println("We attempt to send a content type on a GET request! Removing it forcibly!");
request = request.newBuilder()
.removeHeader("Content-Type")
.build();
}
openapi-generator version
020883fd (master from 2018-07-04)
OpenAPI declaration file content or url
Use ping.yaml
.
Command line used for generation
java -jar openapi-generator-cli.jar generate -g java -i api.yml -o jclient
Steps to reproduce
Generate client.
Related issues/PRs
See Description.
Suggest a fix/enhancement
Do not attempt to even infer a content type for a GET request.