Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
5 of 6 tasks
mgallagher-jamf opened this issue Nov 5, 2021 · 8 comments
Open
5 of 6 tasks

Comments

@mgallagher-jamf
Copy link

[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: [email protected]
    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
Copy link
Contributor

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
Copy link
Contributor

Basic PR with steps to reproduce
#12594

@etremblay
Copy link
Contributor

etremblay commented Jun 14, 2022

The pull request is redy for review 🙂

@alexbudarov
Copy link

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

@etremblay
Copy link
Contributor

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
Copy link
Member

wing328 commented Jul 19, 2023

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

@Mkohm
Copy link

Mkohm commented Jul 21, 2023

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
Copy link

This seems to be fixed in 7.0.1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants