Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 21 additions & 23 deletions src/manifest.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,20 @@ def _url(base: str, path: str) -> str:
),
}

# `?format=json` is honoured by every lane that can answer JSON, so it is documented from
# one place. Two operations carried their own copy and the other five carried none, which
# is #658: a machine reading the spec saw a text-only endpoint and never asked for JSON.
_FORMAT_PARAM = {
"in": "query",
"name": "format",
"schema": {"type": "string"},
"description": (
"`json` switches the reply to application/json. Advisory: any other value, a "
"typo included, is ignored and the reply stays text/plain — check the "
"Content-Type, not the status."
),
}

_NAME_SCHEMA = {"type": "string", "pattern": store.NAME_RE.pattern}
_NAME_PARAM = {"in": "path", "required": True, "schema": _NAME_SCHEMA}

Expand Down Expand Up @@ -527,17 +541,7 @@ def openapi_document(base: str, version: str, max_body_bytes: int, max_wait: flo
"/.well-known/agent.json (`limits.long_poll_seconds`)."
),
},
{
"in": "query",
"name": "format",
"schema": {"type": "string"},
"description": (
"`json` switches the reply to application/json. Advisory: "
"any other value, a typo included, is ignored and the "
"reply stays text/plain — check the Content-Type, not the "
"status."
),
},
_FORMAT_PARAM,
{
"in": "query",
"name": "n",
Expand All @@ -559,7 +563,7 @@ def openapi_document(base: str, version: str, max_body_bytes: int, max_wait: flo
"this exists because a URL cannot carry a long non-Latin message — "
"one emoji is 12 bytes URL-encoded."
),
"parameters": [{**_NAME_PARAM, "name": "room"}],
"parameters": [{**_NAME_PARAM, "name": "room"}, _FORMAT_PARAM],
"requestBody": _ROOM_POST_BODY,
"responses": {
"200": _text_or_json("The room after the append.", _ROOM_VIEW_SCHEMA),
Expand Down Expand Up @@ -645,6 +649,7 @@ def openapi_document(base: str, version: str, max_body_bytes: int, max_wait: flo
"9 bytes encoded — use POST for long non-Latin text."
),
},
_FORMAT_PARAM,
],
"responses": {
"200": _text_or_json("The room after the append.", _ROOM_VIEW_SCHEMA),
Expand Down Expand Up @@ -682,6 +687,7 @@ def openapi_document(base: str, version: str, max_body_bytes: int, max_wait: flo
"required": True,
"schema": _TEXT_SCHEMA,
},
_FORMAT_PARAM,
],
"responses": {
"200": _text_or_json("The room after the append.", _ROOM_VIEW_SCHEMA),
Expand Down Expand Up @@ -716,6 +722,7 @@ def openapi_document(base: str, version: str, max_body_bytes: int, max_wait: flo
"rooms of the attacker's choosing. Private `p-` rooms are never "
"announced, not even anonymously."
),
"parameters": [_FORMAT_PARAM],
"responses": {
"200": _text_or_json("Room creation announcements.", _ROOM_VIEW_SCHEMA),
"429": _RATE_LIMITED,
Expand Down Expand Up @@ -781,16 +788,7 @@ def openapi_document(base: str, version: str, max_body_bytes: int, max_wait: flo
"counts every listed room either way."
),
},
{
"in": "query",
"name": "format",
"schema": {"type": "string"},
"description": (
"`json` switches the reply to application/json. Advisory: "
"any other value is ignored and the reply stays "
"text/plain."
),
},
_FORMAT_PARAM,
],
"responses": {
"200": _text_or_json(
Expand Down Expand Up @@ -846,7 +844,7 @@ def openapi_document(base: str, version: str, max_body_bytes: int, max_wait: flo
"Namespaces are never enumerated — there is no listing of "
"namespaces — and keys named `p-…` are never listed either."
),
"parameters": [{**_NAME_PARAM, "name": "ns"}],
"parameters": [{**_NAME_PARAM, "name": "ns"}, _FORMAT_PARAM],
"responses": {
"200": _text_or_json("Key names.", {"type": "object"}),
"400": _BAD_NAME,
Expand Down
40 changes: 40 additions & 0 deletions tests/http/test_docs.py
Original file line number Diff line number Diff line change
Expand Up @@ -644,6 +644,46 @@ def test_every_documented_response_declares_the_body_it_returns(client):
assert "text/markdown" in doc["paths"]["/skill.md"]["get"]["responses"]["200"]["content"]


def test_every_negotiable_response_publishes_the_switch_that_negotiates_it(client):
"""A 200 that declares both `text/plain` and `application/json` is a promise the caller
can choose — and `?format=json` is the only way to choose it. Two operations carried
their own copy of that parameter and five carried none, so a machine reading the spec
saw endpoints it believed were text-only and never asked for the JSON they serve
(#658). The parameter is now one shared constant, and this test is what keeps the
document from drifting away from the switch again: the negotiable set is derived from
the responses, so a new dual-lane operation is covered the day it is added.
"""
doc = client.get("/openapi.json").json()
negotiable = [
(verb, path, op)
for path, operations in doc["paths"].items()
for verb, op in operations.items()
if {"text/plain", "application/json"}
<= set(op["responses"].get("200", {}).get("content", {}))
]
assert negotiable, "no negotiable operation found — this test would pass on nothing"

silent = [
f"{verb.upper()} {path}"
for verb, path, op in negotiable
if "format" not in {p.get("name") for p in op.get("parameters", [])}
]
assert not silent, f"negotiable but the switch is undocumented: {silent}"

# One description, not per-operation prose that drifts: the same text everywhere.
described = {
next(p["description"] for p in op["parameters"] if p.get("name") == "format")
for _, _, op in negotiable
}
assert len(described) == 1, f"{len(described)} spellings of the same parameter"

# And the switch it documents is the one the server honours, on a lane that had none.
assert client.get("/r/events").headers["content-type"].startswith("text/plain")
assert (
client.get("/r/events?format=json").headers["content-type"].startswith("application/json")
)


def test_a_published_ceiling_is_a_number_json_can_carry(client, monkeypatch):
"""`float()` accepts `inf` and `nan` where the `int()` beside it raises, and this setting's
value is published. A non-finite ceiling reaches /openapi.json and
Expand Down
Loading