OpenAPITools / OpenAPITools/openapi-generator

[BUG][Client][Java 21][WebClient] Generated code contains warnings on java 21

Open
#17,475 1 comment 5 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Issue: Bug
Dominant language
Java
Stars
26.8k
Forks
7.7k
PR merge metrics
PR metrics pending

Description

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

Generator generates code with this-escape warnings in ApiClient.
If you add -Xlint:all, -Werror to java compiler options, you will get error on any open-api specification.

Java compiler logs
...\project\build\java\src\main\java\org\openapitools\example\invoker\ApiClient.java:103: warning: [this-escape] possible 'this' escape before subclass is fully initialized
        this.init();
                 ^
...\project\build\java\src\main\java\org\openapitools\example\invoker\ApiClient.java:107: warning: [this-escape] possible 'this' escape before subclass is fully initialized
        this(Optional.ofNullable(webClient).orElseGet(() -> buildWebClient()), createDefaultDateFormat());
            ^
...\project\build\java\src\main\java\org\openapitools\example\invoker\ApiClient.java:122: warning: [this-escape] previous possible 'this' escape happens here via invocation
        this.init();
                 ^
...\project\build\java\src\main\java\org\openapitools\example\invoker\ApiClient.java:111: warning: [this-escape] possible 'this' escape before subclass is fully initialized
        this(buildWebClient(mapper.copy()), format);
            ^
...\project\build\java\src\main\java\org\openapitools\example\invoker\ApiClient.java:122: warning: [this-escape] previous possible 'this' escape happens here via invocation
        this.init();
                 ^
...\project\build\java\src\main\java\org\openapitools\example\invoker\ApiClient.java:115: warning: [this-escape] possible 'this' escape before subclass is fully initialized
        this(Optional.ofNullable(webClient).orElseGet(() -> buildWebClient(mapper.copy())), format);
            ^
...\project\build\java\src\main\java\org\openapitools\example\invoker\ApiClient.java:122: warning: [this-escape] previous possible 'this' escape happens here via invocation
        this.init();
                 ^
openapi-generator version

v7.2.0

OpenAPI declaration file content or URL

https://github.com/OpenAPITools/openapi-generator/blob/1105759a6f20edac389fa547359c7d5e6c4f9ac8/modules/openapi-generator-gradle-plugin/samples/local-spec/petstore-v3.0.yaml

Information to reproduce

jdk: Oracle OpenJDK 21.0.1
gradle: 8.5
open-api generator plugin: 7.2.0

build.gradle.kts
plugins {
    id("java")
    id("org.openapi.generator") version "7.2.0"
}

group = "test.group"
version = "1.0"

repositories {
    mavenCentral()
}

dependencies {
    implementation("org.openapitools:jackson-databind-nullable:0.2.6")
    implementation("com.fasterxml.jackson.core:jackson-annotations:2.16.1")
    implementation("com.fasterxml.jackson.core:jackson-core:2.16.1")
    implementation("com.fasterxml.jackson.core:jackson-databind:2.16.1")
    implementation("org.springframework.boot:spring-boot-starter-webflux:3.2.1")
    testImplementation(platform("org.junit:junit-bom:5.9.1"))
    testImplementation("org.junit.jupiter:junit-jupiter")
}

sourceSets {
    named("main") {
        java {
            srcDir("$buildDir/java/src/main/java")
        }
    }
}

tasks.test {
    useJUnitPlatform()
}

openApiGenerate {
    generatorName = "java"
    library = "webclient"
    inputSpec = "$projectDir/src/main/openapi/petstore-v3.0.yaml"
    outputDir = "$buildDir/java"
    apiPackage = "org.openapitools.example.api"
    invokerPackage = "org.openapitools.example.invoker"
    modelPackage = "org.openapitools.example.model"
    configOptions.putAll(mapOf(
        "dateLibrary" to "java8",
        "serializationLibrary" to "jackson",
        "useJakartaEe" to "true",
        "hideGenerationTimestamp" to "true"
    ))
    globalProperties.putAll(mapOf(
        "apiTests" to "false",
        "modelDocs" to "false"
    ))
    skipValidateSpec = true
    logToStderr = true
    generateAliasAsModel = false
    enablePostProcessFile = false
}

tasks.withType<JavaCompile>() {
    options.compilerArgs.addAll(listOf("-Xlint:all", "-Werror"))
}
Suggest a fix

Change init method in class ApiClient

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 by reproducing the issue with the supplied petstore-v3.0.yaml, Java 21, the WebClient library, and the Gradle compiler flags. Locate the generator source for the generated ApiClient and inspect the init-related constructors; done means the generated project compiles with -Xlint:all and -Werror without the reported this-escape warnings.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, openapi, spring
Domain
api, tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.