openapi-generator icon indicating copy to clipboard operation
openapi-generator copied to clipboard

[BUG] [Python Client] Python client is unable to resolve complex structure under AnyOf

Open OptimeeringBigya opened this issue 1 year ago • 3 comments

Bug Report Checklist

  • [x] Have you provided a full/minimal spec to reproduce the issue?
  • [x] Have you validated the input using an OpenAPI validator (example)?
  • [ ] Have you tested with the latest master to confirm the issue still exists?
  • [x] Have you searched for related issues/PRs?
  • [ ] What's the actual output vs expected output?
  • [ ] [Optional] Sponsorship to speed up the bug fix or feature request (example)
Description

Attempting to post data using python client results in a error when the data structure for the data in OpenAPI spec contains combination of complex structure and anyof data type.

openapi-generator version

7.6.0

OpenAPI declaration file content or url

https://gist.github.com/OptimeeringBigya/7130bdeb5ed6cdc474ee2d3c5897c1b4

Generation Details

java -jar openapi-generator-cli.jar generate
--input-spec ./test_openapi_json.json
--generator-name python
--output ./test_python_client

Steps to reproduce
  1. Install venv
  2. Using the venv, execute in python:
from test_python_client.openapi_client.api.default_api import DefaultApi
from test_python_client.openapi_client.models.data import Data

data = [
    {
        "prediction_for": "2024-06-25T09:33:46.556Z",
        "value": 0.1
    },
    {
        "prediction_for": "2024-06-25T09:33:46.556Z",
        "values": {"0.1": 0.1}
    },
    {
        "prediction_for": "2024-06-25T09:33:46.556Z",
        "values": {"N": 0.1}
    }
]
a = [Data.from_dict(i) for i in data]
print(a[0].to_dict())
print(a[1].to_dict())
print(a[2].to_dict())
response = DefaultApi().post_post(a)
print(response)
Related issues/PRs

https://github.com/OpenAPITools/openapi-generator/issues/16110

OptimeeringBigya avatar Jun 25 '24 10:06 OptimeeringBigya

Python script is failing with the following exception:

pydantic_core._pydantic_core.ValidationError: 1 validation error for post_post
1
  Input should be a valid dictionary or instance of Data [type=model_type, input_value=[Data(anyof_schema_1_vali...irectionalPrediction'})], input_type=list]
    For further information visit https://errors.pydantic.dev/2.7/v/model_type

OptimeeringBigya avatar Jun 25 '24 10:06 OptimeeringBigya

Did some digging myself.

Seems like the kwargs need to be passed into the init as actual_instance https://github.com/OpenAPITools/openapi-generator/blob/0f294a51297d788630c96a146a9a48d1dae3e3e5/modules/openapi-generator/src/main/resources/python/model_anyof.mustache#L54 like how it is done in https://github.com/OpenAPITools/openapi-generator/blob/0f294a51297d788630c96a146a9a48d1dae3e3e5/modules/openapi-generator/src/main/resources/python/model_anyof.mustache#L52

OptimeeringBigya avatar Aug 22 '24 13:08 OptimeeringBigya

Issue is still present in 7.13.0. It occurs when an anyof model contains a field that is also an anyof model. In model_generic.mustache there are multiple exceptions for field types where we overwrite the default model_dump() output with the output of to_dict(). I suspect a similar exception for these anyof container types would also do the trick for this bug.

Mythir avatar Jun 06 '25 12:06 Mythir