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: Get parameters from requestBody too.

Anders Kaseorg5cac872e4baa
diff --git a/zerver/openapi/curl_param_value_generators.py b/zerver/openapi/curl_param_value_generators.py
index c39c6ee5441..93b9a11f537 100644
--- a/zerver/openapi/curl_param_value_generators.py
+++ b/zerver/openapi/curl_param_value_generators.py
@@ -84,8 +84,8 @@ def patch_openapi_example_values(
if parameter.name in realm_example_values:
parameter.example = realm_example_values[parameter.name]
- if request_body is not None:
- properties = request_body["content"]["multipart/form-data"]["schema"]["properties"]
+ if request_body is not None and "multipart/form-data" in (content := request_body["content"]):
+ properties = content["multipart/form-data"]["schema"]["properties"]
for key, property in properties.items():
if key in realm_example_values:
property["example"] = realm_example_values[key]
diff --git a/zerver/openapi/markdown_extension.py b/zerver/openapi/markdown_extension.py
index 63130b83119..606119f79f9 100644
--- a/zerver/openapi/markdown_extension.py
+++ b/zerver/openapi/markdown_extension.py
@@ -336,10 +336,10 @@ def generate_curl_example(
)
lines.append(example_value)
- if "requestBody" in operation_entry:
- properties = operation_entry["requestBody"]["content"]["multipart/form-data"]["schema"][
- "properties"
- ]
+ if "requestBody" in operation_entry and "multipart/form-data" in (
+ content := operation_entry["requestBody"]["content"]
+ ):
+ properties = content["multipart/form-data"]["schema"]["properties"]
for key, property in properties.items():
lines.append(" -F " + shlex.quote("{}=@{}".format(key, property["example"])))
diff --git a/zerver/openapi/openapi.py b/zerver/openapi/openapi.py
index 2cae5036fe3..e9221d36248 100644
--- a/zerver/openapi/openapi.py
+++ b/zerver/openapi/openapi.py
@@ -323,7 +323,7 @@ NO_EXAMPLE = object()
class Parameter(BaseModel):
- kind: Literal["query", "path"]
+ kind: Literal["query", "path", "formData"]
name: str
description: str
json_encoded: bool
@@ -373,6 +373,31 @@ def get_openapi_parameters(
)
)
+ if "requestBody" in operation and "application/x-www-form-urlencoded" in (
+ content := operation["requestBody"]["content"]
+ ):
+ media_type = content["application/x-www-form-urlencoded"]
+ required = media_type["schema"].get("required", [])
+ for key, schema in media_type["schema"]["properties"].items():
+ json_encoded = (
+ "encoding" in media_type
+ and key in (encodings := media_type["encoding"])
+ and encodings[key].get("contentType") == "application/json"
+ ) or schema.get("type") == "object"
+
+ parameters.append(
+ Parameter(
+ kind="formData",
+ name=key,
+ description=schema["description"],
+ json_encoded=json_encoded,
+ value_schema=schema,
+ example=schema.get("example"),
+ required=key in required,
+ deprecated=schema.get("deprecated", False),
+ )
+ )
+
return parameters