From 00f84673ff17112e335698efb5a37bb88290e1fa Mon Sep 17 00:00:00 2001 From: Akihito Koriyama Date: Tue, 24 Nov 2026 12:57:44 +0900 Subject: [PATCH 1/3] Skip collection hrefs that are relative without a leading slash UriFactory prefixes any schemeless href with the context scheme and host, so `collection/posts` became `page://selfcollection/posts` and purged `/posts`. Only an absolute path now takes the context scheme. The docs no longer claim the purge matches the link crawl: BEAR.Resource's Linker still validates the original href with `new Uri()`, which rejects a path-only href. --- CHANGELOG.md | 2 +- docs/llms-full.txt | 2 +- src/RefreshAnnotatedCommand.php | 10 ++++++- tests/CollectionLinkTest.php | 11 ++++++++ .../src/Resource/App/Collection/Note.php | 26 +++++++++++++++++++ 5 files changed, 48 insertions(+), 3 deletions(-) create mode 100644 tests/Fake/fake-app/src/Resource/App/Collection/Note.php diff --git a/CHANGELOG.md b/CHANGELOG.md index 9f3b442a..a04330fc 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -8,7 +8,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] ### Added -- `#[Link(rel: 'collection', href: ...)]` on a row's `onGet()` names the list it belongs to: a write to the row purges the list's URI and the variants that differ only in default-valued parameters, so the list needs no `#[DependsOn]` for its rows. A path-only href (`/posts`) is resolved as BEAR.Resource resolves a link, under the context scheme (`#[ContextScheme]`). +- `#[Link(rel: 'collection', href: ...)]` on a row's `onGet()` names the list it belongs to: a write to the row purges the list's URI and the variants that differ only in default-valued parameters, so the list needs no `#[DependsOn]` for its rows. A path-only href (`/posts`) takes the context scheme (`#[ContextScheme]`). - The `purge_skipped` log entry (fields `uri`, the write's URI without its query, and `reason`, `unmatched_query`), recorded when a write on a resource without a cache attribute cannot address its own key because its query lacks a required `onGet()` parameter. - `AppSurrogateKeyModule($key)` adds the key to every entry this package stores and to every `Surrogate-Key` it sets, so invalidating the key on the server and at the CDN clears the whole application. Install it before or after `QueryRepositoryModule`. - `#[DependsOn(uri: ..., key: ...)]`: `key` names the rows in the body `onGet()` returns, so `#[DependsOn(uri: 'app://self/article?id={id}', key: 'articles')]` with `$this->body['articles'] = [['id' => 1, ...], ['id' => 2, ...]]` depends on `?id=1` and `?id=2`; a row that lacks the variable is skipped, an empty list names no URI, and a variable no row holds is read from the body's top level, then from the arguments. A body or row list that can be read only once, such as a `Generator`, is left in the body as an `ArrayIterator` with every row, under the keys it yielded unless a key repeats, as with `yield from`, which leaves a list. diff --git a/docs/llms-full.txt b/docs/llms-full.txt index 9112f090..2a776613 100644 --- a/docs/llms-full.txt +++ b/docs/llms-full.txt @@ -143,7 +143,7 @@ class Article extends ResourceObject ``` - The purge reaches every variant of the collection that differs only in default-valued parameters (see Purging query variants): with `onGet(?int $authorId = null, int $page = 1)`, purging `app://self/articles` drops `?page=2` and `?author_id=7`. A list keyed by a required parameter, such as one author's articles, is another group: name it with `#[Purge]` on the write. -- The href expands as a `#[Purge]` template does, from the body the write returns, then from its query. An href that expands to a path (`/articles`) is resolved as BEAR.Resource resolves a link, under the context scheme (`#[ContextScheme]`), so the purge and the link crawl name the same resource; an href that does not expand to a resource URI and a `#[Link]` of another rel purge nothing. +- The href expands as a `#[Purge]` template does, from the body the write returns, then from its query. An href that expands to a path (`/articles`) takes the context scheme (`#[ContextScheme]`); an href that does not expand to a resource URI or an absolute path (`articles`), and a `#[Link]` of another rel, purge nothing. - Which side declares the relation: when the list knows its rows and their number is bounded, declare `#[DependsOn]` on the list; when the row knows its list and the rows are unbounded, declare `#[Link(rel: 'collection')]` on the row. The collection link keeps the list to its own key. - A write that moves a row from one list to another names both lists with `#[Purge]`. The old value comes from the body the write returns, under a name other than the argument's, as the body is read before the query: diff --git a/src/RefreshAnnotatedCommand.php b/src/RefreshAnnotatedCommand.php index 52b61a8f..e9365e5b 100644 --- a/src/RefreshAnnotatedCommand.php +++ b/src/RefreshAnnotatedCommand.php @@ -21,8 +21,12 @@ use function array_values; use function is_array; use function method_exists; +use function parse_url; +use function str_starts_with; use function uri_template; +use const PHP_URL_SCHEME; + /** @internal */ final readonly class RefreshAnnotatedCommand { @@ -125,7 +129,7 @@ private function uris(ResourceObject $ro, string $template, array $query): array } /** - * Resolved as BEAR\Resource\Linker resolves a link, so a path-only href takes the context scheme (#[ContextScheme]) + * A path-only href takes the context scheme (#[ContextScheme]); a relative href without a leading slash names nothing * * @param list $uris * @@ -135,6 +139,10 @@ private function resourceUris(array $uris): array { $resourceUris = []; foreach ($uris as $uri) { + if (parse_url($uri, PHP_URL_SCHEME) === null && ! str_starts_with($uri, '/')) { + continue; + } + try { $resourceUris[] = ($this->uriFactory)($uri); } catch (UriException) { diff --git a/tests/CollectionLinkTest.php b/tests/CollectionLinkTest.php index d43cceba..fee259f8 100644 --- a/tests/CollectionLinkTest.php +++ b/tests/CollectionLinkTest.php @@ -77,6 +77,17 @@ public function testHrefExpandingToAPathIsResolvedAndAnUnusableOneSkipped(): voi $this->assertNull($this->repository->get(new Uri($posts))); } + public function testRelativeHrefWithoutLeadingSlashIsSkipped(): void + { + $posts = 'app://self/collection/posts'; + $this->resource->get($posts); + + $ro = $this->resource->put('app://self/collection/note', ['id' => 1]); + + $this->assertSame(200, $ro->code); + $this->assertNotNull($this->repository->get(new Uri($posts))); + } + /** A row taken from the injector has no URI, and its path-only href still purges the list */ public function testPathOnlyHrefOfARowWithoutUriPurgesTheList(): void { diff --git a/tests/Fake/fake-app/src/Resource/App/Collection/Note.php b/tests/Fake/fake-app/src/Resource/App/Collection/Note.php new file mode 100644 index 00000000..74c65f4d --- /dev/null +++ b/tests/Fake/fake-app/src/Resource/App/Collection/Note.php @@ -0,0 +1,26 @@ +body = ['id' => $id]; + + return $this; + } + + public function onPut(int $id): static + { + $this->body = ['id' => $id]; + + return $this; + } +} From a649751665ca5a15019bc8b0ee720d36369faeb1 Mon Sep 17 00:00:00 2001 From: Akihito Koriyama Date: Tue, 24 Nov 2026 13:01:29 +0900 Subject: [PATCH 2/3] Skip a schemeless href that parse_url() rejects parse_url() returns false for `:80/collection/posts`, so the null check let it through; UriFactory then resolved it to page://self:80/collection/posts, which purged /collection/posts. --- src/RefreshAnnotatedCommand.php | 3 ++- tests/Fake/fake-app/src/Resource/App/Collection/Note.php | 1 + 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/src/RefreshAnnotatedCommand.php b/src/RefreshAnnotatedCommand.php index e9365e5b..6a2057ef 100644 --- a/src/RefreshAnnotatedCommand.php +++ b/src/RefreshAnnotatedCommand.php @@ -20,6 +20,7 @@ use function array_unique; use function array_values; use function is_array; +use function is_string; use function method_exists; use function parse_url; use function str_starts_with; @@ -139,7 +140,7 @@ private function resourceUris(array $uris): array { $resourceUris = []; foreach ($uris as $uri) { - if (parse_url($uri, PHP_URL_SCHEME) === null && ! str_starts_with($uri, '/')) { + if (! is_string(parse_url($uri, PHP_URL_SCHEME)) && ! str_starts_with($uri, '/')) { continue; } diff --git a/tests/Fake/fake-app/src/Resource/App/Collection/Note.php b/tests/Fake/fake-app/src/Resource/App/Collection/Note.php index 74c65f4d..7e78f67d 100644 --- a/tests/Fake/fake-app/src/Resource/App/Collection/Note.php +++ b/tests/Fake/fake-app/src/Resource/App/Collection/Note.php @@ -10,6 +10,7 @@ class Note extends ResourceObject { #[Link(rel: 'collection', href: 'x/collection/posts')] + #[Link(rel: 'collection', href: ':80/collection/posts')] public function onGet(int $id): static { $this->body = ['id' => $id]; From 3ad434ab974252a239f6645649475f38e112b6b0 Mon Sep 17 00:00:00 2001 From: Akihito Koriyama Date: Tue, 24 Nov 2026 13:11:45 +0900 Subject: [PATCH 3/3] Cover the UriException catch with an invalid href that has a scheme The empty href that reached the catch is now skipped before it. --- tests/Fake/fake-app/src/Resource/App/Collection/Comment.php | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/Fake/fake-app/src/Resource/App/Collection/Comment.php b/tests/Fake/fake-app/src/Resource/App/Collection/Comment.php index c4836f6c..039e023d 100644 --- a/tests/Fake/fake-app/src/Resource/App/Collection/Comment.php +++ b/tests/Fake/fake-app/src/Resource/App/Collection/Comment.php @@ -11,6 +11,7 @@ class Comment extends ResourceObject { #[Link(rel: 'collection', href: '{/kind}/posts')] #[Link(rel: 'collection', href: '')] + #[Link(rel: 'collection', href: 'app:/x')] public function onGet(int $id): static { $this->body = ['id' => $id];