Skip to content

[BUG] Kotlin: type aliases don't work #2574

@hosswald

Description

@hosswald

Bug Report Checklist

  • Have you provided a full/minimal spec to reproduce the issue?
  • Have you validated the input using an OpenAPI validator (example)?
  • What's the version of OpenAPI Generator used?
  • Have you search for related issues/PRs?
  • What's the actual output vs expected output?
  • [Optional] Bounty to sponsor the fix (example)
Description

The Kotlin generator can't handle type aliases properly, instead generates empty data classes that don't compile.

openapi-generator version

4.0.0-beta2

OpenAPI declaration file content or url
openapi: "3.0.1"
info:
  version: 0.0.1
  title: broken API
paths:
  /test:
    put:
      summary: No description
      requestBody:
        content:
          application/json:
            schema:
              $ref: '#/components/schemas/MyParameter'
      responses:
        200:
          description: "Lorem Ipsum"
components:
  schemas:
    MyParameter:
      type: object
      properties:
        aDescriptionField:
          $ref: '#/components/schemas/MyParameterTextField'
    MyParameterTextField:
      $ref: '#/components/schemas/MyTypeAlias'
    MyTypeAlias:
      type: string
      minLength: 1
      maxLength: 50
Command line used for generation

java -jar openapi-generator-cli-4.0.0-beta2.jar generate -i brokenApi.yaml -g kotlin -o out

Steps to reproduce
  • Run above command line
  • Go to out\src\main\kotlin\org\openapitools\client\models
  • There are two files, MyParameter.kt and MyParameterTextField.kt, open them in an editor
data class MyParameter (
    val aDescriptionField: kotlin.String? = null // should be of type MyParameterTextField?, not String
){}
data class MyParameterTextField () {} //wrong and won't compile
Suggest a fix

This is a no-brainer imo:

typealias MyParameterTextField = MyTypeAlias

For the entirely missingMyTypeAlias.kt there are multiple options:

// Option 1:
typealias MyTypeAlias = String
// Bonus (similar for Option 2&3):
fun MyTypeAlias.validate() = this.length >= 1 && this.length<=50
// ... and invocation (there needs to be some kind of interface containing validate()):
data class MyParameter (val aDescriptionField: kotlin.MyParameterTextField? = null){ 
    init{ aDescriptionField?.validate().let{/*throw maybe?*/} }
}
// Option 2 (more type safety, more overhead):
inline class MyTypeAlias(val value: String){
// ... Bonus as member function
}
// Option 3 (more type safety, most overhead, most extensible):
data class MyTypeAlias(val value: String){
// ... Bonus as member function
}

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions