OpenAPITools / openapi-generator

OpenAPI Generator allows generation of API client libraries (SDK generation), server stubs, documentation and configuration automatically given an OpenAPI Spec (v2, v3)
https://openapi-generator.tech
Apache License 2.0
21.84k stars 6.59k forks source link

[BUG][JavaSpring] Suppress unused import warnings #12576

Open Service84Tyler opened 2 years ago

Service84Tyler commented 2 years ago

Bug Report Checklist

Description

The files generated from JavaSpring api.mustache, apiController.mustache, apiDelegate.mustache, and model.mustache have unused imports. I generate the API stubs on compile so I can't manually remove unused imports, because they will just come back on the next compile. I am also using STS/eclipse and I am striving for a spotless build so the unused import warnings are a nuisance and potentially hide important warnings.

openapi-generator version

I am using 4.2.3, and 6.0, This also appears in master. I don't believe this is a regression

Version 6.0.0 Generator: spring Library: spring-boot Gradle: 6.9 Java version: 11

OpenAPI declaration file content or url

https://petstore.swagger.io/v2/swagger.json

Generation Details
plugins {
  id 'org.springframework.boot' version '2.7.0'
  id 'io.spring.dependency-management' version '1.0.9.RELEASE'
  id 'org.openapi.generator' version '6.0.0'
  id 'java'
}

apply plugin: 'java'

def jarName = 'petstore-service'
def version = '0.5.0-SNAPSHOT'
def coreName = jarName.split('-')[0]

jar {
  archiveBaseName = jarName
  archiveVersion = version
  enabled = true
}

sourceCompatibility = 11.0

sourceSets {
  main {
    java {
      srcDir 'gen/main/java'
    }
  }
}

repositories {
  mavenLocal()
  mavenCentral()
}

configurations.all {
  exclude module: 'slf4j-jdk14'
}

dependencies {
  implementation 'org.springframework.boot:spring-boot-starter-web'

  implementation 'org.openapitools:jackson-databind-nullable:0.2.2'
  implementation 'org.springdoc:springdoc-openapi-ui:1.6.8'

  ////////////////////////////////
  //Test Dependencies/////////////
  ////////////////////////////////
  testImplementation 'org.springframework.boot:spring-boot-starter-test'
}

test {
  useJUnitPlatform()
}

////////////////////////////////
//API Stubs/////////////////////
////////////////////////////////
task deleteAPIStubs(type: Delete) {
  delete "gen/main/java/io/service84/services/${coreName}"
}

task generateAPIStubs(type: org.openapitools.generator.gradle.plugin.tasks.GenerateTask) {
  inputSpec = "$rootDir/src/main/resources/${coreName}.yaml"
  outputDir = "$rootDir"
  generatorName = 'spring'
  library = 'spring-boot'
  apiPackage = "io.service84.services.${coreName}.api"
  modelPackage = "io.service84.services.${coreName}.dto"
  modelNameSuffix = 'DTO'
  configOptions = [
    delegatePattern: 'True',
    useTags: 'True',
    hideGenerationTimestamp: 'True',
    dateLibrary: 'java8',
    sourceFolder: 'gen/main/java'
  ]
  typeMappings = [
    OffsetDateTime: 'java.time.LocalDateTime'
  ]
}

generateAPIStubs.dependsOn deleteAPIStubs

compileJava.dependsOn generateAPIStubs

Then import into STS/eclipse and you will see the many unused import warnings

Related issues/PRs

I will propose with the suggested fix

12578

Suggest a fix

Add @SuppressWarnings(value = {"unused"})

Service84Tyler commented 2 years ago

The expected output is to generate files that do not cause IDE warnings The actual output is java files that include unused imports which cause IDE warnings

foxpluto commented 2 years ago

I bump the issue, so many warning make the code really difficult to read.

Regards, S.

FieteO commented 1 year ago

I gave the problem a shot in #13650 and #13582 but encountered some edge cases