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.42k stars 6.48k forks source link

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

Open OptimeeringBigya opened 3 months ago

OptimeeringBigya commented 3 months ago

Bug Report Checklist

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 commented 3 months ago

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 commented 1 month ago

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