-
-
Notifications
You must be signed in to change notification settings - Fork 7.4k
Description
Bug Report Checklist
- Have you provided a full/minimal spec to reproduce the issue?
- Have you validated the input using an OpenAPI validator?
- 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
Currently there is an annotationLibrary config option which is supposed to determine which annotations to use for, among others, generated models.
Regardless of the option value, all the imports are added to the generated models.
In my case, the io.swagger.v3.oas.annotations.media.Schema import, which conflicts with the model class with the same name (Schema).
This could also be presented as a different issue - "imports conflict with the model names", which would require significantly more effort to investigate and properly fix (figuring out which imports exactly might be conflicting and using fully-qualified class names all over the templates).
Setting the annotationLibrary or documentationProvider options to any value (including none) has little effect on the generated code.
These options are documented to provide a certain behaviour but it is not respected by the generators (since the code for import mappings is located in AbstractJavaCodegen class, I assume this might affects all Java generators).
openapi-generator version
7.10.0 and 7.15.0 are both affected
OpenAPI declaration file content or url
type: object
required:
- schemas
additionalProperties: false
properties:
schemas:
type: object
additionalProperties:
$ref: "schema.yml" # anything could be hereGeneration Details
Using Gradle plugin:
import org.openapitools.generator.gradle.plugin.tasks.GenerateTask
plugins {
id 'org.openapi.generator' version '7.15.0
}
ext {
openAPIGeneratorVersion = '7.15.0'
jakartaAnnotationVersion = '2.1.1'
jacksonDatabindNullableVersion = '0.2.6'
}
dependencies {
implementation ("org.openapitools:openapi-generator:$openAPIGeneratorVersion") {
exclude group: 'org.slf4j', module: 'slf4j-simple'
}
implementation "org.openapitools:jackson-databind-nullable:$jacksonDatabindNullableVersion"
implementation "jakarta.annotation:jakarta.annotation-api:$jakartaAnnotationVersion"
}
tasks.register('generateOpenAPI', GenerateTask) {
generatorName = "java"
library = "native"
inputSpec = "$rootDir/api/api-v1.yml"
outputDir = "$projectDir"
ignoreFileOverride = "$projectDir/.openapi-generator-ignore"
apiPackage = "com.myapp.api"
modelPackage = "com.myapp.model"
invokerPackage = "com.myapp.client"
configOptions = [
useJakartaEe: "true",
openApiNullable: "false",
useAbstractionForFiles: "true",
annotationLibrary: "none"
]
globalProperties = [
apis: "",
models: "",
supportingFiles: "",
apiDocs: "false",
apiTests: "false",
modelDocs: "false",
modelTests: "false"
]
}
tasks.named('compileJava') {
dependsOn tasks.named('generateOpenAPI')
}
tasks.withType(ProcessResources).configureEach {
dependsOn 'generateOpenAPI'
}
compileJava {
options.release = 17
sourceCompatibility = "17"
targetCompatibility = "17"
}Steps to reproduce
Trying to build a project with the above configuration results in a compile-time error:
> Task :myapp:compileJava FAILED
/Users/shybovycha/projects/myapp/src/main/java/com/myapp/generated/model/Database.java:158: error: cannot find symbol
joiner.add(getSchemas().get(_key).toUrlQueryString(String.format("%sschemas%s%s", prefix, suffix,
^
symbol: method toUrlQueryString(String)
location: @interface Schema
1 error
Upon inspecting the generated/model/Database.java file with IDE, one might see that the model contains the following definition for the getSchemas() method:
/**
* Get schemas
* @return schemas
*/
@jakarta.annotation.Nonnull
@JsonProperty(JSON_PROPERTY_SCHEMAS)
@JsonInclude(value = JsonInclude.Include.ALWAYS)
public Map<String, Schema> getSchemas() {
return schemas;
}Where Schema class is being referenced from the io.swagger.v3.oas.annotations.media package.
Whereas it should be referenced from the generated Schema model.
This is because the generated/model/Database.java file contains an explicit import statement:
import io.swagger.v3.oas.annotations.media.Schema;Upon removal of the said import, the Schema class is implicitly imported from the same package as the Database class.
Related issues/PRs
Not found
Suggest a fix
Gonna raise the PR with a suggested fix.