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.

openapi: Modify curl example generation logic.

Suyash Vardhan Mathur246262fb57e7
diff --git a/zerver/openapi/markdown_extension.py b/zerver/openapi/markdown_extension.py
index 2f85ae4c9aa..72548bdb7dc 100644
--- a/zerver/openapi/markdown_extension.py
+++ b/zerver/openapi/markdown_extension.py
@@ -21,6 +21,7 @@ import zerver.openapi.python_examples
from zerver.openapi.openapi import (
check_requires_administrator,
generate_openapi_fixture,
+ get_curl_include_exclude,
get_openapi_description,
get_openapi_summary,
openapi_spec,
@@ -347,8 +348,6 @@ def generate_curl_example(
def render_curl_example(
function: str,
api_url: str,
- exclude: Optional[List[str]] = None,
- include: Optional[List[str]] = None,
) -> List[str]:
"""A simple wrapper around generate_curl_example."""
parts = function.split(":")
@@ -360,9 +359,18 @@ def render_curl_example(
if len(parts) > 3:
kwargs["auth_api_key"] = parts[3]
kwargs["api_url"] = api_url
- kwargs["exclude"] = exclude
- kwargs["include"] = include
- return generate_curl_example(endpoint, method, **kwargs)
+ rendered_example = []
+ for element in get_curl_include_exclude(endpoint, method):
+ kwargs["include"] = None
+ kwargs["exclude"] = None
+ if element["type"] == "include":
+ kwargs["include"] = element["parameters"]["enum"]
+ if element["type"] == "exclude":
+ kwargs["exclude"] = element["parameters"]["enum"]
+ if "description" in element:
+ rendered_example.extend(element["description"].splitlines())
+ rendered_example = rendered_example + generate_curl_example(endpoint, method, **kwargs)
+ return rendered_example
SUPPORTED_LANGUAGES: Dict[str, Any] = {
diff --git a/zerver/openapi/openapi.py b/zerver/openapi/openapi.py
index f47c8f4b996..62e3f1e3bde 100644
--- a/zerver/openapi/openapi.py
+++ b/zerver/openapi/openapi.py
@@ -214,6 +214,18 @@ def get_openapi_fixture_description(endpoint: str, method: str, status_code: str
return get_schema(endpoint, method, status_code)["description"]
+def get_curl_include_exclude(endpoint: str, method: str) -> List[Dict[str, Any]]:
+ """Fetch all the kinds of parameters required for curl examples."""
+ if (
+ "x-curl-examples-parameters"
+ not in openapi_spec.openapi()["paths"][endpoint][method.lower()]
+ ):
+ return [{"type": "exclude", "parameters": {"enum": [""]}}]
+ return openapi_spec.openapi()["paths"][endpoint][method.lower()]["x-curl-examples-parameters"][
+ "oneOf"
+ ]
+
+
def check_requires_administrator(endpoint: str, method: str) -> bool:
"""Fetch if the endpoint requires admin config."""
return openapi_spec.openapi()["paths"][endpoint][method.lower()].get(