OpenAPITools / OpenAPITools/openapi-generator
[Java] - [RestTemplate] - Generated client thread safety
Nobody has claimed this yet.
- 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;
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
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- 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