OpenAPITools / OpenAPITools/openapi-generator

[Java] - [RestTemplate] - Generated client thread safety

Open
#2,280 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Enhancement: Feature
Dominant language
Java
Stars
26.8k
Forks
7.7k
PR merge metrics
PR metrics pending

Description

Is your feature request related to a problem? Please describe.

Thread safety in the ApiClient (Java - RestTemplate) - see for more info: https://github.com/swagger-api/swagger-codegen/issues/9222

Describe the solution you'd like

As described in the linked issue, I am happy to provide a PR for this, that addresses either just the:

private HttpStatus statusCode;
private MultiValueMap<String, String> responseHeaders;

https://github.com/OpenAPITools/openapi-generator/blob/master/modules/openapi-generator/src/main/resources/Java/libraries/resttemplate/ApiClient.mustache#L100

By making them part of the return value from invokeAPI method, this would be inline with the java - jersey2 library. Also this would addresses the main multi threading issue, that are caused by the library itself.

However the above does not make it fully thread safe, there are still issue with the fields:

private boolean debugging = false;
private HttpHeaders defaultHeaders = new HttpHeaders();
private String basePath = "{{basePath}}";
private RestTemplate restTemplate;
private Map<String, Authentication> authentications;
private DateFormat dateFormat;

The above are not modified by the openapi generate code so the issue with multi threading has to be caused by the user of the client changing these values at runtime (there are public method to do this). I can also resolve these in the PR by making them immutable.

What would be the preferred approach? Thoughts?

Describe alternatives you've considered

Described two possible solution above.

Additional context

https://github.com/swagger-api/swagger-codegen/issues/9222
https://github.com/swagger-api/swagger-codegen/issues/1435
https://github.com/swagger-api/swagger-codegen/issues/7453
https://github.com/swagger-api/swagger-codegen/pull/7605

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with modules/openapi-generator/src/main/resources/Java/libraries/resttemplate/ApiClient.mustache, especially invokeAPI and the listed mutable fields. Compare the proposed return-value approach with the Java jersey2 library and review the linked Swagger issues and pull request. Done requires an agreed approach that addresses the generated client's thread-safety concerns.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, spring
Domain
api, backend
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.