Skip to content
GitLab
Projects Groups Snippets
  • /
  • Help
    • Help
    • Support
    • Community forum
    • Submit feedback
    • Contribute to GitLab
  • Sign in / Register
  • O openapi-generator
  • Project information
    • Project information
    • Activity
    • Labels
    • Members
  • Repository
    • Repository
    • Files
    • Commits
    • Branches
    • Tags
    • Contributors
    • Graph
    • Compare
  • Issues 3,476
    • Issues 3,476
    • List
    • Boards
    • Service Desk
    • Milestones
  • Merge requests 402
    • Merge requests 402
  • CI/CD
    • CI/CD
    • Pipelines
    • Jobs
    • Schedules
  • Deployments
    • Deployments
    • Environments
    • Releases
  • Packages and registries
    • Packages and registries
    • Package Registry
    • Infrastructure Registry
  • Monitor
    • Monitor
    • Incidents
  • Analytics
    • Analytics
    • Value stream
    • CI/CD
    • Repository
  • Wiki
    • Wiki
  • Snippets
    • Snippets
  • Activity
  • Graph
  • Create a new issue
  • Jobs
  • Commits
  • Issue Boards
Collapse sidebar
  • OpenAPI Tools
  • openapi-generator
  • Issues
  • #9669
Closed
Open
Issue created Jun 03, 2021 by Administrator@rootContributor6 of 6 checklist items completed6/6 checklist items

[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?

Assignee
Assign to
Time tracking