Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG] [JAVA] Missing imports in okhttp-gson client #14599

Closed
4 of 6 tasks
swanysimon opened this issue Feb 2, 2023 · 14 comments
Closed
4 of 6 tasks

[BUG] [JAVA] Missing imports in okhttp-gson client #14599

swanysimon opened this issue Feb 2, 2023 · 14 comments

Comments

@swanysimon
Copy link

swanysimon commented Feb 2, 2023

Bug Report Checklist

Description

I'm trying to upgrade my Gradle plugin version from 5.4.0 to 6.3.0 and running into an issue with generated enum classes for my API client.

There are a number of missing imports in https://github.com/juanavilactn/openapi-generator/blob/master/modules/openapi-generator/src/main/resources/Java/libraries/okhttp-gson/pojo.mustache. This is the list I get in my IDE that I have to manually add imports for with my enums:

com.google.gson.TypeAdapter
com.google.gson.stream.JsonReader
com.google.gson.stream.JsonWriter
java.io.IOException
openapi-generator version

6.3.0

OpenAPI declaration file content or url
Generation Details
import org.openapitools.generator.gradle.plugin.tasks.GenerateTask

plugins {
    id "java"
    id "org.openapi.generator" version "6.3.0"
}

group = "example.example"
version = "0.0.1"

repositories { mavenCentral() }

java { toolchain { languageVersion.set(JavaLanguageVersion.of(11)) } }

sourceSets { main { java.srcDirs += "${buildDir}/generated/src/main/java" } }

dependencies {
    implementation "com.google.code.gson:gson:2.8.9"
    implementation "com.squareup.okhttp3:logging-interceptor:4.9.3"
    implementation "com.squareup.okhttp3:okhttp:4.9.3"
    implementation "com.squareup.okio:okio:2.8.0"
    implementation "io.gsonfire:gson-fire:1.8.5"
    implementation "io.swagger:swagger-core:1.6.9"
    implementation "javax.annotation:javax.annotation-api:1.3.2"
    implementation "javax.ws.rs:javax.ws.rs-api:2.1.1"
    implementation "org.openapitools:jackson-databind-nullable:0.2.2"
}

def generateSchemaTask = tasks.register("generateJavaClient", GenerateTask) {
    generatorName = "java"
    group = project.group
    version = project.version
    inputSpec = file("openapi.yaml").toString()
    outputDir = "${buildDir}/generated"
    apiPackage = "example.example.api"
    modelPackage = "example.example.api.model"
    configOptions = [
            dateLibrary            : "java8",
            hideGenerationTimestamp: "true",
    ]
}
Steps to reproduce

Try to generate with the above with an enum object. I can work on a minimal example if it would actually be helpful, but it seems pretty straightforward.

Minimal example: https://github.com/swanysimon/java-generator-example-14599

Related issues/PRs

n/a

Suggest a fix

I can make this change myself since it is just adding imports, but this clearly made it through testing and its possible folks want a more thorough fix and/or testing.

@wing328
Copy link
Member

wing328 commented Feb 3, 2023

I can work on a minimal example

please provide one for easier troubleshooting.

@swanysimon
Copy link
Author

swanysimon commented Feb 7, 2023

@wing328 Here's a (not) working example (heh): https://github.com/swanysimon/java-generator-example-14599

Updating the original comment too

@stowns
Copy link

stowns commented Feb 10, 2023

I seem to have the same missing imports. Java11 with dateLibrary=java8 additionalParameter

@wing328
Copy link
Member

wing328 commented Feb 10, 2023

@swanysimon if you change the spec to 3.0.2, it seems to work (there's another issue how the parameter in which int64 should be format instead)

@wing328
Copy link
Member

wing328 commented Feb 10, 2023

@stowns please provide a spec to reproduce the issue.

@swanysimon
Copy link
Author

Thanks for the update @wing328 . 3.0.2 does indeed seem to work, but then I run into issues with a nullable ref a la #5180. Will try to get that working after reading through that thread

@swanysimon
Copy link
Author

Looks like I'm blocked on a problem extremely similar to #14041 or #12556 and cannot upgrade

@stowns
Copy link

stowns commented Feb 13, 2023

ah yes @wing328 using swagger: "2.0"

@ole-v-v
Copy link

ole-v-v commented Feb 15, 2023

My 15 lines example here: https://github.com/ole-v-v/openapi-generator-java-bug-14599-example/blob/main/test.swagger.json
import java.io.IOException; is missing in the generated Test.java file.

@stowns
Copy link

stowns commented Feb 15, 2023

Migrating my schema yaml from swagger 2.0 -> openapi 3.0.2 didn't solve the import issue. Still missing imports for IOException and TypeAdapter in my models using 6.3.0. Unfortunately, I can't share the schema :/ .

@ole-v-v
Copy link

ole-v-v commented Feb 16, 2023

Have you tested with the latest master to confirm the issue still exists? Yes, I have observed the bug on master (up to commit d7edbad from Wed Feb 15 02:12:39 2023 -0500 (inclusive)).

@vanwobe
Copy link
Contributor

vanwobe commented May 2, 2023

above PR should fix this issue of missing imports.

wing328 pushed a commit that referenced this issue May 3, 2023
Co-authored-by: vanwobe <bert.vanwolleghem@ext.ec.europa.eu>
@wing328
Copy link
Member

wing328 commented May 3, 2023

I've tested the fix (#15388) with the spec provided by @ole-v-v and confirmed the fix.

Thanks for the PR, which has just been merged. Please pull the latest master to give it a try.

@wing328 wing328 closed this as completed May 3, 2023
@ole-v-v
Copy link

ole-v-v commented May 15, 2023

In a few tests using the latest from master (up to and including commit b6b8883 from Sun May 14 23:06:20 2023 +0800 inclusive) I was unable to reproduce the bug. Thanks for fixing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants