Zulip server and web application. Open-source team chat that helps teams stay productive and focused.
openapi: Enhance markdown extension for responses.
Suyash Vardhan Mathur669225085882
diff --git a/zerver/openapi/markdown_extension.py b/zerver/openapi/markdown_extension.pyindex abf0031f1b1..9897c94ebaa 100644--- a/zerver/openapi/markdown_extension.py+++ b/zerver/openapi/markdown_extension.py@@ -19,8 +19,8 @@ from markdown.preprocessors import Preprocessorimport zerver.openapi.python_examplesfrom zerver.openapi.openapi import (+ generate_openapi_fixture,get_openapi_description,- get_openapi_fixture,get_openapi_summary,openapi_spec,)@@ -443,17 +443,8 @@ class APICodeExamplesPreprocessor(Preprocessor):return linesdef render_fixture(self, function: str, name: Optional[str] = None) -> List[str]:- fixture = []-path, method = function.rsplit(":", 1)- fixture_dict = get_openapi_fixture(path, method, name)- fixture_json = json.dumps(fixture_dict, indent=4, sort_keys=True, separators=(",", ": "))-- fixture.append("``` json")- fixture.extend(fixture_json.splitlines())- fixture.append("```")-- return fixture+ return generate_openapi_fixture(path, method, name)class APIDescriptionPreprocessor(Preprocessor):diff --git a/zerver/openapi/openapi.py b/zerver/openapi/openapi.pyindex f2052bee029..6ae4b6822d5 100644--- a/zerver/openapi/openapi.py+++ b/zerver/openapi/openapi.py@@ -5,6 +5,7 @@# definitions and validate that Zulip's implementation matches what is# described in our documentation.+import jsonimport osimport refrom typing import Any, Dict, List, Optional, Set, Tuple@@ -208,6 +209,47 @@ def get_openapi_fixture(endpoint: str, method: str, status_code: str = "200") ->return get_schema(endpoint, method, status_code)["example"]+def get_openapi_fixture_description(endpoint: str, method: str, status_code: str = "200") -> str:+ """Fetch a fixture from the full spec object."""+ return get_schema(endpoint, method, status_code)["description"]+++def generate_openapi_fixture(endpoint: str, method: str, status_code: str = "200") -> List[str]:+ """Generate fixture to be rendered"""+ fixture = []+ if status_code not in openapi_spec.openapi()["paths"][endpoint][method.lower()]["responses"]:+ subschema_count = 0+ elif (+ "oneOf"+ in openapi_spec.openapi()["paths"][endpoint][method.lower()]["responses"][status_code][+ "content"+ ]["application/json"]["schema"]+ ):+ subschema_count = len(+ openapi_spec.openapi()["paths"][endpoint][method.lower()]["responses"][status_code][+ "content"+ ]["application/json"]["schema"]["oneOf"]+ )+ else:+ subschema_count = 1+ for subschema_index in range(subschema_count):+ if subschema_count != 1:+ subschema_status_code = status_code + "_" + str(subschema_index)+ else:+ subschema_status_code = status_code+ fixture_dict = get_openapi_fixture(endpoint, method, subschema_status_code)+ fixture_description = (+ get_openapi_fixture_description(endpoint, method, subschema_status_code).strip() + ":"+ )+ fixture_json = json.dumps(fixture_dict, indent=4, sort_keys=True, separators=(",", ": "))++ fixture.extend(fixture_description.splitlines())+ fixture.append("``` json")+ fixture.extend(fixture_json.splitlines())+ fixture.append("```")+ return fixture++def get_openapi_description(endpoint: str, method: str) -> str:"""Fetch a description from the full spec object."""return openapi_spec.openapi()["paths"][endpoint][method.lower()]["description"]