Skip to content

Conversation

@shybovycha
Copy link
Contributor

@shybovycha shybovycha commented Sep 18, 2025

Fixes #21991 .

Only register the model annotation imports based on the annotationLibrary property (either from Swagger v1, Swagger v2 or none), as documented in Config Options section of the Java generator docs.

This option is currently being partially ignored for that purpose, since all the imports for the model annotations are registered unconditionally in the org.openapitools.codegen.languages.AbstractJavaCodegen#processOpts method:

importMapping.put("ApiModelProperty", "io.swagger.annotations.ApiModelProperty");
importMapping.put("ApiModel", "io.swagger.annotations.ApiModel");
importMapping.put("Schema", "io.swagger.v3.oas.annotations.media.Schema");

Then each specific code generator might change the said mapping. Specifically, the JavaClientCodegen has the following set of conditions:

if (!AnnotationLibrary.SWAGGER1.equals(getAnnotationLibrary())) {
    // Remove io.swagger.annotations.* imports
    codegenModel.imports.remove("ApiModel");
    codegenModel.imports.remove("ApiModelProperty");
}

if (codegenModel.description != null) {
    if (AnnotationLibrary.SWAGGER2.equals(getAnnotationLibrary())) {
        codegenModel.imports.add("Schema");
    }
}

Note how the Schema import is introduced in the parent class (AbstractJavaCodegen) but never removed in the child class (JavaClientCodegen).

This map (importMapping) is then being added to the imports variable in the org.openapitools.codegen.languages.AbstractJavaCodegen#postProcessAllModels method:

// There must be a better way ...
for (String imp : inheritedImports) {
    String qimp = importMapping().get(imp);
    if (qimp != null) {
        Map<String, String> toAdd = new HashMap<>();
        toAdd.put("import", qimp);
        modelsAttrs.getImports().add(toAdd);
    }
}

This variable is then being rendered in the model templates in pretty much all the Java generators.

Take modules/openapi-generator/src/main/resources/Java/libraries/native/model.mustache for example:

import java.util.Objects;
import java.util.Map;
import java.util.HashMap;
import java.util.Locale;
{{#imports}}
import {{import}};
{{/imports}}
{{#serializableModel}}
import java.io.Serializable;
{{/serializableModel}}

or modules/openapi-generator/src/main/resources/Java/libraries/resttemplate/model.mustache:

import java.util.Objects;
import java.util.Arrays;
import java.util.Locale;
{{#imports}}
import {{import}};
{{/imports}}
{{#serializableModel}}
import java.io.Serializable;
{{/serializableModel}}

Note the following section:

{{#imports}}
import {{import}};
{{/imports}}

This will effectively inject the flattened importMapping map (or rather only its values) in the generated model file.

A simple solution to the issue would be to add an additional condition to the JavaClientGenerator to remove the Schema import for non-Swagger2 annotationLibrary, which this PR is doing.

PR checklist

  • Read the contribution guidelines.
  • Pull Request title clearly describes the work in the pull request and Pull Request description provides details about how to validate the work. Missing information here may result in delayed response from the community.
  • Run the following to build the project and update samples:
    ./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.
  • File the PR against the correct branch: master (upcoming 7.x.0 minor release - breaking changes with fallbacks), 8.0.x (breaking changes without fallbacks)
  • If your PR solves a reported issue, reference it using GitHub's linking syntax (e.g., having "fixes #123" present in the PR description)
  • If your PR is targeting a particular programming language, @mention the technical committee members, so they are more likely to review the pull request.

@shybovycha shybovycha force-pushed the java-client-fix-annotation-library-imports branch from ed0b720 to dcc1521 Compare September 18, 2025 12:47
@shybovycha shybovycha marked this pull request as draft September 18, 2025 12:51
@shybovycha shybovycha force-pushed the java-client-fix-annotation-library-imports branch from 290fee3 to b488012 Compare September 18, 2025 13:07
@shybovycha shybovycha marked this pull request as ready for review September 18, 2025 23:58
@shybovycha
Copy link
Contributor Author

@wing328
Copy link
Member

wing328 commented Sep 19, 2025

thanks for the PR

what about adding a test in https://github.com/OpenAPITools/openapi-generator/blob/master/modules/openapi-generator/src/test/java/org/openapitools/codegen/java/JavaClientCodegenTest.java ?

@shybovycha
Copy link
Contributor Author

@wing328 good point! added the positive and negative scenario test cases

@shybovycha
Copy link
Contributor Author

@wing328 any thoughts on this PR?

@wing328
Copy link
Member

wing328 commented Sep 23, 2025

thanks for the pr

let's give it a try

@wing328 wing328 merged commit fe6da71 into OpenAPITools:master Sep 23, 2025
14 checks passed
rajvesh pushed a commit to rajvesh/openapi-generator that referenced this pull request Dec 25, 2025
* Only register the Swagger annotation imports for the corresponding annotationLibrary

* Fix the issue in a specific generator

* Add unit test
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] [JAVA] Imports in generated models ignore annotationLibrary option

2 participants