def __init__(self, root): # type: (Union[IOBase, str], str, Callable) -> None if not isinstance(root, OpenAPI): root = OpenAPI(root) # This ensures all elements have URLs and JSON pointers meta.url(root, meta.url(root)) meta.pointer(root, meta.pointer(root)) # Private Properties self._traversed_relative_urls_pointers = set() self._relative_urls_pointers_class_names = {} self._class_names_relative_urls_pointers = {} self._relative_urls_pointers_meta = {} self._class_names_meta = {} self._relative_urls_pointers_models = {} self._class_names_models = {} # Public properties self.root = root self.resolver = Resolver(self.root) self.major_version = int( (self.root.swagger or self.root.openapi).split(".")[0].strip() ) self.references = OrderedDict() self.pointers_schemas = OrderedDict() self.names_models = OrderedDict() self.names_schemas = {} self.pointers_models = OrderedDict()
def unmarshal_resolved_reference(resolved_reference, url, pointer, types=None): # type: (Model, Optional[str], str, Sequence[Property, type]) -> Model if types or (not isinstance(resolved_reference, Model)): resolved_reference = unmarshal(resolved_reference, types=types) # Re-assign the URL and pointer meta.url(resolved_reference, url) meta.pointer(resolved_reference, pointer) return resolved_reference
def get_definition_relative_url_and_pointer( self, definition: Union[Schema, Operation, Parameter] ) -> Tuple[str, str]: """ Given a schema/operation/parameter definition, return a relative path in relation to the root document and the pointer """ url: Optional[str] = meta.url(definition) or "" assert isinstance(url, str) pointer = meta.pointer(definition) return self.get_relative_url(url), pointer
def __init__( self, resolver, # type: Resolver root, # type: Union[Sequence, Dict, Array, Dictionary] url=None, # type: Optional[str] ): # type: (...) -> None assert isinstance(url, str) assert isinstance(resolver, Resolver) # Infer the document URL if (url is None) and isinstance(root, Model): url = meta.url(root) self.resolver = resolver self.root = root self.pointers = {} self.url = url
def __init__(self, root, url=None, urlopen=request.urlopen): # type: (OpenAPI, Optional[str], typing.Callable, bool) -> None # Ensure arguments are of the correct types assert callable(urlopen) assert isinstance(root, OpenAPI) assert isinstance(url, (str, NoneType)) # This is the function used to open external pointer references self.urlopen = urlopen # Infer the URL from the `OpenAPI` document, if not explicitly provided if url is None: url = meta.url(root) or "" self.url = url # This is the primary document--the one we are resolving document = _Document(self, root, url) # Store the primary document both by URL and under the key "#" (for # convenient reference) self.documents = {url: document} if url != "": self.documents[""] = document
def _resolve(self, model_instance, types=None): # type: (ModelBase, Optional[Sequence[type, Property]]) -> ModelBase """ If `model_instance` is a reference, get the referenced object """ if isinstance(model_instance, Reference): if types is None: types = (Schema,) url = meta.url(model_instance) or "" pointer = urljoin(meta.pointer(model_instance), model_instance.ref) while isinstance(model_instance, Reference): resolved_model_instance = self.resolver.get_document( url ).resolve(pointer, types) if resolved_model_instance is model_instance: raise RuntimeError( "`Reference` instance is self-referential: " + pointer ) else: model_instance = resolved_model_instance if isinstance(model_instance, Reference): raise RuntimeError(pointer) return model_instance