-
-
Notifications
You must be signed in to change notification settings - Fork 6.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
java: Content-Type: application/json on GET requests #476
Comments
Thank you for this issue. I am not that familiar with HTTP Headers, but if I understand you correctly when in a OpenAPI Spec with an
I have compared the header sent between the java clients for the
So I guess it is OK to not send |
Sorry for missing that, I always talk about OAS3. There is no mention of Your reiteration of the issue is correct. For the example of Also, note that in your comment not only the |
Yes I have also mixed OAS2 and OAS3 in my message.
Is to understand as: that does not have a requestBody with a mimeType. Thank you for the quick feedback. I think changing the code as you have proposed is a good idea. In ApiClient (to be changed in /**
* Select the Content-Type header's value from the given array:
* if JSON exists in the given array, use it;
* otherwise use the first one of the array.
*
* @param contentTypes The Content-Type array to select from
* @return The Content-Type header to use. If the given array is empty, null will be returned.
* If the given array matches "any", JSON will be used.
*/
public String selectHeaderContentType(String[] contentTypes) {
if (contentTypes.length == 0) {
return null;
}
for (String contentType : contentTypes) {
if (isJsonMime(contentType)) {
return contentType;
}
}
if(contentTypes[0].equals("*/*")) {
return "application/json";
}
return contentTypes[0];
} In final String localVarContentType = apiClient.selectHeaderContentType(localVarContentTypes);
if(localVarContentType != null) {
localVarHeaderParams.put("Content-Type", localVarContentType);
} Does it makes sense to you? Do you want to open a pull request for that? |
These two changes LGTM. I could open a PR, but you did the work and are maintainer, so I guess the change would get through quicker if you do it. But wait, one question though: What if the OAS 3 spec defines some MIME type that returns |
I have started to work on this on a branch issue476_contentType in my fork. I would like to perform some tests before I open a PR. You can already review it there. |
A desired fix! any timeline? |
I'd also appreciate a fix for this problem. |
Is there a reason the proposed changes here were never actually merged? seems this issue has dragged on for a while now. Any updates? |
I'd be interested, too! |
I ran across this issue in relation to a This can be reproduced by generating the client for the petstore project where the
and the In general, I would expect any request that doesn't specify a request body to not send the For context, this is with generatorName=java and configOptions/library=resttemplate. |
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#1628I 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 callApiClient#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 toapplication/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:
openapi-generator version
020883f (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.
The text was updated successfully, but these errors were encountered: