Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

List of child objects and kotlin generator #16175

Open
honzour opened this issue Jul 24, 2023 · 2 comments
Open

List of child objects and kotlin generator #16175

honzour opened this issue Jul 24, 2023 · 2 comments

Comments

@honzour
Copy link

honzour commented Jul 24, 2023

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
Copy link
Author

honzour commented Jul 24, 2023

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
Copy link

#10010 (comment)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants