OpenAPITools / OpenAPITools/openapi-generator

[BUG][JavaSpring] Suppress unused import warnings

Open
#12,576 3 comments 0 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

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"})

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 with api.mustache, apiController.mustache, apiDelegate.mustache, and model.mustache in the JavaSpring generator, using the Petstore specification and the generation details to reproduce the output. Compare the generated Java files with the proposed fix in pull request 12578; done means the generated sources no longer produce unused-import warnings.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, spring
Domain
api, backend, tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.