OpenAPITools / OpenAPITools/openapi-generator

[BUG] Unable to unpickle an object in python

Open
#10,607 5 comments 4 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Client: Python Issue: Bug
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

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

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

The failure is in generated testopenapi/model_utils.py, where unpickling reaches getattr, getitem, and contains before _data_store exists. Reproduce the provided pickle round trip from the OpenAPI spec, trace the generated Python model behavior, and verify that pickle.loads(pickle.dumps(test)) completes without a KeyError.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.