[BUG] Java RestTemplate Mustache template doesn't use reserved words for local variables
Created by: amra
Bug Report Checklist
-
Have you provided a full/minimal spec to reproduce the issue? -
Have you validated the input using an OpenAPI validator (example)? -
Have you tested with the latest master to confirm the issue still exists? -
Have you searched for related issues/PRs? -
What's the actual output vs expected output? -
[Optional] Sponsorship to speed up the bug fix or feature request (example)
Description
Java generator defines list reserved words, part of them are for local variables. These are not used for java resttemplate. And local variable name can collide with parameter name (as it happened in my case). Code will not compile because of it.
Examples:
resttemplate snippet from openapi-generator\Java\libraries\resttemplate\api.mustache
(with wrong local variable name):
final MultiValueMap<String, String> queryParams = new LinkedMultiValueMap<String, String>();
final HttpHeaders headerParams = new HttpHeaders();
final MultiValueMap<String, String> cookieParams = new LinkedMultiValueMap<String, String>();
final MultiValueMap<String, Object> formParams = new LinkedMultiValueMap<String, Object>();{{#hasQueryParams}}
resteasy snippet from openapi-generator\Java\libraries\resteasy\api.mustache
(with local variable name using reserved word):
{{javaUtilPrefix}}List<Pair> localVarQueryParams = new {{javaUtilPrefix}}ArrayList<Pair>();
{{javaUtilPrefix}}Map<String, String> localVarHeaderParams = new {{javaUtilPrefix}}HashMap<String, String>();
{{javaUtilPrefix}}Map<String, String> localVarCookieParams = new {{javaUtilPrefix}}HashMap<String, String>();
{{javaUtilPrefix}}Map<String, Object> localVarFormParams = new {{javaUtilPrefix}}HashMap<String, Object>();
openapi-generator version
5.4
OpenAPI declaration file content or url
inputSpec: spec/openapi.json
generatorName: java
library: resttemplate
outputDir: generated
java8: true
invokerPackage: com.project.client
modelPackage: com.project.model
apiPackage: com.project.api
groupId: com.api
artifactId: project
artifactVersion: 1.0-SNAPSHOT
Generation Details
./bin/openapi-generator-cli generate --config opslog.yaml
Steps to reproduce
- Create a
openapi.json
spec, where a parameter name will bequeryParams
. - Generate code using java resttemplate.
- Code will not compile because method parameter name collide with local variable name.
Example:
"/api/v1/project/commands": {
"put": {
"operationId": "commands",
"parameters": [
{
"in": "query",
"name": "queryParams",
"required": true,
"schema": {
"$ref": "#/components/schemas/ProjectCommonQueryParameters"
}
}
],
Related issues/PRs
Suggest a fix
Use reserved names local*
for local variables for resttemplate as it's used for other libraries, e.g. resteasy.
List of reserved words for java: https://openapi-generator.tech/docs/generators/java#reserved-words