Skip to content
GitLab
Projects Groups Snippets
  • /
  • Help
    • Help
    • Support
    • Community forum
    • Submit feedback
    • Contribute to GitLab
  • Sign in / Register
  • O openapi-generator
  • Project information
    • Project information
    • Activity
    • Labels
    • Members
  • Repository
    • Repository
    • Files
    • Commits
    • Branches
    • Tags
    • Contributors
    • Graph
    • Compare
  • Issues 3,476
    • Issues 3,476
    • List
    • Boards
    • Service Desk
    • Milestones
  • Merge requests 402
    • Merge requests 402
  • CI/CD
    • CI/CD
    • Pipelines
    • Jobs
    • Schedules
  • Deployments
    • Deployments
    • Environments
    • Releases
  • Packages and registries
    • Packages and registries
    • Package Registry
    • Infrastructure Registry
  • Monitor
    • Monitor
    • Incidents
  • Analytics
    • Analytics
    • Value stream
    • CI/CD
    • Repository
  • Wiki
    • Wiki
  • Snippets
    • Snippets
  • Activity
  • Graph
  • Create a new issue
  • Jobs
  • Commits
  • Issue Boards
Collapse sidebar
  • OpenAPI Tools
  • openapi-generator
  • Merge requests
  • !11303

[Java][Native] Fix the Content-Type and Accept headers that were forced to application/json

  • Review changes

  • Download
  • Email patches
  • Plain diff
Merged Administrator requested to merge github/fork/Paul-Etienne/fix-6779 into master Jan 13, 2022
  • Overview 0
  • Commits 2
  • Pipelines 0
  • Changes 9

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
    Commit all changed files. This is important, as CI jobs will verify all generator outputs of your HEAD commit as it would merge with master. These must match the expectations made by your contribution. You may regenerate an individual generator by passing the relevant config(s) as an argument to the script, for example ./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

Assignee
Assign to
Reviewers
Request review from
Time tracking
Source branch: github/fork/Paul-Etienne/fix-6779