openapi-generator icon indicating copy to clipboard operation
openapi-generator copied to clipboard

[BUG] [KOTLIN] simple data type of arrays is lost on implementing classes or discriminator interfaces

Open toggm opened this issue 1 year ago • 1 comments

Bug Report Checklist

  • [x] Have you provided a full/minimal spec to reproduce the issue?
  • [x] Have you validated the input using an OpenAPI validator (example)?
  • [x] Have you tested with the latest master to confirm the issue still exists?
  • [x] Have you searched for related issues/PRs?
  • [x] What's the actual output vs expected output?
  • [ ] [Optional] Sponsorship to speed up the bug fix or feature request (example)
Description

When generating data classes of an interface declaring properties having an array of primitive types, the resulting type of the list resolves to kotlin.Any and the code doesn't compile. Same happens for discriminator interfaces.

openapi-generator version

7.5.0

OpenAPI declaration file content or url

https://gist.github.com/toggm/f8ea4f221a49ccb34b082fe1585b78b3

Generation Details
openapi-generator-cli 7.5.0
  commit : cce4139
  built  : -999999999-01-01T00:00:00+18:00
  source : https://github.com/openapitools/openapi-generator
  docs   : https://openapi-generator.tech/

Generated data class or interface is ok:

data class Interface(

    @Schema(example = "null", description = "")
    @get:JsonProperty("stringArrayProperty") val stringArrayProperty: kotlin.collections.List<kotlin.String>? = null,

    @Schema(example = "null", description = "")
    @get:JsonProperty("booleanArrayProperty") val booleanArrayProperty: kotlin.collections.List<kotlin.Boolean>? = null,

    @field:Valid
    @Schema(example = "null", description = "")
    @get:JsonProperty("objectArrayProperty") val objectArrayProperty: kotlin.collections.List<MyObject>? = null,

    @Schema(example = "null", description = "")
    @get:JsonProperty("type") val type: kotlin.String? = null

Generated data class for MyImpl lost type information:

data class MyImpl1(

    @field:Valid
    @Schema(example = "null", description = "")
    @get:JsonProperty("stringArrayProperty") val stringArrayProperty: kotlin.collections.List<kotlin.Any>? = null,

    @field:Valid
    @Schema(example = "null", description = "")
    @get:JsonProperty("booleanArrayProperty") val booleanArrayProperty: kotlin.collections.List<kotlin.Any>? = null,

    @field:Valid
    @Schema(example = "null", description = "")
    @get:JsonProperty("objectArrayProperty") val objectArrayProperty: kotlin.collections.List<MyObject>? = null,

Same for discriminator interface:

@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, include = JsonTypeInfo.As.PROPERTY, property = "type", visible = true)
@JsonSubTypes(
      JsonSubTypes.Type(value = MyImpl1::class, name = "impl1")
)

interface DiscriminatorInterface{
                @get:Schema(example = "null", description = "")
        val title: kotlin.String?

                @get:Schema(example = "null", description = "")
        val stringArrayProperty: kotlin.collections.List<kotlin.Any>?

                @get:Schema(example = "null", description = "")
        val booleanArrayProperty: kotlin.collections.List<kotlin.Any>?

                @get:Schema(example = "null", description = "")
        val objectArrayProperty: kotlin.collections.List<MyObject>?

                @get:Schema(example = "null", description = "")
        val type: kotlin.String?
}

Steps to reproduce
Related issues/PRs
Suggest a fix

Didn't find workaround so far.

toggm avatar Apr 23 '24 14:04 toggm

Found the following workaround: Define a component for primitive types and reference them in the item array seems to generate the correct data classes for kotlin. Didn't check for other languages.

openapi: 3.1.0
info:
  version: '1'
  title: Minimal example
components:
  schemas:
    String:
      type: string
    Boolean:
      type: boolean
    MyObject:
      type: object
      properties:
        name:
          type: string
    Interface:
      type: object
      properties:
        stringArrayProperty:
          type: array
          items: 
            $ref: '#/components/schemas/String'
        booleanArrayProperty:
          type: array
          items: 
            $ref: '#/components/schemas/Boolean'
        objectArrayProperty:
          type: array
          items:
            $ref: '#/components/schemas/MyObject'
        type:
          type: string
    MyImpl1:
      type: object
      allOf:
        - $ref: '#/components/schemas/Interface'
    DiscriminatorInterface:
      oneOf:
        - $ref: '#/components/schemas/MyImpl1'
      discriminator:
        propertyName: type
        mapping:
          impl1: '#/components/schemas/MyImpl1'
paths:
  /myapi:
    put:
      responses:
        '200':
          description: Sample request
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/DiscriminatorInterface'                

toggm avatar Apr 24 '24 17:04 toggm