OpenAPITools / OpenAPITools/openapi-generator

[BUG] [Kotlin] when using discriminator pattern in Kotlin generator the data classes don't implement the Interface

Open
#19,928 0 comments 10 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

Description

Related to: https://github.com/OpenAPITools/openapi-generator/issues/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

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 supplied openapi.yaml example and run ./gradlew openApiGenerate in the linked demonstration project, including useOneOfInterfaces=true. Trace the Kotlin generator output for PetResponse, Cat, and Dog; done means the generated Cat and Dog data classes implement the generated PetResponse interface.

Written by the indexing model from the issue text.

Assessment

Tech stack
kotlin, openapi
Domain
api, tooling
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.