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] when using discriminator pattern in Kotlin generator the data classes don't implement the Interface #19928

Open
luismospinam opened this issue Oct 21, 2024 · 0 comments

Comments

@luismospinam
Copy link

Description

Related to: #13484
In Kotlin, when using discriminator property, the data/concrete classes don't implement the generated interface.

With the following openapi.yaml spec:

openapi: "3.0.3"
info:
  title: MS Pet API
  version: 1.0.0
  description: De API for testing the discriminator in openapi
paths:
  /api/projects:
    get:
      summary: get a pet
      operationId: getPets
      tags:
        - pets
      responses:
        200:
          description: Succes respons
          content:
            application/json:
              schema:
                type: array
                items:
                  $ref: '#/components/schemas/PetResponse'

components:
  schemas:
    PetResponse:
      oneOf:
        - $ref: '#/components/schemas/Cat'
        - $ref: '#/components/schemas/Dog'
      discriminator:
        propertyName: petType
    Cat:
      type: object
      # all other properties specific to a `Cat`
      required:
        - petType
      properties:
        catName:
          type: string
        petType:
          type: string
    Dog:
      type: object
      # all other properties specific to a `Dog`
      required:
        - petType
      properties:
        bark:
          type: string
        petType:
          type: string

Generates the following classes and interface in Kotlin:

interface PetResponse {
    @Json(name = "petType")
    val petType: kotlin.String

    @Json(name = "catName")
    val catName: kotlin.String?

    @Json(name = "bark")
    val bark: kotlin.String?
}

data class Cat (
    @Json(name = "petType")
    val petType: kotlin.String,

    @Json(name = "catName")
    val catName: kotlin.String? = null
)
data class Dog (
    @Json(name = "petType")
    val petType: kotlin.String,

    @Json(name = "bark")
    val bark: kotlin.String? = null
)

As seen, the data classes (Dog and Cat) don't implement the interface PetResponse, even when using the following property config:

additionalProperties.set(
        mapOf(
            "useOneOfInterfaces" to "true",
        ),
    )

Here's a simple project to demonstrate the issue: https://github.com/luismospinam/Openapi-generator-kotlin-discriminator
run ./gradlew openApiGenerate

@luismospinam luismospinam changed the title [BUG] [Kotlin] when using discriminator pattern the Kotlin generator the data classes don't implement the Interface [BUG] [Kotlin] when using discriminator pattern in Kotlin generator the data classes don't implement the Interface Oct 30, 2024
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

1 participant