openapi: Get parameters from requestBody too.

Signed-off-by: Anders Kaseorg <anders@zulip.com>
This commit is contained in:
Anders Kaseorg 2024-01-31 21:07:29 -08:00 committed by Anders Kaseorg
parent a67d1b57b9
commit 5cac872e4b
3 changed files with 32 additions and 7 deletions

View File

@ -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]

View File

@ -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"])))

View File

@ -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