api-docs: Handle multiple examples in `responses`.

This commit is contained in:
Vector73 2024-08-11 11:35:10 +05:30 committed by Tim Abbott
parent 07c927ae88
commit fa408a969e
3 changed files with 82 additions and 37 deletions

View File

@ -204,14 +204,20 @@ def get_schema(endpoint: str, method: str, status_code: str) -> dict[str, Any]:
return schema return schema
def get_openapi_fixture(endpoint: str, method: str, status_code: str = "200") -> dict[str, Any]: def get_openapi_fixture(
endpoint: str, method: str, status_code: str = "200"
) -> list[dict[str, Any]]:
"""Fetch a fixture from the full spec object.""" """Fetch a fixture from the full spec object."""
return get_schema(endpoint, method, status_code)["example"] if "example" not in get_schema(endpoint, method, status_code):
return openapi_spec.openapi()["paths"][endpoint][method.lower()]["responses"][status_code][
"content"
def get_openapi_fixture_description(endpoint: str, method: str, status_code: str = "200") -> str: ]["application/json"]["examples"].values()
"""Fetch a fixture from the full spec object.""" return [
return get_schema(endpoint, method, status_code)["description"] {
"description": get_schema(endpoint, method, status_code)["description"],
"value": get_schema(endpoint, method, status_code)["example"],
}
]
def get_curl_include_exclude(endpoint: str, method: str) -> list[dict[str, Any]]: def get_curl_include_exclude(endpoint: str, method: str) -> list[dict[str, Any]]:
@ -279,17 +285,15 @@ def generate_openapi_fixture(endpoint: str, method: str) -> list[str]:
else: else:
subschema_status_code = status_code subschema_status_code = status_code
fixture_dict = get_openapi_fixture(endpoint, method, subschema_status_code) fixture_dict = get_openapi_fixture(endpoint, method, subschema_status_code)
fixture_description = get_openapi_fixture_description( for example in fixture_dict:
endpoint, method, subschema_status_code fixture_json = json.dumps(
).strip() example["value"], indent=4, sort_keys=True, separators=(",", ": ")
fixture_json = json.dumps( )
fixture_dict, indent=4, sort_keys=True, separators=(",", ": ") if "description" in example:
) fixture.extend(example["description"].strip().splitlines())
fixture.append("``` json")
fixture.extend(fixture_description.splitlines()) fixture.extend(fixture_json.splitlines())
fixture.append("``` json") fixture.append("```")
fixture.extend(fixture_json.splitlines())
fixture.append("```")
return fixture return fixture

View File

@ -9265,21 +9265,54 @@ paths:
Legacy property for a part of the Zephyr mirroring system. Legacy property for a part of the Zephyr mirroring system.
**Deprecated**. Clients should ignore this field. **Deprecated**. Clients should ignore this field.
example: examples:
{ modern-presence-format-example:
"msg": "", description: |
"presences": Modern presence format:
{ value:
"10": {
{ "msg": "",
"idle_timestamp": 1656958530, "presences":
"active_timestamp": 1656958520, {
}, "10":
}, {
"result": "success", "idle_timestamp": 1656958530,
"server_timestamp": 1656958539.6287155, "active_timestamp": 1656958520,
"presence_last_update_id": 1000, },
} },
"result": "success",
"server_timestamp": 1656958539.6287155,
"presence_last_update_id": 1000,
}
legacy-presence-format-example:
description: |
Legacy presence format:
value:
{
"msg": "",
"presences":
{
"user@example.com":
{
"aggregated":
{
"client": "website",
"status": "idle",
"timestamp": 1594825445,
},
"website":
{
"client": "website",
"status": "idle",
"timestamp": 1594825445,
"pushable": false,
},
},
},
"result": "success",
"server_timestamp": 1656958539.6287155,
"presence_last_update_id": 1000,
}
/users/me/status: /users/me/status:
post: post:
operationId: update-status operationId: update-status

View File

@ -73,7 +73,7 @@ class OpenAPIToolsTest(ZulipTestCase):
""" """
def test_get_openapi_fixture(self) -> None: def test_get_openapi_fixture(self) -> None:
actual = get_openapi_fixture(TEST_ENDPOINT, TEST_METHOD, TEST_RESPONSE_BAD_REQ) actual = get_openapi_fixture(TEST_ENDPOINT, TEST_METHOD, TEST_RESPONSE_BAD_REQ)[0]["value"]
expected = { expected = {
"code": "BAD_REQUEST", "code": "BAD_REQUEST",
"msg": "You don't have permission to edit this message", "msg": "You don't have permission to edit this message",
@ -1031,9 +1031,17 @@ class OpenAPIAttributesTest(ZulipTestCase):
) )
continue continue
validate_schema(schema) validate_schema(schema)
assert validate_against_openapi_schema( if "example" not in schema:
schema["example"], path, method, status_code assert "examples" in response["content"]["application/json"]
) examples = response["content"]["application/json"]["examples"]
for example in examples:
assert validate_against_openapi_schema(
examples[example]["value"], path, method, status_code
)
else:
assert validate_against_openapi_schema(
schema["example"], path, method, status_code
)
class OpenAPIRegexTest(ZulipTestCase): class OpenAPIRegexTest(ZulipTestCase):