From 5cac872e4baa9148035369205d05748d46e4a462 Mon Sep 17 00:00:00 2001 From: Anders Kaseorg Date: Wed, 31 Jan 2024 21:07:29 -0800 Subject: [PATCH] openapi: Get parameters from requestBody too. Signed-off-by: Anders Kaseorg --- zerver/openapi/curl_param_value_generators.py | 4 +-- zerver/openapi/markdown_extension.py | 8 +++--- zerver/openapi/openapi.py | 27 ++++++++++++++++++- 3 files changed, 32 insertions(+), 7 deletions(-) diff --git a/zerver/openapi/curl_param_value_generators.py b/zerver/openapi/curl_param_value_generators.py index c39c6ee544..93b9a11f53 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 63130b8311..606119f79f 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 2cae5036fe..e9221d3624 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