Skip to content
GitLab
Projects Groups Snippets
  • /
  • Help
    • Help
    • Support
    • Community forum
    • Submit feedback
    • Contribute to GitLab
  • Sign in / Register
  • O openapi-generator
  • Project information
    • Project information
    • Activity
    • Labels
    • Members
  • Repository
    • Repository
    • Files
    • Commits
    • Branches
    • Tags
    • Contributors
    • Graph
    • Compare
  • Issues 3,476
    • Issues 3,476
    • List
    • Boards
    • Service Desk
    • Milestones
  • Merge requests 402
    • Merge requests 402
  • CI/CD
    • CI/CD
    • Pipelines
    • Jobs
    • Schedules
  • Deployments
    • Deployments
    • Environments
    • Releases
  • Packages and registries
    • Packages and registries
    • Package Registry
    • Infrastructure Registry
  • Monitor
    • Monitor
    • Incidents
  • Analytics
    • Analytics
    • Value stream
    • CI/CD
    • Repository
  • Wiki
    • Wiki
  • Snippets
    • Snippets
  • Activity
  • Graph
  • Create a new issue
  • Jobs
  • Commits
  • Issue Boards
Collapse sidebar
  • OpenAPI Tools
  • openapi-generator
  • Issues
  • #14949
Closed
Open
Issue created Mar 14, 2023 by Philip Graf@panphg5 of 6 checklist items completed5/6 checklist items

[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

modules/openapi-generator/src/test/resources/3_0/petstore-multiple-required-properties-has-same-oneOf-object.yaml

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 = ...).

Assignee
Assign to
Time tracking