def get_ref_body(ref: str, openapi_model_parser: OpenAPIModelParser, components: Dict[str, Any]) -> Dict[str, Any]: if ref.startswith('#/components'): return get_model_by_path(components, ref[13:].split('/')) elif ref.startswith('http://') or ref.startswith('https://'): if '#/' in ref: url, path = ref.rsplit('#/', 1) ref_body = openapi_model_parser._get_ref_body(url) return get_model_by_path(ref_body, path.split('/')) else: return openapi_model_parser._get_ref_body(ref) raise NotImplementedError(f'ref={ref} is not supported')
def response_objects(self) -> List[Response]: responses: List[Response] = [] for status_code, detail in self.responses.items(): ref: Optional[str] = detail.get('$ref') if ref and ref.startswith('#/components/'): content: Dict[str, Any] = get_model_by_path( self.components, ref[13:].split('/') ).get("content", {}) else: content = detail.get("content", {}) contents = {} for content_type, obj in content.items(): contents[content_type] = ( JsonSchemaObject.parse_obj(obj["schema"]) if "schema" in obj else None ) responses.append( Response( status_code=status_code, description=detail.get("description"), contents=contents, ) ) return responses
def get_ref_model(self, ref: str) -> Dict[str, Any]: ref_file, ref_path = self.model_resolver.resolve_ref(ref).split('#', 1) if ref_file: ref_body = self._get_ref_body(ref_file) else: # pragma: no cover ref_body = self.raw_obj return get_model_by_path(ref_body, ref_path.split('/')[1:])
def test_get_model_by_path(schema: Dict, path: str, model: Dict): assert get_model_by_path(schema, path.split('/') if path else []) == model