swagger-api / swagger-api/swagger-codegen-generators

Kotlin inheritance is broken when using Enums

Open
#325 1 comment 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Mustache
Stars
299
Forks
439
PR merge metrics
No merged PRs in 30d

Description

swagger-core currently generates enums inlined. I've opened a corresponding bug to be able to pull these classes out into their own enum referenced object which seems to be supported in OAS3, https://github.com/swagger-api/swagger-core/issues/3124.

Either way, the kotlin-client generator is currently generating non-compilable Kotlin code.

For example, this is what's derived from swagger-core:

{
  "openapi" : "3.0.1",
  "info" : {
    "title" : "Test",
    "contact" : {
      "email" : "support@test.com"
    },
    "version" : "1.0"
  },
  "paths" : {},
  "components" : {
    "schemas" : {
      "Parent" : {
        "type" : "object",
        "properties" : {
          "enumeration" : {
            "type" : "string",
            "enum" : [ "TEST" ]
          }
        }
      },
      "Child" : {
        "type" : "object",
        "properties" : {},
        "allOf" : [ {
          "$ref" : "#/components/schemas/Parent"
        } ]
      }
    }
  }
}

When generating the Kotlin model classes with swagger-codegen, this generates exploded classes because Kotlin data classes cannot be inherited. The issue is the Child's reference to the Enumeration class which does not exist.

Parent.kt

/**
 * 
 * @param enumeration 
 */
data class Parent (

    val enumeration: Parent.Enumeration? = null
) {
    /**
    * 
    * Values: TEST
    */
    enum class Enumeration(val value: kotlin.String){
        TEST("TEST");
    }
}

Child.kt

/**
 * 
 */
data class Child (

    val enumeration: Child.Enumeration? = null
) {
}

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 kotlin-client generator and compare the generated Parent.kt and Child.kt models shown in the issue. Reproduce the schema, trace how inherited properties and inline enums are rendered, and verify that the generated Kotlin classes compile without an unresolved Child.Enumeration reference.

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
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.