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.23k stars 6.43k forks source link

List of child objects and kotlin generator #16175

Open honzour opened 1 year ago

honzour commented 1 year ago
Description

Openapi generator into kotlin-spring generates incorrect kotlin code when I use list (ZOO) of child objects (Cats or Dog) with the common inheritance parent (Pet) in the openapi definition. It creates a data class ZOOAnimalsInner with all fields required, does matter if it is field of Cat, Dog or Pet.

openapi-generator version

6.6.0

OpenAPI declaration file content or url
openapi: 3.0.0

info:

  version: 1.0.0

  title: allOf test

  description: based on https://swagger.io/docs/specification/data-models/oneof-anyof-allof-not/

paths:

  /zoo:

    patch:

      requestBody:

        content:

          application/json:

            schema:

              $ref: '#/components/schemas/ZOO'

      responses:

        '200':

          description: Updated

components:

  schemas:

    Pet:

      type: object

      required:

        - pet_type

      properties:

        pet_type:

          type: string

      discriminator:

        propertyName: pet_type

    Dog:     # "Dog" is a value for the pet_type property (the discriminator value)

      allOf: # Combines the main `Pet` schema with `Dog`-specific properties

        - $ref: '#/components/schemas/Pet'

        - type: object

          # all other properties specific to a `Dog`

          required:

            - bark

            - breed

          properties:

            bark:

              type: boolean

            breed:

              type: string

              enum: [Dingo, Husky, Retriever, Shepherd]

    Cat:     # "Cat" is a value for the pet_type property (the discriminator value)

      allOf: # Combines the main `Pet` schema with `Cat`-specific properties

        - $ref: '#/components/schemas/Pet'

        - type: object

          # all other properties specific to a `Cat`

          required:

            - hunts

            - age

          properties:

            hunts:

              type: boolean

            age:

              type: integer

    ZOO:

      type: object

      properties:

        animals:

          type: array

          items:

            oneOf:

              - $ref: '#/components/schemas/Dog'

              - $ref: '#/components/schemas/Cat'
Suggest a fix

Fields of Cat and fields of Dog shlould be optional in the generated class ZOOAnimalsInner.

honzour commented 1 year ago

Here is the incorrect generated Kotlin file:

package mypackage.openapi

import java.util.Objects

import com.fasterxml.jackson.annotation.JsonProperty

import com.fasterxml.jackson.annotation.JsonValue

import mypackage.openapi.Cat

import mypackage.openapi.Dog

import javax.validation.constraints.DecimalMax

import javax.validation.constraints.DecimalMin

import javax.validation.constraints.Email

import javax.validation.constraints.Max

import javax.validation.constraints.Min

import javax.validation.constraints.NotNull

import javax.validation.constraints.Pattern

import javax.validation.constraints.Size

import javax.validation.Valid

import io.swagger.v3.oas.annotations.media.Schema

/**

*

data class ZOOAnimalsInner(

@Schema(example = "null", required = true, description = "")

@get:JsonProperty("pet_type", required = true) val petType: kotlin.String,

@Schema(example = "null", required = true, description = "")

@get:JsonProperty("hunts", required = true) val hunts: kotlin.Boolean,

@Schema(example = "null", required = true, description = "")

@get:JsonProperty("age", required = true) val age: kotlin.Int,

@Schema(example = "null", required = true, description = "")

@get:JsonProperty("bark", required = true) override val bark: kotlin.Boolean,

@Schema(example = "null", required = true, description = "")

@get:JsonProperty("breed", required = true) override val breed: ZOOAnimalsInner.Breed

) {

}

isadounikau commented 11 months ago

https://github.com/OpenAPITools/openapi-generator/issues/10010#issuecomment-1716168444