Skip to content

armgit / zulip

Public Imported from GitHub
0

Zulip server and web application. Open-source team chat that helps teams stay productive and focused.

api_documentation: Render object details in parameter description.

Lauryn Menard7993aed53082
diff --git a/zerver/lib/markdown/api_arguments_table_generator.py b/zerver/lib/markdown/api_arguments_table_generator.py
index b166ed21fc4..807f625f775 100644
--- a/zerver/lib/markdown/api_arguments_table_generator.py
+++ b/zerver/lib/markdown/api_arguments_table_generator.py
@@ -17,6 +17,37 @@ from zerver.openapi.openapi import (
REGEXP = re.compile(r"\{generate_api_arguments_table\|\s*(.+?)\s*\|\s*(.+)\s*\}")
+API_PARAMETER_TEMPLATE = """
+<div class="api-argument" id="parameter-{argument}">
+ <p class="api-argument-name"><strong>{argument}</strong> <span class="api-field-type">{type}</span> {required} {deprecated} <a href="#parameter-{argument}" class="api-argument-hover-link"><i class="fa fa-chain"></i></a></p>
+ <div class="api-example">
+ <span class="api-argument-example-label">Example</span>: <code>{example}</code>
+ </div>
+ <div class="api-description">{description}{object_details}</div>
+ <hr>
+</div>
+""".strip()
+
+OBJECT_DETAILS_TEMPLATE = """
+<p><strong>{argument}</strong> object details:</p>
+<ul>
+{values}
+</ul>
+""".strip()
+
+OBJECT_LIST_ITEM_TEMPLATE = """
+<li>
+<code>{value}</code>: <span class=api-field-type>{data_type}</span> {description}{object_details}
+</li>
+""".strip()
+
+OBJECT_DESCRIPTION_TEMPLATE = """
+{description}
+<p>{additional_information}</p>
+""".strip()
+
+OBJECT_CODE_TEMPLATE = "<code>{value}</code>".strip()
+
class MarkdownArgumentsTableGenerator(Extension):
def __init__(self, configs: Mapping[str, Any] = {}) -> None:
@@ -80,7 +111,7 @@ class APIArgumentsTablePreprocessor(Preprocessor):
arguments = json_obj[doc_name]
if arguments:
- text = self.render_table(arguments)
+ text = self.render_parameters(arguments)
# We want to show this message only if the parameters
# description doesn't say anything else.
elif is_openapi_format and get_parameters_description(endpoint, method) == "":
@@ -101,22 +132,13 @@ class APIArgumentsTablePreprocessor(Preprocessor):
done = True
return lines
- def render_table(self, arguments: Sequence[Mapping[str, Any]]) -> List[str]:
- # TODO: Fix naming now that this no longer renders a table.
- table = []
- argument_template = """
-<div class="api-argument" id="parameter-{argument}">
- <p class="api-argument-name"><strong>{argument}</strong> <span class="api-field-type">{type}</span> {required} {deprecated} <a href="#parameter-{argument}" class="api-argument-hover-link"><i class="fa fa-chain"></i></a></p>
- <div class="api-example">
- <span class="api-argument-example-label">Example</span>: <code>{example}</code>
- </div>
- <div class="api-description">{description}</div>
- <hr>
-</div>"""
+ def render_parameters(self, arguments: Sequence[Mapping[str, Any]]) -> List[str]:
+ parameters = []
md_engine = markdown.Markdown(extensions=[])
arguments = sorted(arguments, key=lambda argument: "deprecated" in argument)
for argument in arguments:
+ name = argument.get("argument") or argument.get("name")
description = argument["description"]
oneof = ["`" + str(item) + "`" for item in argument.get("schema", {}).get("enum", [])]
if oneof:
@@ -163,18 +185,100 @@ class APIArgumentsTablePreprocessor(Preprocessor):
else:
deprecated_block = ""
- table.append(
- argument_template.format(
- argument=argument.get("argument") or argument.get("name"),
+ object_block = ""
+ # TODO: There are some endpoint parameters with object properties
+ # that are not defined in `zerver/openapi/zulip.yaml`
+ if "object" in data_type:
+ if "schema" in argument:
+ object_schema = argument["schema"]
+ else:
+ object_schema = argument["content"]["application/json"]["schema"]
+
+ if "items" in object_schema and "properties" in object_schema["items"]:
+ object_block = self.render_object_details(object_schema["items"], str(name))
+ elif "properties" in object_schema:
+ object_block = self.render_object_details(object_schema, str(name))
+
+ parameters.append(
+ API_PARAMETER_TEMPLATE.format(
+ argument=name,
example=escape_html(example),
required=required_block,
deprecated=deprecated_block,
description=md_engine.convert(description),
- type=data_type,
+ type=(data_type),
+ object_details=object_block,
+ )
+ )
+
+ return parameters
+
+ def render_object_details(self, schema: Mapping[str, Any], name: str) -> str:
+ md_engine = markdown.Markdown(extensions=[])
+ li_elements = []
+
+ object_values = schema.get("properties", {})
+ for value in object_values:
+
+ description = ""
+ if "description" in object_values[value]:
+ description = object_values[value]["description"]
+
+ # check for default, enum, required or example in documentation
+ additions: List[str] = []
+
+ default = object_values.get(value, {}).get("default")
+ if default is not None:
+ formatted_default = OBJECT_CODE_TEMPLATE.format(value=json.dumps(default))
+ additions += f"Defaults to {formatted_default}. "
+
+ enums = object_values.get(value, {}).get("enum")
+ if enums is not None:
+ formatted_enums = [
+ OBJECT_CODE_TEMPLATE.format(value=json.dumps(enum)) for enum in enums
+ ]
+ additions += "Must be one of: {}. ".format(", ".join(formatted_enums))
+
+ if "required" in schema:
+ if value in schema["required"]:
+ additions += "Required value. "
+ else:
+ additions += "Optional value. "
+
+ if "example" in object_values[value]:
+ example = json.dumps(object_values[value]["example"])
+ formatted_example = OBJECT_CODE_TEMPLATE.format(value=escape_html(example))
+ additions += f"Example: {formatted_example}"
+
+ if len(additions) > 0:
+ additional_information = "".join(additions).strip()
+ description_final = OBJECT_DESCRIPTION_TEMPLATE.format(
+ description=md_engine.convert(description),
+ additional_information=additional_information,
)
+ else:
+ description_final = md_engine.convert(description)
+
+ data_type = generate_data_type(object_values[value])
+
+ details = ""
+ if "object" in data_type and "properties" in object_values[value]:
+ details += self.render_object_details(object_values[value], str(value))
+
+ li = OBJECT_LIST_ITEM_TEMPLATE.format(
+ value=value,
+ data_type=data_type,
+ description=description_final,
+ object_details=details,
)
- return table
+ li_elements.append(li)
+
+ object_details = OBJECT_DETAILS_TEMPLATE.format(
+ argument=name,
+ values="\n".join(li_elements),
+ )
+ return object_details
def makeExtension(*args: Any, **kwargs: str) -> MarkdownArgumentsTableGenerator: