2020-03-27 05:03:26 +01:00
|
|
|
import json
|
2020-06-11 00:54:34 +02:00
|
|
|
import re
|
2024-02-01 05:43:43 +01:00
|
|
|
from typing import Any, List, Mapping, Sequence
|
2017-12-27 08:57:47 +01:00
|
|
|
|
2020-06-11 00:54:34 +02:00
|
|
|
import markdown
|
2018-08-14 02:50:05 +02:00
|
|
|
from django.utils.html import escape as escape_html
|
2017-12-27 08:57:47 +01:00
|
|
|
from markdown.extensions import Extension
|
|
|
|
from markdown.preprocessors import Preprocessor
|
2023-10-12 19:43:45 +02:00
|
|
|
from typing_extensions import override
|
2020-06-11 00:54:34 +02:00
|
|
|
|
2024-05-20 22:09:35 +02:00
|
|
|
from zerver.lib.markdown.priorities import PREPROCESSOR_PRIORITIES
|
2021-06-23 17:27:22 +02:00
|
|
|
from zerver.openapi.openapi import (
|
2024-02-01 05:43:43 +01:00
|
|
|
Parameter,
|
2021-06-23 17:27:22 +02:00
|
|
|
check_deprecated_consistency,
|
|
|
|
get_openapi_parameters,
|
|
|
|
get_parameters_description,
|
|
|
|
)
|
2017-12-27 08:57:47 +01:00
|
|
|
|
2021-02-12 08:20:45 +01:00
|
|
|
REGEXP = re.compile(r"\{generate_api_arguments_table\|\s*(.+?)\s*\|\s*(.+)\s*\}")
|
2017-12-27 08:57:47 +01:00
|
|
|
|
2021-11-16 16:19:24 +01:00
|
|
|
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>
|
2022-01-25 14:14:56 +01:00
|
|
|
<code>{value}</code>: <span class=api-field-type>{data_type}</span> {required} {description}{object_details}
|
2021-11-16 16:19:24 +01:00
|
|
|
</li>
|
|
|
|
""".strip()
|
|
|
|
|
|
|
|
OBJECT_DESCRIPTION_TEMPLATE = """
|
|
|
|
{description}
|
|
|
|
<p>{additional_information}</p>
|
|
|
|
""".strip()
|
|
|
|
|
|
|
|
OBJECT_CODE_TEMPLATE = "<code>{value}</code>".strip()
|
|
|
|
|
2017-12-27 08:57:47 +01:00
|
|
|
|
|
|
|
class MarkdownArgumentsTableGenerator(Extension):
|
2023-10-12 19:43:45 +02:00
|
|
|
@override
|
2020-10-20 01:28:13 +02:00
|
|
|
def extendMarkdown(self, md: markdown.Markdown) -> None:
|
|
|
|
md.preprocessors.register(
|
2021-09-17 19:01:36 +02:00
|
|
|
APIArgumentsTablePreprocessor(md, self.getConfigs()),
|
|
|
|
"generate_api_arguments",
|
2024-05-20 22:09:35 +02:00
|
|
|
PREPROCESSOR_PRIORITIES["generate_api_arguments"],
|
2017-12-27 08:57:47 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
class APIArgumentsTablePreprocessor(Preprocessor):
|
2020-10-20 02:49:02 +02:00
|
|
|
def __init__(self, md: markdown.Markdown, config: Mapping[str, Any]) -> None:
|
2020-04-09 21:51:58 +02:00
|
|
|
super().__init__(md)
|
2017-12-27 08:57:47 +01:00
|
|
|
|
2023-10-12 19:43:45 +02:00
|
|
|
@override
|
2017-12-27 08:57:47 +01:00
|
|
|
def run(self, lines: List[str]) -> List[str]:
|
|
|
|
done = False
|
|
|
|
while not done:
|
|
|
|
for line in lines:
|
|
|
|
loc = lines.index(line)
|
|
|
|
match = REGEXP.search(line)
|
|
|
|
|
2018-07-13 14:09:03 +02:00
|
|
|
if not match:
|
|
|
|
continue
|
|
|
|
|
|
|
|
doc_name = match.group(2)
|
2023-01-31 19:49:06 +01:00
|
|
|
endpoint, method = doc_name.rsplit(":", 1)
|
2024-02-01 05:43:43 +01:00
|
|
|
parameters = get_openapi_parameters(endpoint, method)
|
|
|
|
if parameters:
|
|
|
|
text = self.render_parameters(parameters)
|
2021-06-23 17:27:22 +02:00
|
|
|
# We want to show this message only if the parameters
|
|
|
|
# description doesn't say anything else.
|
2023-01-31 19:49:06 +01:00
|
|
|
elif get_parameters_description(endpoint, method) == "":
|
2021-02-12 08:20:45 +01:00
|
|
|
text = ["This endpoint does not accept any parameters."]
|
2021-06-23 17:27:22 +02:00
|
|
|
else:
|
|
|
|
text = []
|
2018-07-13 14:09:03 +02:00
|
|
|
# The line that contains the directive to include the macro
|
|
|
|
# may be preceded or followed by text or tags, in that case
|
|
|
|
# we need to make sure that any preceding or following text
|
|
|
|
# stays the same.
|
|
|
|
line_split = REGEXP.split(line, maxsplit=0)
|
|
|
|
preceding = line_split[0]
|
|
|
|
following = line_split[-1]
|
2020-09-02 06:59:07 +02:00
|
|
|
text = [preceding, *text, following]
|
2021-02-12 08:19:30 +01:00
|
|
|
lines = lines[:loc] + text + lines[loc + 1 :]
|
2018-07-13 14:09:03 +02:00
|
|
|
break
|
2017-12-27 08:57:47 +01:00
|
|
|
else:
|
|
|
|
done = True
|
|
|
|
return lines
|
|
|
|
|
2024-02-01 05:43:43 +01:00
|
|
|
def render_parameters(self, parameters: Sequence[Parameter]) -> List[str]:
|
|
|
|
lines = []
|
2017-12-27 08:57:47 +01:00
|
|
|
|
|
|
|
md_engine = markdown.Markdown(extensions=[])
|
2024-02-01 05:43:43 +01:00
|
|
|
parameters = sorted(parameters, key=lambda parameter: parameter.deprecated)
|
|
|
|
for parameter in parameters:
|
|
|
|
name = parameter.name
|
|
|
|
description = parameter.description
|
|
|
|
enums = parameter.value_schema.get("enum")
|
2022-01-27 20:18:59 +01:00
|
|
|
if enums is not None:
|
|
|
|
formatted_enums = [
|
|
|
|
OBJECT_CODE_TEMPLATE.format(value=json.dumps(enum)) for enum in enums
|
|
|
|
]
|
|
|
|
description += "\nMust be one of: {}. ".format(", ".join(formatted_enums))
|
2018-07-26 20:48:34 +02:00
|
|
|
|
2024-02-01 05:43:43 +01:00
|
|
|
default = parameter.value_schema.get("default")
|
2018-07-26 20:48:34 +02:00
|
|
|
if default is not None:
|
2021-02-12 08:20:45 +01:00
|
|
|
description += f"\nDefaults to `{json.dumps(default)}`."
|
2024-02-01 05:43:43 +01:00
|
|
|
data_type = generate_data_type(parameter.value_schema)
|
2018-07-26 20:48:34 +02:00
|
|
|
|
2020-03-27 05:03:26 +01:00
|
|
|
# TODO: OpenAPI allows indicating where the argument goes
|
|
|
|
# (path, querystring, form data...). We should document this detail.
|
2024-02-01 05:43:43 +01:00
|
|
|
|
|
|
|
# We use this style without explicit JSON encoding for
|
|
|
|
# integers, strings, and booleans.
|
|
|
|
# * For booleans, JSON encoding correctly corrects for Python's
|
|
|
|
# str(True)="True" not matching the encoding of "true".
|
|
|
|
# * For strings, doing so nicely results in strings being quoted
|
|
|
|
# in the documentation, improving readability.
|
|
|
|
# * For integers, it is a noop, since json.dumps(3) == str(3) == "3".
|
|
|
|
example = json.dumps(parameter.example)
|
2020-05-11 16:26:33 +02:00
|
|
|
|
2020-06-11 21:35:13 +02:00
|
|
|
required_string: str = "required"
|
2024-02-01 05:43:43 +01:00
|
|
|
if parameter.kind == "path":
|
2020-06-11 21:35:13 +02:00
|
|
|
# Any path variable is required
|
2024-02-01 05:43:43 +01:00
|
|
|
assert parameter.required
|
2021-02-12 08:20:45 +01:00
|
|
|
required_string = "required in path"
|
2020-06-11 21:35:13 +02:00
|
|
|
|
2024-02-01 05:43:43 +01:00
|
|
|
if parameter.required:
|
2020-06-11 21:35:13 +02:00
|
|
|
required_block = f'<span class="api-argument-required">{required_string}</span>'
|
|
|
|
else:
|
|
|
|
required_block = '<span class="api-argument-optional">optional</span>'
|
|
|
|
|
2024-02-01 05:43:43 +01:00
|
|
|
check_deprecated_consistency(parameter.deprecated, description)
|
|
|
|
if parameter.deprecated:
|
2020-06-26 16:18:27 +02:00
|
|
|
deprecated_block = '<span class="api-argument-deprecated">Deprecated</span>'
|
|
|
|
else:
|
2021-02-12 08:20:45 +01:00
|
|
|
deprecated_block = ""
|
2020-06-26 16:18:27 +02:00
|
|
|
|
2021-11-16 16:19:24 +01:00
|
|
|
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:
|
2024-02-01 05:43:43 +01:00
|
|
|
object_schema = parameter.value_schema
|
2021-11-16 16:19:24 +01:00
|
|
|
|
|
|
|
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))
|
|
|
|
|
2024-02-01 05:43:43 +01:00
|
|
|
lines.append(
|
2021-11-16 16:19:24 +01:00
|
|
|
API_PARAMETER_TEMPLATE.format(
|
|
|
|
argument=name,
|
2021-02-12 08:19:30 +01:00
|
|
|
example=escape_html(example),
|
|
|
|
required=required_block,
|
|
|
|
deprecated=deprecated_block,
|
|
|
|
description=md_engine.convert(description),
|
2023-09-12 21:10:57 +02:00
|
|
|
type=data_type,
|
2021-11-16 16:19:24 +01:00
|
|
|
object_details=object_block,
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
2024-02-01 05:43:43 +01:00
|
|
|
return lines
|
2021-11-16 16:19:24 +01:00
|
|
|
|
|
|
|
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 "example" in object_values[value]:
|
|
|
|
example = json.dumps(object_values[value]["example"])
|
|
|
|
formatted_example = OBJECT_CODE_TEMPLATE.format(value=escape_html(example))
|
2022-01-25 14:14:56 +01:00
|
|
|
additions += (
|
|
|
|
f'<span class="api-argument-example-label">Example</span>: {formatted_example}'
|
|
|
|
)
|
2021-11-16 16:19:24 +01:00
|
|
|
|
|
|
|
if len(additions) > 0:
|
|
|
|
additional_information = "".join(additions).strip()
|
|
|
|
description_final = OBJECT_DESCRIPTION_TEMPLATE.format(
|
|
|
|
description=md_engine.convert(description),
|
|
|
|
additional_information=additional_information,
|
2021-02-12 08:19:30 +01:00
|
|
|
)
|
2021-11-16 16:19:24 +01:00
|
|
|
else:
|
|
|
|
description_final = md_engine.convert(description)
|
|
|
|
|
2022-01-25 14:14:56 +01:00
|
|
|
required_block = ""
|
|
|
|
if "required" in schema:
|
|
|
|
if value in schema["required"]:
|
|
|
|
required_block = '<span class="api-argument-required">required</span>'
|
|
|
|
else:
|
|
|
|
required_block = '<span class="api-argument-optional">optional</span>'
|
|
|
|
|
2021-11-16 16:19:24 +01:00
|
|
|
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,
|
2022-01-25 14:14:56 +01:00
|
|
|
required=required_block,
|
2021-11-16 16:19:24 +01:00
|
|
|
description=description_final,
|
|
|
|
object_details=details,
|
2021-02-12 08:19:30 +01:00
|
|
|
)
|
2017-12-27 08:57:47 +01:00
|
|
|
|
2021-11-16 16:19:24 +01:00
|
|
|
li_elements.append(li)
|
|
|
|
|
|
|
|
object_details = OBJECT_DETAILS_TEMPLATE.format(
|
|
|
|
argument=name,
|
|
|
|
values="\n".join(li_elements),
|
|
|
|
)
|
|
|
|
return object_details
|
2017-12-27 08:57:47 +01:00
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2017-12-27 08:57:47 +01:00
|
|
|
def makeExtension(*args: Any, **kwargs: str) -> MarkdownArgumentsTableGenerator:
|
2023-01-31 19:49:06 +01:00
|
|
|
return MarkdownArgumentsTableGenerator(*args, **kwargs)
|
2020-12-10 15:58:12 +01:00
|
|
|
|
2021-02-12 08:19:30 +01:00
|
|
|
|
2020-12-10 15:58:12 +01:00
|
|
|
def generate_data_type(schema: Mapping[str, Any]) -> str:
|
|
|
|
data_type = ""
|
2021-02-12 08:20:45 +01:00
|
|
|
if "oneOf" in schema:
|
|
|
|
for item in schema["oneOf"]:
|
2020-12-10 15:58:12 +01:00
|
|
|
data_type = data_type + generate_data_type(item) + " | "
|
|
|
|
data_type = data_type[:-3]
|
2021-02-12 08:20:45 +01:00
|
|
|
elif "items" in schema:
|
|
|
|
data_type = "(" + generate_data_type(schema["items"]) + ")[]"
|
2020-12-10 15:58:12 +01:00
|
|
|
else:
|
2021-02-12 08:20:45 +01:00
|
|
|
data_type = schema["type"]
|
2024-02-02 01:22:50 +01:00
|
|
|
if schema.get("nullable", False):
|
2021-11-16 16:37:24 +01:00
|
|
|
data_type = data_type + " | null"
|
2020-12-10 15:58:12 +01:00
|
|
|
return data_type
|