OpenAPITools / OpenAPITools/openapi-generator

[BUG] Kotlin generator produces invalid code when allOf is used

Open
#10,792 9 comments 6 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] Kotlin generator produces invalid code when allOf is used

  • 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 generator produces invalid code when using allOf in the Spec

Actual

data class Bird (

    @field:JsonProperty("id")
    override val id: java.util.UUID,

    @field:JsonProperty("featherType")
    val featherType: kotlin.String

) : Animal()

Expected

data class Bird (

    @field:JsonProperty("id")
    override val id: java.util.UUID,

    @field:JsonProperty("featherType")
    val featherType: kotlin.String

) : Animal

The generated code for the class Bird is attempting to call the constructor on the Animal interface it is implementing. Kotlin interfaces do not have constructors.

openapi-generator version

The bug exists using 5.3.0, the issue is not present in 5.2.1

OpenAPI declaration file content or url
openapi: 3.0.1
info:
  title: Example
  description: An example
  version: '0.1'
  contact:
    email: contact@example.org
    url: 'https://example.org'
servers:
  - url: http://example.org
tags:
  - name: bird
paths:
  '/v1/bird/{id}':
    get:
      tags:
        - bird
      responses:
        '200':
          description: OK
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/bird'
      operationId: get-bird
    parameters:
      - schema:
          type: string
          format: uuid
        name: id
        in: path
        required: true
components:
  schemas:
    animal:
      title: An animal
      type: object
      properties:
        id:
          type: string
          format: uuid
      required:
        - id
      discriminator:
        propertyName: type
        mapping:
          BIRD: '#/components/schemas/bird'
    bird:
      title: A bird
      type: object
      allOf:
        - $ref: '#/components/schemas/animal'
        - properties:
            featherType:
              type: string
          required:
            - featherType
Generation Details

Use openapi-generator-cli version 5.3.0

#!/bin/sh

openapi-generator-cli generate \
--generator-name kotlin \
--input-spec schema.yaml \
--output output \
--config config.yaml \
--template-dir templates \
--ignore-file-override .openapi-generator-ignore

All supporting files (including this script) are contained in the attached file issue.zip

Steps to reproduce
  1. unzip issue.zip
  2. cd issue; ./generate.sh
  3. Observe the generated file
    • output/generated/com/example/api/models/Bird.kt
Related issues/PRs

N/A

Suggest a fix

The bug exists in 5.3.0, the issue is not present in 5.2.1

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 attached issue.zip, inspect schema.yaml and run generate.sh with openapi-generator-cli 5.3.0. Compare output/generated/com/example/api/models/Bird.kt with the expected Kotlin output, confirming that generation for the allOf example no longer attempts to call a constructor on the Animal interface.

Written by the indexing model from the issue text.

Assessment

Tech stack
kotlin
Domain
tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.