[BUG][python] deepcopy of model gives KeyError: '_data_store'
Created by: mfmarche
Bug Report Checklist
-
Have you provided a full/minimal spec to reproduce the issue? -
Have you validated the input using an OpenAPI validator (example)? -
Have you tested with the latest master to confirm the issue still exists? -
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
I ran into this as I was attempting to use dataclasses.asdict(obj). I tracked it down to use of a deepcopy call. While copying the openapi model object, I believe the default deepcopy is missing the object attributes.
openapi-generator version
master
OpenAPI declaration file content or url
I used the sample petstore api in: samples/openapi3/client/petstore/python
Generation Details
N/A
Steps to reproduce
from copy import deepcopy
from petstore_api.model.banana import Banana
first = Banana(5.2)
second = deepcopy(first)
<snip>
KeyError: '_data_store'
Related issues/PRs
Suggest a fix
I came up with a solution to this by updating the model_utils.py, specifically the OpenApiModel, adding:
def __copy__(self):
cls = self.__class__
result = cls.__new__(cls)
result.__dict__.update(self.__dict__)
return result
def __deepcopy__(self, memo):
cls = self.__class__
result = cls.__new__(cls)
for k, v in self.__dict__.items():
setattr(result, k, deepcopy(v, memo))
return result
Note that this worked only for a Normal object without a discriminator. I ran into a separate issue (might be unrelated?), where the same test with a discriminator gave the following:
from copy import deepcopy
from petstore_api.model.mammal import Mammal
deeepcopy(Mammal())
<snip>
petstore_api.exceptions.ApiValueError: Cannot deserialize input data due to missing discriminator. The discriminator property 'className' is missing at path: ()
Could there be other pitfalls with implementation of the deepcopy above?