openapi-core/openapi_core/schema/schemas/registries.py

33 lines
895 B
Python
Raw Normal View History

2018-04-17 12:18:40 +00:00
"""OpenAPI core schemas registries module"""
import logging
2018-07-28 20:57:49 +00:00
from lazy_object_proxy import Proxy
2018-04-17 12:18:40 +00:00
from openapi_core.schema.schemas.factories import SchemaFactory
2018-07-28 20:57:49 +00:00
from openapi_core.schema.schemas.util import dicthash
2018-04-17 12:18:40 +00:00
log = logging.getLogger(__name__)
class SchemaRegistry(SchemaFactory):
def __init__(self, dereferencer):
super(SchemaRegistry, self).__init__(dereferencer)
self._schemas = {}
def get_or_create(self, schema_spec):
2018-07-28 20:57:49 +00:00
schema_hash = dicthash(schema_spec)
2018-04-17 12:18:40 +00:00
schema_deref = self.dereferencer.dereference(schema_spec)
2018-07-28 20:57:49 +00:00
if schema_hash in self._schemas:
return self._schemas[schema_hash], False
if '$ref' in schema_spec:
schema = Proxy(lambda: self.create(schema_deref))
else:
schema = self.create(schema_deref)
self._schemas[schema_hash] = schema
2018-04-17 12:18:40 +00:00
2018-07-28 20:57:49 +00:00
return schema, True