From 7de882b20b1379262a83074d30f313e1c122be40 Mon Sep 17 00:00:00 2001 From: Ihor Kalnytskyi Date: Sat, 29 Aug 2020 19:23:30 +0300 Subject: [PATCH] Add function to retrieve markers from OAS object So far the very same logic, the one that retrieves markers out from OAS object, is implemented two times. Once, for request parameters, and once, for response headers. Since I'm about to introduce the very same logic in third place, it's better to move it into helper function. --- .../openapi/renderers/_httpdomain.py | 48 +++++++++---------- 1 file changed, 22 insertions(+), 26 deletions(-) diff --git a/sphinxcontrib/openapi/renderers/_httpdomain.py b/sphinxcontrib/openapi/renderers/_httpdomain.py index e5eb6ef..84774d5 100644 --- a/sphinxcontrib/openapi/renderers/_httpdomain.py +++ b/sphinxcontrib/openapi/renderers/_httpdomain.py @@ -107,6 +107,26 @@ def _iterexamples(media_types, example_preference, examples_from_schemas): yield content_type, example +def _get_markers_from_object(oas_object, schema): + """Retrieve a bunch of OAS object markers.""" + + markers = [] + + if schema.get("type"): + type_ = schema["type"] + if schema.get("format"): + type_ = f"{type_}:{schema['format']}" + markers.append(type_) + + if oas_object.get("required"): + markers.append("required") + + if oas_object.get("deprecated"): + markers.append("deprecated") + + return markers + + class HttpdomainRenderer(abc.RestructuredTextRenderer): """Render OpenAPI v3 using `sphinxcontrib-httpdomain` extension.""" @@ -229,7 +249,6 @@ def render_parameter(self, parameter): kinds = CaseInsensitiveDict( {"path": "param", "query": "queryparam", "header": "reqheader"} ) - markers = [] schema = parameter.get("schema", {}) if "content" in parameter: @@ -247,18 +266,6 @@ def render_parameter(self, parameter): ) return - if schema.get("type"): - type_ = schema["type"] - if schema.get("format"): - type_ = f"{type_}:{schema['format']}" - markers.append(type_) - - if parameter.get("required"): - markers.append("required") - - if parameter.get("deprecated"): - markers.append("deprecated") - yield f":{kinds[parameter['in']]} {parameter['name']}:" if parameter.get("description"): @@ -266,6 +273,7 @@ def render_parameter(self, parameter): self._convert_markup(parameter["description"]).strip().splitlines() ) + markers = _get_markers_from_object(parameter, schema) if markers: markers = ", ".join(markers) yield f":{kinds[parameter['in']]}type {parameter['name']}: {markers}" @@ -342,7 +350,6 @@ def render_response(self, status_code, response): .splitlines() ) - markers = [] schema = header_value.get("schema", {}) if "content" in header_value: # According to OpenAPI v3 spec, 'content' in this case may @@ -350,18 +357,7 @@ def render_response(self, status_code, response): # list is not expensive and should be acceptable. schema = list(header_value["content"].values())[0].get("schema", {}) - if schema.get("type"): - type_ = schema["type"] - if schema.get("format"): - type_ = f"{type_}:{schema['format']}" - markers.append(type_) - - if header_value.get("required"): - markers.append("required") - - if header_value.get("deprecated"): - markers.append("deprecated") - + markers = _get_markers_from_object(header_value, schema) if markers: markers = ", ".join(markers) yield f":resheadertype {header_name}: {markers}"