Skip to content

Commit 7993de8

Browse files
committed
Small change to Schema model to allow generated specs to be picklable
- overriding __dict__ is death for pickling. I renamed it to __newdict__ and tweaked the one user of it, and now this part of the structure is working fine for pickling - there are also upstream changes in jsonschema that need to be in for the overall success - this allows me to create the API spec from a swagger file once (which takes 2-20s for the files I'm working with), and cache the result as a pickle file for loading on the next startup (assuming the swagger file hasn't been updated). The load from pickle files takes 2-5ms. This is an improvement of load time by 3 orders of magnitude.
1 parent 1270d5a commit 7993de8

File tree

2 files changed

+6
-2
lines changed

2 files changed

+6
-2
lines changed

openapi_core/schema/schemas/models.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,8 +67,12 @@ def __init__(
6767

6868
self._source = _source
6969

70+
# Overriding object.__dict__ is a VERY bad idea as it totally breaks any
71+
# possibility of pickling this object. Pickling marshalls via object.__dict__
72+
# via default __getstate__ and __setstate__ methods. This is now renamed to
73+
# keep the functionality for the validators, but keep pickling operational.
7074
@property
71-
def __dict__(self):
75+
def __newdict__(self):
7276
return self._source or self.to_dict()
7377

7478
def to_dict(self):

openapi_core/unmarshalling/schemas/factories.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ def get_validator(self, schema):
8686
}
8787
if self.context is not None:
8888
kwargs[self.CONTEXT_VALIDATION[self.context]] = True
89-
return OAS30Validator(schema.__dict__, **kwargs)
89+
return OAS30Validator(schema.__newdict__, **kwargs)
9090

9191
def _get_format_checker(self):
9292
fc = deepcopy(oas30_format_checker)

0 commit comments

Comments
 (0)