OpenAPITools / OpenAPITools/openapi-generator

[KOTLIN] code generation fails on discriminator enum

Open
#8,993 0 comments 2 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Java
Stars
26.8k
Forks
7.7k
PR merge metrics
PR metrics pending

Description

Description

Error while generating model for Kotlin.

For the swagger definition below, we get the following two generated classes:

Instruction.kt:

@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = "instructionType", visible = true)
@JsonSubTypes(
    JsonSubTypes.Type(value = SomeInstruction::class, name = "SOME"),
)
interface Instruction {
    @get:JsonProperty("instructionType")
    val instructionType: Instruction.InstructionType?

    enum class InstructionType(val value: kotlin.String){
        @JsonProperty(value = "SOME") sOME("SOME");
    }
}

Some.kt:

data class SomeInstruction (
    @field:JsonProperty("instructionType")
    override val instructionType: SomeInstruction.InstructionType? = null,
)

However, instead of SomeInstruction.InstructionType in SomeInstruction.kt, we would expect Instruction, since the reference SomeInstruction.InstructionType does not exist.

openapi-generator version

5.0.1

OpenAPI declaration file content or url
openapi: 3.0.1
components:
  schemas:
    SomeInstruction:
      title: SomeInstruction
      type: object
      allOf:
      - $ref: "#/components/schemas/Instruction"
    Instruction:
      title: Instruction
      type: object
      properties:
        instructionType:
          type: string
          description: Type of instruction
          enum:
          - SOME
      discriminator:
        propertyName: instructionType
        mapping:
          SOME: "#/components/schemas/SomeInstruction"
Command line used for generation

Not sure about the command line, but this is part of the pom.xml:

                    <execution>
                        <id>generate-openapi</id>
                        <goals>
                            <goal>generate</goal>
                        </goals>
                        <configuration>
                            <inputSpec>${basedir}/src/main/swagger/input-models/something/swagger.yaml</inputSpec>
                            <generatorName>kotlin</generatorName>

                            <output>${project.build.directory}/generated-sources</output>
                            <modelPackage>somepackage</modelPackage>

                            <!-- Disable if when swagger file does not comply with spec -->
                            <skipValidateSpec>true</skipValidateSpec>

                            <!-- Only generate models, disable generation of RestClient and API classes -->
                            <configOptions>
                                <serializationLibrary>jackson</serializationLibrary>
                            </configOptions>
                            <generateModelTests>false</generateModelTests>
                            <generateApis>false</generateApis>
                            <generateApiDocumentation>false</generateApiDocumentation>
                            <generateModelDocumentation>false</generateModelDocumentation>
                            <generateSupportingFiles>false</generateSupportingFiles>
                        </configuration>
                    </execution>
Steps to reproduce

Generate using Maven.

Related issues/PRs

I checked to see a related issue, I could not find it.

Suggest a fix/enhancement

The template needs to refer to the overarching class/interface that contains the discriminator enum instead of the current class.

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

Run the Maven generation configured in pom.xml with the supplied OpenAPI YAML, then compare Instruction.kt and Some.kt. Trace how the Kotlin generator resolves the discriminator enum reference; done means SomeInstruction uses the enum declared by Instruction rather than a nonexistent nested type.

Written by the indexing model from the issue text.

Assessment

Tech stack
kotlin
Domain
build-system, tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.