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.8k stars 6.58k forks source link

[BUG] Unable to unpickle an object in python #10607

Open fspv opened 3 years ago

fspv commented 3 years ago

Bug Report Checklist

Description

Can't unpickle pickled model in python. On unpickling it throws a KeyError. Expect it to be unpickled correctly.

openapi-generator version

5.2.1

OpenAPI declaration file content or url
openapi: 3.0.0
info:
  description: test
  version: 1.0.1-1
  title: test
  contact:
    name: Pavel Safronov
    email: pv.safronov@gmail.com
  license:
    name: Apache 2.0
    url: 'http://www.apache.org/licenses/LICENSE-2.0.html'
components:
  schemas:
    Test:
      required:
        - test
      properties:
        test:
          type: string
paths: {}

servers:
  - url: 'https://test.com'
Generation Details

Something like that. So pretty much regular generation

wget https://repo1.maven.org/maven2/org/openapitools/openapi-generator-cli/5.2.1/openapi-generator-cli-5.2.1.jar -O openapi-generator-cli.jar

java \
    -jar $tmp_dir/openapi-generator-cli.jar \
    generate \
    -g python \
    -i ${swaggerfile} \
    --package-name testopenapi \
    -o ${out_dir}
Steps to reproduce
In [8]: import pickle                                      

In [9]: from testopenapi.model.test import Test            

In [10]: test = Test(test="test")                          

In [11]: pickle.loads(pickle.dumps(test))                  
---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
<ipython-input-11-b386bc64ce4d> in <module>
----> 1 pickle.loads(pickle.dumps(test))

~/git/github.com/prius/leetcode-swagger/python/out/testopenapi/model_utils.py in __getattr__(self, attr)
    186     def __getattr__(self, attr):
    187         """get the value of an attribute using dot notation: `instance.attr`"""
--> 188         return self.__getitem__(attr)
    189 
    190     def __new__(cls, *args, **kwargs):

~/git/github.com/prius/leetcode-swagger/python/out/testopenapi/model_utils.py in __getitem__(self, name)
    493     def __getitem__(self, name):
    494         """get the value of an attribute using square-bracket notation: `instance[attr]`"""
--> 495         if name in self:
    496             return self.get(name)
    497 

~/git/github.com/prius/leetcode-swagger/python/out/testopenapi/model_utils.py in __contains__(self, name)
    507             return name in self.__dict__
    508 
--> 509         return name in self.__dict__['_data_store']
    510 
    511     def to_dict(self):

KeyError: '_data_store'

I see the same behavior in the real project as well.

Related issues/PRs

Looks like there is something similar going on here https://github.com/OpenAPITools/openapi-generator/issues/9669

However not sure it is fixable the same way, so file a bug anyway.

Suggest a fix
fspv commented 3 years ago

I have tested it against the master branch. The generated code includes the changes from https://github.com/OpenAPITools/openapi-generator/commit/9464999d9c799ff9eb723886206ea98b7222032d

And it still doesn't work. So this is a different issue.

JHthe4 commented 2 years ago

Hi, did you ever find a solution/workaround to this? I'm running into the same issue unfortunately.

fspv commented 2 years ago

Hi @JHthe4, I didn't. I gave up in the end and still using a swagger generator.

JHthe4 commented 2 years ago

Alright :/, thanks for letting me know.