From 89f637eac697ef50abac1d2b70aa1ff6cb5101c6 Mon Sep 17 00:00:00 2001 From: Andrey Kislyuk Date: Fri, 1 Jun 2018 17:02:31 -0700 Subject: [PATCH 1/2] Resolve internal references in Swagger spec --- hca/util/__init__.py | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/hca/util/__init__.py b/hca/util/__init__.py index 118ca0dc..481cae3b 100755 --- a/hca/util/__init__.py +++ b/hca/util/__init__.py @@ -94,6 +94,7 @@ def special_cli_command(self, required_argument, optional_argument=""): from requests.adapters import HTTPAdapter from requests_oauthlib import OAuth2Session from urllib3.util import retry, timeout +from jsonpointer import resolve_pointer from .. import get_config, logger from .compat import USING_PYTHON2 @@ -202,6 +203,25 @@ def __init__(self, config=None, **session_kwargs): for http_method, method_data in path_data.items(): self._build_client_method(http_method, http_path, method_data) + @staticmethod + def load_swagger_json(swagger_json, ptr_str="$ref"): + """ + Load the Swagger JSON and resolve {"$ref": "#/..."} internal JSON Pointer references. + """ + refs = [] + + def store_refs(d): + if len(d) == 1 and list(d)[0] == ptr_str: + refs.append(d) + return d + + swagger_content = json.load(swagger_json, object_hook=store_refs) + for ref in refs: + _, target = ref.popitem() + assert target[0] == "#" + ref.update(resolve_pointer(swagger_content, target[1:])) + return swagger_content + @property def swagger_spec(self): if not self._swagger_spec: @@ -225,7 +245,7 @@ def swagger_spec(self): with open(swagger_filename, "wb") as fh: fh.write(res.content) with open(swagger_filename) as fh: - self.__class__._swagger_spec = json.load(fh) + self.__class__._swagger_spec = self.load_swagger_json(fh) return self._swagger_spec @property From ddf8b921e3f24ec3a12dc963545cd017e36d9a7f Mon Sep 17 00:00:00 2001 From: Andrey Kislyuk Date: Fri, 1 Jun 2018 17:02:31 -0700 Subject: [PATCH 2/2] WIP --- hca/util/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/hca/util/__init__.py b/hca/util/__init__.py index 481cae3b..7687fe23 100755 --- a/hca/util/__init__.py +++ b/hca/util/__init__.py @@ -211,7 +211,7 @@ def load_swagger_json(swagger_json, ptr_str="$ref"): refs = [] def store_refs(d): - if len(d) == 1 and list(d)[0] == ptr_str: + if len(d) == 1 and ptr_str in d: refs.append(d) return d