From 680487ac9720162fbd3e14261b925c8d48f10ab6 Mon Sep 17 00:00:00 2001 From: 1152405245hwc-coder <279400421+1152405245hwc-coder@users.noreply.github.com> Date: Mon, 30 Nov 2026 08:54:21 +0000 Subject: [PATCH] Clarify non-exploded URI-template list matching Document that matching does not recover a list from a comma-joined expansion, and pin that limit next to scalar-comma and exploded-list behavior. --- docs/servers/uri-templates.md | 27 +++++++++++- tests/shared/test_uri_template.py | 71 +++++++++++++++++++++++++++++-- 2 files changed, 94 insertions(+), 4 deletions(-) diff --git a/docs/servers/uri-templates.md b/docs/servers/uri-templates.md index 1d6e13c0a0..d43f272645 100644 --- a/docs/servers/uri-templates.md +++ b/docs/servers/uri-templates.md @@ -72,7 +72,30 @@ ignored, and omitted params fall through to your function defaults. So If you want each path segment as a separate list item rather than one string with slashes, use `{/name*}`. With `shelves://browse{/path*}`, a client reading `shelves://browse/fiction/sci-fi` calls -`browse_shelf(["fiction", "sci-fi"])`. +`browse_shelf(["fiction", "sci-fi"])`. Use this exploded form when the +handler takes a list. + +`expand()` accepts a list for any variable. A non-exploded variable +comma-joins the items, which is the expansion RFC 6570 specifies. +`match()` returns that text as one string. A comma in the URI may be +the join between list items or text inside a single value, and +matching has no way to decide which. RFC 6570 specifies expansion +rather than a general inverse, so the SDK does not split on those +commas. + +`items{/ids}` expanded with `["a", "b"]` produces `items/a,b`. +Matching that URI returns `{"ids": "a,b"}`. Expanding the match +percent-encodes the comma and produces `items/a%2Cb`. The simple form +stops at the comma: `items/{ids}` expanded with the same list is also +`items/a,b`, and that URI does not match the template. + +A comma that belongs to one scalar still round-trips. Expanding +`{"ids": "a,b"}` through `items{/ids}` or `items/{ids}` produces +`items/a%2Cb`, and matching that URI returns `{"ids": "a,b"}`. + +The exploded form keeps the list, including a comma inside one item. +`items{/ids*}` expanded with `["a,b", "c"]` produces `items/a%2Cb/c`, +and matching returns `{"ids": ["a,b", "c"]}`. ### Template reference @@ -256,6 +279,8 @@ Three things are happening in the highlighted lines: * `{name}` matches one segment; `{+name}` keeps the slashes; `{?a,b}` pulls from the query string; `{/name*}` splits segments into a list. +* A non-exploded variable comma-joins a list on expand and returns one + string on match. Use `{/name*}` when the handler takes a list. * Two variables with nothing between them, or a second multi-segment variable, are rejected at parse time. A parameter bound to a trailing `{?...}`/`{&...}` query variable must declare a Python default. diff --git a/tests/shared/test_uri_template.py b/tests/shared/test_uri_template.py index cddbb82b0b..d5a6c12bc0 100644 --- a/tests/shared/test_uri_template.py +++ b/tests/shared/test_uri_template.py @@ -868,6 +868,63 @@ def test_roundtrip_expand_then_match(template: str, variables: dict[str, str | l assert t.match(uri) == variables +def test_match_non_exploded_path_list_returns_a_joined_string() -> None: + """A list expanded through `items{/ids}` matches as one string, and that string re-encodes. + + `expand()` comma-joins the items. `match()` returns `"a,b"`, and expanding + that match percent-encodes the comma, so the URI is `items/a%2Cb`. Pinned + current behavior: RFC 6570 specifies expansion, and a comma may be scalar + content, so matching does not split it back into a list. + """ + template = UriTemplate.parse("items{/ids}") + uri = template.expand({"ids": ["a", "b"]}) + assert uri == "items/a,b" + matched = template.match(uri) + assert isinstance(matched, dict) + assert matched == {"ids": "a,b"} + assert template.expand(matched) == "items/a%2Cb" + + +def test_match_non_exploded_simple_list_expansion_is_not_a_match() -> None: + """`items/{ids}` does not match the URI produced by expanding a list. + + Simple expansion stops at a comma, so `items/a,b` — the expansion of + `["a", "b"]` — is not a match. Pinned current behavior, same limit as + the non-exploded path form, with a different result: no match at all. + """ + template = UriTemplate.parse("items/{ids}") + uri = template.expand({"ids": ["a", "b"]}) + assert uri == "items/a,b" + assert template.match(uri) is None + + +def test_match_keeps_a_scalar_comma_and_an_exploded_list_item() -> None: + """A comma inside one value stays there, and an exploded list stays a list. + + The scalar `"a,b"` expands to `items/a%2Cb` and matches back to that + string on both `items{/ids}` and `items/{ids}`. `items{/ids*}` expanded + with `["a,b", "c"]` matches back to the same list; the encoded comma + stays inside the first item and the slash is the list boundary. + """ + for template_text in ("items{/ids}", "items/{ids}"): + template = UriTemplate.parse(template_text) + uri = template.expand({"ids": "a,b"}) + assert uri == "items/a%2Cb", template_text + matched = template.match(uri) + assert isinstance(matched, dict) + assert matched == {"ids": "a,b"} + assert template.expand(matched) == uri + + exploded = UriTemplate.parse("items{/ids*}") + values: dict[str, list[str]] = {"ids": ["a,b", "c"]} + exploded_uri = exploded.expand(values) + assert exploded_uri == "items/a%2Cb/c" + exploded_match = exploded.match(exploded_uri) + assert isinstance(exploded_match, dict) + assert exploded_match == values + assert exploded.expand(exploded_match) == exploded_uri + + def test_match_simple_var_accepts_empty() -> None: # RFC 6570 §3.2.2: {var} with var="" expands to nothing, so the inverse # must accept it. v1.x's [^/]+ regex did not — see migration guide. @@ -924,7 +981,12 @@ def _random_value(rng: random.Random) -> str: def _random_values(specs: list[tuple[str, bool]], rng: random.Random) -> dict[str, str | list[str]]: - """Draw a value for every variable: a string, or a non-empty list for explode variables.""" + """Draw a string for each variable, or a non-empty list only when that variable is exploded. + + `expand()` also accepts a list for a non-exploded variable. This generator + never draws one, so the inversion property does not cover every value + `expand()` accepts. + """ return { name: [_random_value(rng) for _ in range(rng.randint(1, 3))] if explode else _random_value(rng) for name, explode in specs @@ -954,8 +1016,11 @@ def test_match_inverts_expand_for_every_parseable_template() -> None: Exact equality with the original values is not required: a different pre-image (e.g. an explode list that flattens) is a correct answer as long - as it re-expands identically. SDK-defined contract — RFC 6570 specifies - only expansion, so `match()` is the inverse the SDK promises. + as it re-expands identically. The drawn values are strings, or lists only + for exploded variables, so the property does not cover every value + `expand()` accepts — a list passed to a non-exploded variable is one such + value. SDK-defined contract — RFC 6570 specifies only expansion, so + `match()` is the inverse the SDK promises. """ rng = random.Random(_PROPERTY_SEED) accepted = 0