Skip to content

Commit

Permalink
Fix remote obj cache (#313)
Browse files Browse the repository at this point in the history
* Fix remote obj cache

* fix key on remote_object_cache
  • Loading branch information
koxudaxi authored Jan 21, 2021
1 parent ad63568 commit f9b64e0
Showing 1 changed file with 10 additions and 4 deletions.
14 changes: 10 additions & 4 deletions datamodel_code_generator/parser/jsonschema.py
Original file line number Diff line number Diff line change
Expand Up @@ -785,19 +785,25 @@ def _get_ref_body_from_url(self, ref: str) -> Dict[Any, Any]:
return ref_body

def _get_ref_body_from_remote(self, ref: str) -> Dict[Any, Any]:
cached_ref_body: Optional[Dict[str, Any]] = self.remote_object_cache.get(ref)
if cached_ref_body:
return cached_ref_body
# Remote Reference – $ref: 'document.json' Uses the whole document located on the same server and in
# the same location. TODO treat edge case
if self.current_source_path and len(self.current_source_path.parts) > 1:
full_path = self.base_path / self.current_source_path.parent / ref
else:
full_path = self.base_path / ref

ref_full_path = str(full_path)

cached_ref_body: Optional[Dict[str, Any]] = self.remote_object_cache.get(
ref_full_path
)
if cached_ref_body:
return cached_ref_body

# yaml loader can parse json data.
with full_path.open(encoding=self.encoding) as f:
ref_body = load_yaml(f)
self.remote_object_cache[ref] = ref_body
self.remote_object_cache[ref_full_path] = ref_body
return ref_body

def parse_ref(self, obj: JsonSchemaObject, path: List[str]) -> None:
Expand Down

0 comments on commit f9b64e0

Please sign in to comment.