OpenAPITools / OpenAPITools/openapi-generator

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

Open
#19,177 0 comments 1 reaction 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

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

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 with the python-pydantic-v1 generator configured with useOneOfDiscriminatorLookup=true, then reproduce the anyOf discriminator case using the described validate_parameters call. Compare from_dict with parse_obj and model construction for the generated type. Done means discriminated values are serialized consistently when passed through the generated API call.

Written by the indexing model from the issue text.

Assessment

Tech stack
openapi, python
Domain
tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.