Skip to content

Commit

Permalink
Add function to retrieve markers from OAS object
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
ikalnytskyi committed Aug 29, 2020
1 parent 9dbae9c commit 7de882b
Showing 1 changed file with 22 additions and 26 deletions.
48 changes: 22 additions & 26 deletions sphinxcontrib/openapi/renderers/_httpdomain.py
Original file line number Diff line number Diff line change
Expand Up @@ -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."""

Expand Down Expand Up @@ -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:
Expand All @@ -247,25 +266,14 @@ 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"):
yield from indented(
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}"
Expand Down Expand Up @@ -342,26 +350,14 @@ 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
# have one and only one entry. Hence casting its values to
# 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}"
Expand Down

0 comments on commit 7de882b

Please sign in to comment.