OpenAPITools / openapi-generator

OpenAPI Generator allows generation of API client libraries (SDK generation), server stubs, documentation and configuration automatically given an OpenAPI Spec (v2, v3)
https://openapi-generator.tech
Apache License 2.0
21.43k stars 6.48k forks source link

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

Open mgallagher-jamf opened 2 years ago

mgallagher-jamf commented 2 years ago

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

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

etremblay commented 2 years ago

We have the same issue.

With some debugging, I see that isMap is true on the template even if the schema is not a map but a plain object.

https://github.com/OpenAPITools/openapi-generator/blob/master/modules/openapi-generator/src/main/resources/kotlin-client/data_class.mustache#L63

When we remove the type: object the generated code is valid again.

I will try to submit a pull request.

etremblay commented 2 years ago

Basic PR with steps to reproduce https://github.com/OpenAPITools/openapi-generator/pull/12594

etremblay commented 2 years ago

The pull request is redy for review :slightly_smiling_face:

alexbudarov commented 1 year ago

Was the PR merged? I met the same problem with openapi-generator-maven-plugin 6.4.0.

etremblay commented 1 year ago

Was the PR merged? I met the same problem with openapi-generator-maven-plugin 6.4.0.

The pr was only for the kotlin generator

wing328 commented 1 year ago

@alexbudarov can you please give it a try with the latest master to see if you still experience the same issue?

Mkohm commented 1 year ago

According to https://github.com/OpenAPITools/openapi-generator/blob/master/docs/generators/kotlin.md#schema-support-feature "allOff" feature is not yet supported. If PR #12594 fixes that, please update the documentation 👍 However, I get wrong generated code using "allOff", but it might be caused by some other issue.

enekonieto commented 11 months ago

This seems to be fixed in 7.0.1