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.76k stars 6.56k forks source link

[BUG] 7.5.5 w/ Python 'useOneOfDiscriminatorLookup' logic doesn't work for `parse_obj` /w @validate_arguments #19177

Open emilebosch opened 3 months ago

emilebosch commented 3 months ago

Description

Bug that makes it impossible to use discriminated types consistently

  1. Generate a type with an anyof with a discriminator
  2. Use that type in a api call
  3. See that the api call isn't properly done, because its None
@validate_parameters
def my_generated_api_call(x: MyGeneratedType):
   print(x.input) # not properly serialized so is None, but should have been a TimeSeriesType

my_generated_api_call(x={
        "input": { # < = Anyof [timeseries, audio etc]
            "inputType": "time-series", 
            "frequencyHz": 100,
            "windowLengthMs": 330,
        },
    }
})

# 1 `MyGeneratedType.from_dict({ dict here })` works.
# 2. But using `MyGeneratedType.parse_obj({ dict_here })` doesn't work. 
# 3. `MyGeneratedType(**{ k:v})` doesn't work either

Probable cause

I've tracked it down to the way this is implemented in openapi generator:

  1. MyGeneratedType.from_dict({ dict here }) works.
  2. But using MyGeneratedType.parse_obj({ dict_here }) doesn't work.
  3. MyGeneratedType(**{ k:v}) doesn't work either

Because the openapi calls are done via @validate_arguments decorater which under the hood also uses one of those 2,3, those types aren't properly serialized.

openapi-generator version

7.5.5

OpenAPI declaration file content or url

Generation Details

OPENAPI_CONTAINER="openapitools/openapi-generator-cli:v7.5.0" #v6.4.0
-g python-pydantic-v1 \
--additional-properties="useOneOfDiscriminatorLookup=true,floatStrictType=false,packageName=$PACKAGE_NAME,generateSourceCodeOnly=true"

Steps to reproduce

Related issues/PRs

Suggest a fix