fix(kotlin-client): rename parameters that collide with template locals
On master, jvm-ktor generates this for a query parameter named localVariableQuery:
val localVariableQuery = mutableMapOf<String, List<String>>()
localVariableQuery?.apply { localVariableQuery["localVariableQuery"] = listOf("$localVariableQuery") }
The template's own val localVariableQuery shadows the parameter, so the request sends the string form of the map instead of the caller's value. The kotlin client templates declare several such localVariable* locals (the naming follows the Java generators), and a spec parameter with the same Kotlin name is read from the local in every statement after the declaration.
Fix
KotlinClientCodegen.toParamName() renames a parameter to param_<name>, passed through toVariableName (for example paramLocalVariableQuery), when the name it would otherwise get is one of the colliding locals of the current library. This is the same pattern as the existing callback ā paramCallback rename. The wire name still comes from baseName.
A name counts as colliding only when the template references the parameter in a statement after the local is declared. A reference inside the local's own initializer still resolves to the parameter, for example a path parameter named localVariableConfig used in the RequestConfig(...) that initializes val localVariableConfig. I checked this with kotlinc. So those names keep working on master and are left alone.
| Library | Renamed | Not renamed (only referenced in their own initializer) |
|---|---|---|
jvm-okhttp4, jvm-spring-webclient, jvm-spring-restclient | localVariableBody, localVariableQuery, localVariableHeaders | localVariableConfig, localVarResponse, localVarError |
jvm-ktor, multiplatform | the three above and localVariableAuthNames | localVariableConfig |
jvm-vertx, jvm-retrofit2, jvm-volley, jvm-microprofile | none: their templates declare none of these localVariable* locals |
The table is derived from each library's api.mustache. If a template adds such a local, the list in KotlinClientCodegen needs the same name; the Javadoc there says so.
A reserved-word entry would not help here: for Kotlin, escapeReservedWord only wraps the name in backticks, and the backticked name is the same identifier.
multiplatform/api.mustache also changes in one line. For a single (non-array) file part it wrote {{{paramName}}}?.apply { append({{{baseName}}}) }, using the spec name as an identifier. That breaks whenever the two differ (for example upload_file / uploadFile), and the rename above would add another such case, so it now uses paramName on both sides. #25624 makes the same one-line change; whichever of the two is merged second will already have it.
An explicit parameterNameMappings entry still wins: the mapped name is used as is, even if it is one of the names above.
Behavior change
In the five affected libraries, a parameter with one of the listed names gets a new Kotlin name. Callers that pass it as a named argument (localVariableQuery = ā¦) have to use the new name. On master those parameters were not sent correctly. Server generators and the other four client libraries are unchanged.
Not covered here
These template names can also collide with parameter names. Only reading the templates, I have not found them in the checked-in samples. They need template changes rather than a parameter rename, so I'd rather handle them separately:
jvm-vertx: the localsvertxClient,request,form. (TheApiClientmembersbasePath,accessToken,apiKey,apiKeyPrefix,username,passwordandvertxare now read throughthis.in #25691.)jvm-volley: the localsbody,contentTypes,contentType,path,formParams,queryParams,headerParams, and the memberbasePath.
#25624 (form bodies in jvm-ktor / multiplatform) adds a lambda parameter localVariableBuilder. Whichever of the two PRs is merged second should add that name to the jvm-ktor and multiplatform entries; I will do it in the rebase.
Tests
KotlinClientCodegenApiTest (new):
| Test | Asserts (each as an exact, trimmed generated line) |
|---|---|
testTemplateLocalNameCollisionParamsAreRenamed, all 9 libraries (new allClientLibraries provider, adding jvm-microprofile to the existing list) | in the five affected libraries, each renamed parameter is still bound to its wire name; the names that must not change (localVariableConfig, localVarResponse, localVarError, localVariant, normalParam) keep their names everywhere; nothing is renamed in the other four libraries |
testTemplateLocalNameCollisionParameterNameMappingWins | a mapping to another name is used |
testTemplateLocalNameCollisionParameterNameMappingCanPinName | a mapping that pins localVariableQuery to itself keeps the name (the precedence check in toParamName) |
testMultiplatformMultipartFileParamAppendsParamName (new spec kotlin-multiplatform-multipart-file-param-name.yaml) | multiplatform: paramLocalVariableQuery?.apply { append(paramLocalVariableQuery) } and uploadFile?.apply { append(uploadFile) } |
./mvnw -nsu -Denforcer.skip=true -pl modules/openapi-generator -am -Dtest=KotlinClientCodegenApiTest -Dsurefire.failIfNoSpecifiedTests=false test
| Result | |
|---|---|
| master + this PR | 46 tests, 0 failures, 0 skipped |
with the KotlinClientCodegen change reverted | 6 failures: the jvm-ktor, jvm-okhttp4, jvm-spring-webclient, jvm-spring-restclient and multiplatform rows of testTemplateLocalNameCollisionParamsAreRenamed, and testMultiplatformMultipartFileParamAppendsParamName |
with only the parameterNameMapping check in toParamName removed | testTemplateLocalNameCollisionParameterNameMappingCanPinName fails |
with the multiplatform/api.mustache change reverted | testMultiplatformMultipartFileParamAppendsParamName fails |
Samples
Regenerated all bin/configs/kotlin-* configs (128, including the kotlin-server ones): no diff. None of their specs has a parameter with one of the listed names, and no multiplatform sample has a single file part whose name differs from its Kotlin name.
cc @wing328 and the Kotlin technical committee: @karismann @Zomzog @andrewemery @4brunu @yutaka0m @stefankoppier @e5l @AndreyVMarkelov
PR checklist
- Read the contribution guidelines.
- Run the following to build the project and update samples ā scoped to this change: built the CLI (
./mvnw -pl modules/openapi-generator-cli -am package -DskipTests) and regeneratedbin/configs/kotlin-*with./bin/generate-samples.sh; there is no sample diff.export_docs_generators.shnot run: no generator option or metadata changes.
(For Windows users, please run the script in WSL)./mvnw clean package || exit ./bin/generate-samples.sh ./bin/configs/*.yaml || exit ./bin/utils/export_docs_generators.sh || exit
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*.
IMPORTANT: Do NOT purge/delete any folders/files (e.g. tests) when regenerating the samples as manually written tests may be removed. - If your PR is targeting a particular programming language, @mention the technical committee members, so they are more likely to review the pull request.
š¤ Generated with Claude Code