OpenAPITools / OpenAPITools/openapi-generator
[BUG][python] deepcopy of model gives KeyError: '_data_store'
Nobody has claimed this yet.
- Dominant language
- Java
- Stars
- 26.8k
- Forks
- 7.7k
- PR merge metrics
- PR metrics pending
Description
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?
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 samples/openapi3/client/petstore/python and model_utils.py, focusing on OpenApiModel and the Banana deepcopy reproduction. Compare deepcopy(Banana(5.2)) with deepcopy(Mammal()) and verify the fix avoids the '_data_store' KeyError while accounting for the reported discriminator failure.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- python
- Domain
- api
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 28/100