diff --git a/zerver/openapi/openapi.py b/zerver/openapi/openapi.py index b6a37cab52..f2052bee02 100644 --- a/zerver/openapi/openapi.py +++ b/zerver/openapi/openapi.py @@ -7,7 +7,7 @@ import os import re -from typing import Any, Dict, List, Optional, Set +from typing import Any, Dict, List, Optional, Set, Tuple from jsonschema.exceptions import ValidationError as JsonSchemaValidationError from openapi_core import create_spec @@ -218,6 +218,15 @@ def get_openapi_summary(endpoint: str, method: str) -> str: return openapi_spec.openapi()["paths"][endpoint][method.lower()]["summary"] +def get_endpoint_from_operationid(operationid: str) -> Tuple[str, str]: + for endpoint in openapi_spec.openapi()["paths"]: + for method in openapi_spec.openapi()["paths"][endpoint]: + operationId = openapi_spec.openapi()["paths"][endpoint][method].get("operationId") + if operationId == operationid: + return (endpoint, method) + raise AssertionError("No such page exists in OpenAPI data.") + + def get_openapi_paths() -> Set[str]: return set(openapi_spec.openapi()["paths"].keys()) diff --git a/zerver/views/documentation.py b/zerver/views/documentation.py index e88eb3606f..062d9fd8d0 100644 --- a/zerver/views/documentation.py +++ b/zerver/views/documentation.py @@ -15,6 +15,7 @@ from zerver.lib.integrations import CATEGORIES, INTEGRATIONS, HubotIntegration, from zerver.lib.request import REQ, has_request_variables from zerver.lib.subdomains import get_subdomain from zerver.models import Realm +from zerver.openapi.openapi import get_endpoint_from_operationid, get_openapi_summary from zerver.templatetags.app_filters import render_markdown_path @@ -105,7 +106,12 @@ class MarkdownDirectoryView(ApiURLView): with open(article_path) as article_file: first_line = article_file.readlines()[0] # Strip the header and then use the first line to get the article title - article_title = first_line.lstrip("#").strip() + if self.path_template == "/zerver/api/%s.md" and first_line[0] != "#": + api_operation = context["OPEN_GRAPH_URL"].split("/api/")[1].replace("-", "_") + endpoint_path, endpoint_method = get_endpoint_from_operationid(api_operation) + article_title = get_openapi_summary(endpoint_path, endpoint_method) + else: + article_title = first_line.lstrip("#").strip() if context["not_index_page"]: context["OPEN_GRAPH_TITLE"] = f"{article_title} ({title_base})" else: