OpenAPITools / OpenAPITools/openapi-generator

[BUG] [Kotlin] Object inheritance with discriminator object generates invalid class hierarchy

Open
#16,946 2 comments 5 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

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

Description

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

When putting the discriminator object (e.g. enum) on the base type and using oneOf to specify any of the allowed sub types, openapi generator generates an property in the parent but attempts to override it in the sub types with a non-existing embedded local one. This results in a compilation error, as the following sub type is being generated:

data class Subtype1(

    @get:JsonProperty("subtype1Property") val subtype1Property: kotlin.String? = null,

    @get:JsonProperty("commonProperty") override val commonProperty: kotlin.String? = null,

    @get:JsonProperty("solutionType") override val solutionType: Subtype1.SolutionType? = null
) : BaseObject

So, it attempts to override the solutionType but instead of using the property of BaseObject, it assumes it to be a local class. The actual type is in the interface though (annotation omitted):

interface BaseObject {
    val commonProperty: kotlin.String?

    val solutionType: BaseObject.SolutionType?

    enum class SolutionType(val value: kotlin.String) {

        @JsonProperty("Subtype1")
        SUBTYPE1("Subtype1"),

        @JsonProperty("Subtype2")
        SUBTYPE2("Subtype2"),

        @JsonProperty("Subtype3")
        SUBTYPE3("Subtype3")
    }
}
openapi-generator version

7.0.1

OpenAPI declaration file content or url

https://gist.github.com/DennisVredeveld/68b143d22d64cdbb74221b5ad5fe6fd2

Generation Details

The following config has been used:

 <plugin>
                <groupId>org.openapitools</groupId>
                <artifactId>openapi-generator-maven-plugin</artifactId>
                <version>7.0.1</version>
                <executions>
                    <execution>
                        <id>generated-simple</id>
                        <goals>
                            <goal>generate</goal>
                        </goals>
                        <configuration>
                            <inputSpec>
                                ${basedir}/src/main/resources/example.json
                            </inputSpec>
                            <generatorName>kotlin-spring</generatorName>
                            <generateSupportingFiles>false</generateSupportingFiles>
                            <generateApis>false</generateApis>
                            <generateApiTests>false</generateApiTests>
                            <generateModels>true</generateModels>
                            <generateModelTests>false</generateModelTests>
                            <library>spring-boot</library>
                            <configOptions>
                                <sourceFolder>./</sourceFolder>
                                <interfaceOnly>true</interfaceOnly>
                                <useTags>true</useTags>
                                <packageName>com.example.simple.generated</packageName>
                                <enumPropertyNaming>UPPERCASE</enumPropertyNaming>
                                <annotationLibrary>none</annotationLibrary>
                                <documentationProvider>none</documentationProvider>
                                <swaggerAnnotations>false</swaggerAnnotations>
                                <useSpringBoot3>true</useSpringBoot3>
                                <reactive>true</reactive>
                            </configOptions>
                        </configuration>
                    </execution>
                </executions>
            </plugin>
Steps to reproduce

Perform a mvn clean compile with the configuration above.

Related issues/PRs

None found

Suggest a fix

In the subtype, the discriminator object in defined like this:

override val solutionType: Object1.SolutionType

Instead, this should be:

override val solutionType: BaseObject.SolutionType

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

Start with the kotlin-spring generator and reproduce the issue using the linked OpenAPI declaration, Maven configuration, and mvn clean compile. Compare the generated subtype and base interface shown in the report; done means the subtype refers to BaseObject.SolutionType and the generated models compile successfully.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.