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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ The package version runs on its own semver; the schema it is generated from is `

## Unreleased

## 0.158.3 (2026-11-28)

- **Fixed: a request body is refused for its depth only past 256 levels, where onlyworlds.com stops reading a body**, its top counted as 1: `ValueError: a request body is nested more than 256 deep (an object or list at the top is depth 1), past what onlyworlds.com reads. Nothing was sent.` 0.158.2 drew the line at 200 counted from the body's top, which refused a `bulk` item whose element held a value 199 or 200 levels deep. The server stores that value, since it counts a stored value's 200 from the element it sits in, and the folder readers read it back. A value nested past 200 in an element is still refused, by the server, with a 422 that names its key. As the npm SDK (4.219.3).

## 0.158.2 (2026-11-28)

- **Fixed: a non-finite number in a request body is refused before sending, by name and with its place.** `json.dumps` wrote the bare tokens `NaN`, `Infinity` and `-Infinity`, which are not JSON, and the server answered them with a 500. Any write now raises `ValueError: a request body holds NaN at x_score, which JSON cannot carry: send a finite number, or None to clear the field. Nothing was sent.`, the place a list index in brackets (`items[1].element.x_weight` in a `bulk`). The body is then encoded with `allow_nan=False`, so no bare token leaves the client.
Expand Down
2 changes: 1 addition & 1 deletion src/onlyworlds/_version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = "0.158.2"
__version__ = "0.158.3"
19 changes: 12 additions & 7 deletions src/onlyworlds/http.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@
from typing import Any, Protocol

from ._ids import uuid7
from ._outside_json import MAX_DEPTH
from ._schema import FIELD_KINDS
from ._version import __version__
from .errors import ApiError, api_error, error_code, parse_retry_after
Expand Down Expand Up @@ -199,13 +198,19 @@ def _segment(value: str, pattern: re.Pattern[str], what: str) -> str:
return value


#: The deepest request body keel's reader takes, its top counted as 1 (it answers 422 past it).
_BODY_MAX_DEPTH = 256


def _encode_body(payload: Any) -> bytes:
"""A request body as the JSON bytes sent, or ``ValueError`` before anything is sent, in the npm SDK's
words (None where it says null), for the two bodies keel cannot read as written:

- nested more than ``MAX_DEPTH`` (200) deep, counted as the folder readers count it (an object or list
at the top is depth 1), however deep it goes: the walk keeps its own stack. keel reads 256, so a body
this sends is under its line.
- nested more than ``_BODY_MAX_DEPTH`` (256) deep (an object or list at the top is depth 1), however
deep it goes: the walk keeps its own stack. keel's reader of a body takes no more and answers 422.
What keel stores is held to the standard's 200, counted from the level a value is stored at (an
element, inside a ``bulk`` item too), and keel refuses past that by name: so no value keel would
store is refused here.
- a non-finite float (NaN, either infinity) anywhere in it, named with its place: keel answers the bare
tokens ``json.dumps`` writes with a 500.

Expand All @@ -224,9 +229,9 @@ def _encode_body(payload: Any) -> bytes:
non_finite = f"{token} at {place or 'the top'}"
continue
depth = parent + 1
if depth > MAX_DEPTH:
if depth > _BODY_MAX_DEPTH:
raise ValueError(
f"a request body is nested more than {MAX_DEPTH} deep (an object or list at the top is depth 1),"
f"a request body is nested more than {_BODY_MAX_DEPTH} deep (an object or list at the top is depth 1),"
" past what onlyworlds.com reads. Nothing was sent."
)
stack.extend((v, depth, p) for v, p in reversed(children))
Expand Down Expand Up @@ -526,7 +531,7 @@ def _prepare(
self, method: str, path: str, payload: Any, headers: Mapping[str, str] | None
) -> tuple[str, dict[str, str], bytes | None]:
"""The URL, headers and body of one request. Raises at once on a body it cannot send
(one carrying `world`, a value JSON cannot hold, a non-finite number, a nesting past 200 levels):
(one carrying `world`, a value JSON cannot hold, a non-finite number, a nesting past 256 levels):
the request was never made."""
sent = dict(self._headers)
if path.split("?", 1)[0].strip("/") == "health":
Expand Down
20 changes: 14 additions & 6 deletions tests/test_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -359,24 +359,32 @@ def test_a_non_finite_number_anywhere_in_a_body_is_refused_by_name_with_its_plac


def _nest(levels: int) -> Any:
"""``levels - 1`` lists inside the body's own object: the body is ``levels`` deep."""
"""``levels - 1`` lists inside the object that holds it: that object is ``levels`` deep."""
v: Any = 1
for _ in range(levels - 1):
v = [v]
return v


def test_a_body_nested_past_200_levels_is_refused_by_name_and_200_is_sent(make_client: MakeClient) -> None:
t = FakeTransport(script=[jresp(200, {})])
def test_a_body_nested_past_256_levels_past_what_keel_reads_is_refused_by_name_and_256_is_sent(
make_client: MakeClient,
) -> None:
ok = {"errors": False, "items": [{"status": 201, "id": "11111111-2222-4333-8444-555555555555"}]}
t = FakeTransport(script=[jresp(200, {}), jresp(200, ok)])
c = make_client(t)
for body in ({"x_tool": _nest(201)}, {"x_tool": _nest(200_000)}, {"x_score": float("nan"), "x_tool": _nest(201)}):
for body in ({"x_tool": _nest(257)}, {"x_tool": _nest(200_000)}, {"x_score": float("nan"), "x_tool": _nest(257)}):
# Deeper than json.dumps can walk is the same refusal; past the line and holding NaN, the depth is named.
with pytest.raises(ValueError, match="a request body is nested more than 200 deep") as got:
with pytest.raises(ValueError, match="a request body is nested more than 256 deep") as got:
c.patch_world(body)
assert str(got.value).endswith("Nothing was sent.")
assert t.calls == []
c.patch_world({"x_tool": _nest(200)})
c.patch_world({"x_tool": _nest(256)})
assert len(t.calls) == 1
# A bulk item's element 200 deep, counted from the element as keel stores it, is sent: the element
# sits at depth 4 of the body ({"items": [{"element": ...}]}), so the body is 203 deep.
pid = "11111111-2222-4333-8444-555555555555"
c.bulk([{"type": "character", "element": {"id": pid, "name": "K", "x_tool": _nest(200)}}])
assert len(t.calls) == 2


def test_a_transport_refusal_that_is_not_an_exception_is_never_retried(make_client: MakeClient) -> None:
Expand Down
Loading