[BUG][kotlin-spring] Schema with discriminator mapping produces uncompilable code
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 kotlin-spring generator produces uncompilable code for a schema using a discriminator mapping. The generated interface contains the non-existing annotation attribute requiredMode
for its properties:
...
interface Pet{
@Schema(example = "null", requiredMode = Schema.RequiredMode.REQUIRED, description = "")
val attr: Pet.Attr
...
Instead it should use the annotation attribute required = true
and the annotation target @get:
:
...
interface Pet{
@get:Schema(example = "null", required = true, description = "")
val attr: Pet.Attr
...
openapi-generator version
6.4.0
OpenAPI declaration file content or url
The snippet from the above declaration that produces the uncompilable code:
pet:
oneOf:
- $ref: '#/components/schemas/dog'
- $ref: '#/components/schemas/cat'
discriminator:
propertyName: attr
mapping:
DOG: '#/components/schemas/dog'
CAT: '#/components/schemas/cat'
Generation Details
I'm using the openapi-generator-gradle-plugin 6.4.0 to generate the model:
tasks.create("openApiGenerate", org.openapitools.generator.gradle.plugin.tasks.GenerateTask::class) {
inputSpec.set(project.projectDir.resolve("src/main/resources/META-INF/openapi.yaml").absolutePath)
outputDir.set(project.buildDir.resolve("openapi").absolutePath)
modelPackage.set("org.example")
generatorName.set("kotlin-spring")
globalProperties.put("apis", "false")
globalProperties.put("invokers", "false")
globalProperties.put("models", "")
}
Steps to reproduce
The following test, added to KotlinSpringServerCodegenTest, reproduces the issue:
@Test
public void modelWithDiscriminatorMappingShouldCompile() throws Exception {
String baseModelPackage = "zz";
File output = Files.createTempDirectory("test").toFile().getCanonicalFile();
OpenAPI openAPI = TestUtils.parseFlattenSpec("src/test/resources/3_0/petstore-multiple-required-properties-has-same-oneOf-object.yaml");
KotlinSpringServerCodegen codegen = new KotlinSpringServerCodegen();
codegen.setOutputDir(output.getAbsolutePath());
codegen.additionalProperties().put(CodegenConstants.MODEL_PACKAGE, baseModelPackage + ".yyyy.model.xxxx");
ClientOptInput input = new ClientOptInput();
input.openAPI(openAPI);
input.config(codegen);
DefaultGenerator generator = new DefaultGenerator();
generator.opts(input).generate();
File resultSourcePath = new File(output, "src/main/kotlin");
File outputModel = Files.createTempDirectory("test").toFile().getCanonicalFile();
FileUtils.copyDirectory(new File(resultSourcePath, baseModelPackage), new File(outputModel, baseModelPackage));
//no exception
KotlinTestUtils.buildModule(Collections.singletonList(outputModel.getAbsolutePath()), Thread.currentThread().getContextClassLoader());
}
Related issues/PRs
I did not find any related issues.
Suggest a fix
The templates modules/openapi-generator/src/main/resources/kotlin-spring/interfaceOptVar.mustache and modules/openapi-generator/src/main/resources/kotlin-spring/interfaceReqVar.mustache should both be changed to generate the annotation @get:Schema(example = ..., required = true, description = ...)
.