fix(kotlin-client): read ApiClient properties through this. in jvm-vertx

KH
20h ago
+445-328

The jvm-vertx API classes extend ApiClient and read its properties (basePath, accessToken, apiKey, apiKeyPrefix, username, password, vertx) without this.. In Kotlin a function parameter hides a class property with the same name, so a spec parameter with one of these names replaces the client's value inside that operation.

Reproducer

modules/openapi-generator/src/test/resources/3_0/kotlin/kotlin-vertx-member-collision.yaml (added by this PR) has operations with basic, bearer, OAuth2 and API key (header and query) security, and query parameters named after the properties. On master, jvm-vertx generates for example:

fun basicOpWithHttpInfo(username: kotlin.String?, password: kotlin.String?) : Future<ApiResponse<Unit?>> {
    val vertxClient = WebClient.create(vertx)
    val request = vertxClient.requestAbs(HttpMethod.GET, UriTemplate.of("$basePath/basic"))
    ...
    username?.let { username ->
        password?.let { password ->
            request.basicAuthentication(username, password)

I compiled and ran the same shapes with kotlinc:

  • the basic credentials sent are the values of the username and password parameters, not the client's;
  • a basePath parameter replaces the base URL;
  • an apiKey parameter (a String?) makes apiKey["X-API-KEY"] fail to compile.

Fix

jvm-vertx/api.mustache now reads these properties through this.:

  • WebClient.create(this.vertx) and "${this.basePath}{{{path}}}";
  • this.apiKey[...] and this.apiKeyPrefix[...] in the API key branches;
  • this.username?.let { username -> this.password?.let { password -> ... } };
  • this.accessToken?.let { accessToken -> ... } for bearer and OAuth.

The parameters keep their names, so the generated signatures do not change. Only the property reads change.

Not covered here

  • Function calls (responseBody(...), handleResponse(...), encodeURIComponent(...), parseDateToQueryString(...), toMultiValue(...), collectionDelimiter(...)) are left as they are. Kotlin resolves a call to the function even when a value parameter has the same name; I checked this with kotlinc.
  • The template's own locals in jvm-vertx (vertxClient, request, form) can also collide with parameters. A body parameter named request, for example, makes .sendBuffer(responseBody(request)) read the local. Neither this PR nor #25690 (which renames parameters that collide with localVariable* locals in five other libraries) handles them. Renaming them touches the same lines as #25690's and #25623's tests, so I'd rather do it after those are settled.
  • jvm-volley has the same pattern: the properties basePath, requestFactory, postProcessors and requestQueue are read without this.. This needs a separate change.

Relation to other open PRs

  • #25690 changes KotlinClientCodegen and does not rename anything in jvm-vertx. The two PRs only meet in KotlinClientCodegenApiTest.
  • #25623 (jvm-vertx form bodies) changes other lines of the same template; the two template changes merge without conflict. The test file and the jvm-vertx samples overlap. Whichever PR is merged second needs a rebase and a sample regeneration, which I will do.

Tests

KotlinClientCodegenApiTest.testJvmVertxApiClientMembersAreThisQualified (new) generates jvm-vertx from the spec above. It checks two things:

  • every property read listed under Fix appears with this., matched as an exact, trimmed line, for each auth branch;
  • no line reads one of the seven properties without this..

The same parameters are still sent as query parameters under their own names.

./mvnw -nsu -Denforcer.skip=true -pl modules/openapi-generator -am -Dtest=KotlinClientCodegenApiTest -Dsurefire.failIfNoSpecifiedTests=false test

Result
master + this PR35 tests, 0 failures, 0 skipped
with the template change revertedtestJvmVertxApiClientMembersAreThisQualified fails
with only one this. removed (apiKeyPrefix, OAuth accessToken, or the query API key)testJvmVertxApiClientMembersAreThisQualified fails in each case

Samples

Regenerated bin/configs/kotlin-*. Only the four jvm-vertx samples (kotlin-jvm-vertx-gson, -jackson, -jackson-coroutines, -moshi) change, in PetApi.kt, StoreApi.kt and UserApi.kt (12 files). Each changed line only gains this.. I have not compiled the samples locally; the existing Kotlin client sample workflow builds them.

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 regenerated bin/configs/kotlin-* with ./bin/generate-samples.sh; the four jvm-vertx samples are committed. export_docs_generators.sh not run: no generator option or metadata changes.
    ./mvnw clean package || exit
    ./bin/generate-samples.sh ./bin/configs/*.yaml || exit
    ./bin/utils/export_docs_generators.sh || exit
    
    (For Windows users, please run the script in WSL)
    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


Summary by cubic

Fixes a bug in generated jvm-vertx Kotlin clients where an operation parameter with the same name as an ApiClient property (basePath, accessToken, apiKey, apiKeyPrefix, username, password, vertx) shadowed the client's value, causing wrong credentials or URLs to be sent and API key collisions to fail compilation.

All ApiClient member reads in the library template are now this.-qualified. Spec parameters keep their names, so generated signatures are unchanged. A regression test covers basic, bearer, OAuth2, and header and query API key branches; the same shadowing also exists in jvm-volley and is not addressed here.