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.48k stars 6.5k forks source link

[BUG] Python client code generation with allOf and discriminator #8268

Open wessmq opened 3 years ago

wessmq commented 3 years ago

Bug Report Checklist

Description

I am trying to generate python client code from yaml that uses allOf with a discriminator property, generated code in 4.3.1 is not describing inheritance between generated models, schemas that are composed from parent model (using allOf) do not have the attributes of the parent as it is represented in JAVA with class extension.

Tried generating using v5 that came out recently and the request validation is failing around get_discriminated_attributes in model_utils

openapi-generator version

4.3.1 / 5.0.0

OpenAPI declaration file content or url

ParentObject:
    type: object
    required:
         - objectType
         - baseProperty
    properties:
       objectType:
            type: string
       baseProperty:
            type: string
    discriminator:
         propertyName:  objectType

ChildObject1:
      allOf:
          -   $ref: '#/components/schemas/ParentObject
          -   type: object
               properties:
                   extraProp1:
                       type: string
ChildObject2:
      allOf:
          -   $ref: '#/components/schemas/ParentObject
          -   type: object
               properties:
                  extraProp2:
                      type: string

in 4.3.1, generated models are: ParentObject with objectType property ChildObject1 with extraProp1 and no mention of ParentObject properties/any relation to it (unlike Java where there is a clear inheritance) same with ChildObject2

in 5.0, generated code looks more attentive to the allOf attributes, but when running requests with created object of type ChildObject1 for example, request validation fails on initial steps, and GET requests fail on nullable and non-required properties if returned as null from server

Steps to reproduce
Related issues/PRs
Suggest a fix
spacether commented 3 years ago

requests fail on nullable and non-required properties if returned as null from server The spec above did not describe that any of the properties can be nullable. This is expected behavior. Set nullable to true for any parameters where null can be received by the client and this will work.

bherw commented 3 years ago

I have a similar structure to the example one above, and when I try to construct a ChildObject1(object_type="ChildObject1"), for example, my code throws a RecursionError in model_utils's get_discriminated_classes method. It looks like because the discriminator() property for all the child objects is set to a list of all of the sibling class plus the child class its, the method tries to recurse forever trying to find grandchildren of the parent object that don't exist.

spacether commented 3 years ago

@bherw that sounds like a separate issue. Can you file a separate ticket with it with a minimal sample that reproduces your issue? Sounds like you have a cyclic schema definition and our code isn't terminating after it loops back on itself.

bherw commented 3 years ago

Thanks, @spacether, I reported #8579