Created by: Paul-Etienne
This PR intends to fix #6779 (closed). There's a bug in the native Java generator that forces the Content-Type and Accept headers of every endpoint of the generated client to application/json
, even though a different media-type is specified in the spec.
I have let application/json
as a default value in case no media-type is found for a given endpoint. I made sur the Accept header contains all possible values referenced in the responses in the spec, as opposed to the Content-Type header where I only selected the first media-type found in the consumes array variable since this header only accepts one value, as per the HTTP standard.
I have tested this fix on a company project by using a template with api.mustache
, which has proven to give the expected results. We are using this template to fix this bug before this gets integrated to the OpenAPI Generator project. For my tests, I used a spec which had a request using application/xml
and were giving responses of types application/xml
or application/json
.
In order to validate my work, you need to use the Java generator to generate a client with the native library with a spec that has an endpoint with a request body of type different than application/json
. Same thing for the responses of that endpoint.
You should then look for this part of the generated code:
private HttpRequest.Builder endpointRequestBuilder(String body) throws ApiException {
...
localVarRequestBuilder.header("Content-Type", "application/xml"); // or any other type used in the spec
localVarRequestBuilder.header("Accept", "application/json, application/xml"); // or any other types used in the spec
...
return localVarRequestBuilder;
}
I hope this fix fully resolves the issue.
PR checklist
-
Read the contribution guidelines. -
Pull Request title clearly describes the work in the pull request and Pull Request description provides details about how to validate the work. Missing information here may result in delayed response from the community. -
Run the following to build the project and update samples: ./mvnw clean package ./bin/generate-samples.sh ./bin/utils/export_docs_generators.sh
./bin/generate-samples.sh bin/configs/java*
. For Windows users, please run the script in Git BASH.
Note: Despite running these scripts in Git BASH from Windows, it generated a lot of false positive changes with a wrong line separator (CRLF instead of LF) and with a backslash instead of slashes. So I only committed the changes that looked relevant to me. I hope I didn't miss anything.
-
File the PR against the correct branch: master
(5.3.0),6.0.x
-
If your PR is targeting a particular programming language, @mention the technical committee members, so they are more likely to review the pull request.
I'm tagging the technical committee responsible for the Java generator: @bbdouglas @sreeshas @jfiala @lukoyanov @cbornet @jeff9finger @karismann @Zomzog @lwlee2608