OpenAPITools / openapi-generator

OpenAPI Generator allows generation of API client libraries (SDK generation), server stubs, documentation and configuration automatically given an OpenAPI Spec (v2, v3)
https://openapi-generator.tech
Apache License 2.0
21.29k stars 6.44k forks source link

[BUG] [Kotlin] Unusable model generated for `anyOf` with required fields #18626

Open david-kubecka opened 4 months ago

david-kubecka commented 4 months ago

Bug Report Checklist

Description

When a type in an anyOf directive contains a required field then the generated class makes that field also required even though that particular type may not be present in the data.

As as example, take the enclosed Pets model from the official OpenAPI guide. The generated kotlin class is as follows:

data class PetsPatchRequest (

    @Json(name = "age")
    val age: kotlin.Int,

    @Json(name = "pet_type")
    val petType: PetsPatchRequest.PetType,

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

    @Json(name = "hunts")
    val hunts: kotlin.Boolean? = null

) {

    /**
     * 
     *
     * Values: Cat,Dog
     */
    @JsonClass(generateAdapter = false)
    enum class PetType(val value: kotlin.String) {
        @Json(name = "Cat") Cat("Cat"),
        @Json(name = "Dog") Dog("Dog");
    }
}

With this class it is impossible to construct an instance for the valid model {"age": 3} because the petType field (from the other anyOf type) is missing.

openapi-generator version

Both 7.5.0 and latest (master).

OpenAPI declaration file content or url
openapi: 3.0.0
info:
  version: 1.0.0
  title: pets

paths:
  /pets:
    patch:
      requestBody:
        content:
          application/json:
            schema:
              anyOf:
                - $ref: '#/components/schemas/PetByAge'
                - $ref: '#/components/schemas/PetByType'
      responses:
        '200':
          description: Updated

components:
  schemas:
    PetByAge:
      type: object
      properties:
        age:
          type: integer
        nickname:
          type: string
      required:
        - age

    PetByType:
      type: object
      properties:
        pet_type:
          type: string
          enum: [Cat, Dog]
        hunts:
          type: boolean
      required:
        - pet_type
Generation Details
docker run --rm -v "${PWD}:/local" openapitools/openapi-generator-cli:latest generate -i /local/pets.yaml -g kotlin -o /local/out
Steps to reproduce
Suggest a fix

I'm afraid Kotlin's type system is not strong enough to express these kinds of nullability nuances. A good enough approach would be to make all the generated fields nullable.

AnthonyPicquet commented 2 months ago

Hi @david-kubecka

Have you found a solution to this problem?

wing328 commented 2 months ago

did you try enabling the option generateOneOfAnyOfWrappers ?

ref: https://openapi-generator.tech/docs/generators/kotlin/

AnthonyPicquet commented 1 month ago

Hello @wing328,

Yes, I've tried, but it makes no difference. Here is my full config :

    globalProperties.set(
        mapOf(
            "apis" to "",
            "models" to "",
            "auth" to "",
            "supportingFiles" to "CollectionFormats.kt"
        )
    )
    configOptions.set(
        mapOf(
            "dateLibrary" to "legacy",
            "library" to "jvm-ktor",
            "useCoroutines" to "true",
            "serializationLibrary" to "kotlinx_serialization",
            "enumPropertyNaming" to "UPPERCASE",
            "apiSuffix" to "InternalApi",
            "generateOneOfAnyOfWrappers" to "true"
        )
    )
    typeMappings.set(
        mapOf(
            "DateTime" to "java.util.Date",
            "number" to "kotlin.Int",
            "Json" to "kotlinx.serialization.json.Json"
        )
    )