openapi-generator
openapi-generator copied to clipboard
[BUG][PYTHON] Deepcopy fail for allOf schemas
Description
The deepcopy operation fails in models with allof schemas. For example, it is not possible to deepcopy the model dog (from petstore_api.model.dog import Dog). The deepcopy of the Dog model throws a ApiValueError Cannot deserialize input data due to missing discriminator. The discriminator property 'className' is missing at path: ().
openapi-generator version
master
OpenAPI declaration file content or url
Models from openapi-generator/samples/client/petstore/python
Command line used for generation
N/A
Steps to reproduce
>>> from copy import deepcopy
>>> from petstore_api.model.dog import Dog
>>> model_dog = Dog(class_name='Dog', color='white', breed='Jack Russel Terrier')
>>> new_dog = deepcopy(model_dog)
petstore_api.exceptions.ApiValueError: Cannot deserialize input data due to missing discriminator. The discriminator property 'className' is missing at path: ()
Related issues/PRs
may be related with: https://github.com/OpenAPITools/openapi-generator/issues/9669
Suggest a fix/enhancement
In model_utils.py file (from methods_shared.mustache mustache) add the **self.__dict__ *args to the __new__ function (as in the copy function) seems to solve the problem:
def __deepcopy__(self, memo):
cls = self.__class__
if self.get("_spec_property_naming", False):
new_inst = cls._new_from_openapi_data()
else:
new_inst = cls.__new__(cls, **self.__dict__)
for k, v in self.__dict__.items():
setattr(new_inst, k, deepcopy(v, memo))
return new_inst