api docs: Expand checking for deprecated fields.

Added assertion to check that if a deprecated flag is in a field's
schema, then it should have deprecated mentioned in description
as well, and moved these checks to a separate function.
Fixes part of #15967.
This commit is contained in:
Suyash Vardhan Mathur
2021-02-16 17:31:36 +05:30
committed by Tim Abbott
parent 422fea8f20
commit 96bfeeb9e6
3 changed files with 12 additions and 8 deletions

View File

@@ -8,7 +8,7 @@ from django.utils.html import escape as escape_html
from markdown.extensions import Extension
from markdown.preprocessors import Preprocessor
from zerver.openapi.openapi import get_openapi_parameters, likely_deprecated_parameter
from zerver.openapi.openapi import check_deprecated_consistency, get_openapi_parameters
REGEXP = re.compile(r"\{generate_api_arguments_table\|\s*(.+?)\s*\|\s*(.+)\s*\}")
@@ -139,9 +139,7 @@ class APIArgumentsTablePreprocessor(Preprocessor):
else:
required_block = '<span class="api-argument-optional">optional</span>'
# Test to make sure deprecated parameters are marked so.
if likely_deprecated_parameter(description):
assert argument["deprecated"]
check_deprecated_consistency(argument, description)
if argument.get("deprecated", False):
deprecated_block = '<span class="api-argument-deprecated">Deprecated</span>'
else:

View File

@@ -7,7 +7,7 @@ import markdown
from markdown.extensions import Extension
from markdown.preprocessors import Preprocessor
from zerver.openapi.openapi import get_openapi_return_values, likely_deprecated_parameter
from zerver.openapi.openapi import check_deprecated_consistency, get_openapi_return_values
from .api_arguments_table_generator import generate_data_type
@@ -134,9 +134,7 @@ class APIReturnValuesTablePreprocessor(Preprocessor):
continue
description = return_values[return_value]["description"]
data_type = generate_data_type(return_values[return_value])
# Test to make sure deprecated keys are marked appropriately.
if likely_deprecated_parameter(description):
assert return_values[return_value]["deprecated"]
check_deprecated_consistency(return_values[return_value], description)
ans.append(self.render_desc(description, spacing, data_type, return_value))
if "properties" in return_values[return_value]:
ans += self.render_table(return_values[return_value]["properties"], spacing + 4)

View File

@@ -402,6 +402,14 @@ def likely_deprecated_parameter(parameter_description: str) -> bool:
return "**Deprecated**" in parameter_description
def check_deprecated_consistency(argument: Dict[str, Any], description: str) -> None:
# Test to make sure deprecated parameters are marked so.
if likely_deprecated_parameter(description):
assert argument["deprecated"]
if "deprecated" in argument:
assert likely_deprecated_parameter(description)
# Skip those JSON endpoints whose query parameters are different from
# their `/api/v1` counterpart. This is a legacy code issue that we
# plan to fix by changing the implementation.