OpenAPITools / OpenAPITools/openapi-generator

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

Open
#18,626 6 comments 4 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

Bug Report Checklist
  • [/] Have you provided a full/minimal spec to reproduce the issue?
  • [/ ] Have you validated the input using an OpenAPI validator (example)?
  • [/] Have you tested with the latest master to confirm the issue still exists?
  • [/] Have you searched for related issues/PRs?
  • [/] What's the actual output vs expected output?
  • [Optional] Sponsorship to speed up the bug fix or feature request (example)
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
  • generate the classes as per above
  • look at the PetsPatchRequest class
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.

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 by reproducing the issue with the provided OpenAPI declaration and the documented Docker command using the Kotlin generator. Inspect the generated PetsPatchRequest model and test it with the valid {"age": 3} payload. Done means the generated Kotlin model can represent valid anyOf data when pet_type is absent, without breaking the other variant.

Written by the indexing model from the issue text.

Assessment

Tech stack
kotlin, openapi
Domain
backend-api-design, tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 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.