OpenAPITools / OpenAPITools/openapi-generator
[BUG] 7.5.5 w/ Python 'useOneOfDiscriminatorLookup' logic doesn't work for `parse_obj` /w @validate_arguments
Nobody has claimed this yet.
- 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
- Generate a type with an
anyofwith a discriminator - Use that type in a api call
- 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:
MyGeneratedType.from_dict({ dict here })works.- But using
MyGeneratedType.parse_obj({ dict_here })doesn't work. 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
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- 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