OpenAPITools / OpenAPITools/openapi-generator

[BUG] Kolin - Inheritance from more than 1 class using allOf does not work (generated code not compilable)

Open
#20,220 2 comments 0 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

If I decare two parent classes(1) with discriminator and use them both in child class (2) using allOf, the generated kotlin child class has compilation errors.
The result is:

  • both parent interfaces are correctly generated
  • both interfaces have correct @JsonSubTypes annotations
  • child class (the one with multiple allOf) is generated incorrectly:
    -- missing override keyword (which causes compilation error)
    -- inherits only from the first of the parent interfaces

The reason to do this is to share common types in an inheritance hierarchy. In my case I have SimpleFoo and ComplexFoo, with both of them declaring validation errors. Some errors classes may be shared and I dont want to decare them multimple times for each type of Foo.

(1) SimpleFooInvalidReason and ComplexFooInvalidReason in the example
(2) RoundingError in the example

openapi-generator version

7.10

OpenAPI declaration file content or url
openapi: 3.0.3

info:
  version: v0.40
  title: Foo schema
  license:
    name: Apache 2.0
    url: 'http://www.apache.org/licenses/LICENSE-2.0.html'

paths:
  /online/v1/foo/read/simple:
    get:
      operationId: readFooSimple
      responses:
        '200':
          $ref: '#/components/responses/SimpleFooCommandResponse'

  /online/v1/foo/read/complex:
    get:
      operationId: readFooComplex
      responses:
        '200':
          $ref: '#/components/responses/ComplexFooCommandResponse'

components:
  responses:
    SimpleFooCommandResponse:
      content:
        application/json:
          schema:
            $ref: '#/components/schemas/SimpleFooCommandResponse'
      description: Foo

    ComplexFooCommandResponse:
      content:
        application/json:
          schema:
            $ref: '#/components/schemas/ComplexFooCommandResponse'
      description: Foo

  schemas:
    SimpleFooCommandResponse:
      type: object
      properties:
        invalidReasons:
          type: array
          items:
            $ref: '#/components/schemas/SimpleFooInvalidReason'
          default: [ ]
    ComplexFooCommandResponse:
      type: object
      properties:
        invalidReasons:
          type: array
          items:
            $ref: '#/components/schemas/ComplexFooInvalidReason'
          default: [ ]

    SimpleFooInvalidReason:
      type: object
      discriminator:
        propertyName: type
        mapping:
          RoundingError: '#/components/schemas/RoundingError'
          SimpleFooError: '#/components/schemas/SimpleFooError'
      required:
        - type
      properties:
        type:
          type: string
    ComplexFooInvalidReason:
      type: object
      discriminator:
        propertyName: type
        mapping:
          StakeRoundingError: '#/components/schemas/RoundingError'
          ComplexFooError: '#/components/schemas/ComplexFooError'
      required:
        - type
      properties:
        type:
          type: string

    SimpleFooError:
      type: object
      allOf:
        - $ref: '#/components/schemas/SimpleFooInvalidReason'
      properties:
        foo:
          type:
            string
    ComplexFooError:
      type: object
      allOf:
        - $ref: '#/components/schemas/ComplexFooInvalidReason'
      properties:
        foo:
          type: string
    RoundingError:
      type: object
      allOf:
        - $ref: '#/components/schemas/SimpleFooInvalidReason'
        - $ref: '#/components/schemas/ComplexFooInvalidReason'
      properties:
        foo:
          type: string


<plugin>
    <groupId>org.openapitools</groupId>
    <artifactId>openapi-generator-maven-plugin</artifactId>
    <version>${openapi-generator-maven-plugin.version}</version>
    <executions>
        <execution>
            <id>generate-betslip-schema</id>
            <phase>generate-sources</phase>
            <goals>
                <goal>generate</goal>
            </goals>
            <configuration>
               <inputSpecRootDirectory>${project.build.directory}/classes/oas/foo</inputSpecRootDirectory>
                <generatorName>kotlin</generatorName>
                <configOptions>
                    <library>jvm-ktor</library>
                    <serializationLibrary>jackson</serializationLibrary>
                    <modelMutable>true</modelMutable> <!-- true or false, makes no difference in this -->
                    <enumPropertyNaming>UPPERCASE</enumPropertyNaming>
                    <sourceFolder>src/main/kotlin</sourceFolder>
                    <additionalModelTypeAnnotations>@com.fasterxml.jackson.annotation.JsonInclude(com.fasterxml.jackson.annotation.JsonInclude.Include.NON_EMPTY)</additionalModelTypeAnnotations>
                </configOptions>
                <openapiNormalizer>REF_AS_PARENT_IN_ALLOF=true</openapiNormalizer>
                <generateApis>false</generateApis>
                <generateModelTests>false</generateModelTests>
                <generateApiTests>false</generateApiTests>
                <generateSupportingFiles>false</generateSupportingFiles>
                <modelPackage>foo.model</modelPackage>
                <packageName>foo</packageName>
                <additionalProperties>removeEnumValuePrefix=false</additionalProperties>
            </configuration>
        </execution>         
    </executions>
</plugin>
Expected vs actual
@com.fasterxml.jackson.annotation.JsonInclude(com.fasterxml.jackson.annotation.JsonInclude.Include.NON_EMPTY)

data class RoundingError (

    @get:JsonProperty("type")
    var type: kotlin.String

) : SimpleFooInvalidReason {

should be

@com.fasterxml.jackson.annotation.JsonInclude(com.fasterxml.jackson.annotation.JsonInclude.Include.NON_EMPTY)

data class RoundingError (

    @get:JsonProperty("type")
    override var type: kotlin.String

) : SimpleFooInvalidReason, ComplexFooInvalidReason {
Steps to reproduce

mvn clean install

Related issues/PRs
Suggest a fix
  • #isInherited is probably evaluated as false
  • is it possible to have more than one parent?

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 by reproducing the supplied OpenAPI specification with the Kotlin generator and run mvn clean install. Trace the generated RoundingError model and the reported isInherited handling. Done means the generated Kotlin compiles, declares both parent interfaces, and marks the shared type property with override.

Written by the indexing model from the issue text.

Assessment

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

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.