Skip to content

fix(hydra): document sub-resource operations on parent link - #8876

Open
soyuka wants to merge 2 commits into
api-platform:mainfrom
soyuka:fix/hydra-subresource-link-operations
Open

soyuka wants to merge 2 commits into
api-platform:mainfrom
soyuka:fix/hydra-subresource-link-operations

Conversation

@soyuka

@soyuka soyuka commented Nov 26, 2026 •

Copy link
Copy Markdown
Member
Q A
Branch? main
Tickets
License MIT
Doc PR

A sub-resource collection (/books/{bookId}/reviews) is documented as #Entrypoint/review, a link the entrypoint never returns (GET /reviews is a 404), while #Book/reviews is a bare hydra:Link. A Hydra client can't know it may POST a review to a book's reviews link.

When a readable property declares ApiProperty(uriTemplate: ...) it serializes as the collection IRI (same lookup as AbstractItemNormalizer, matched on the URI template), so the POST and GET collection operations sharing that uriTemplate are now documented as supportedOperation on that hydra:Link (Hydra Core §4.2, example 12, and §7 "or the target of the Hydra link"). An @type set through jsonldContext is kept.

An operation is removed from #Entrypoint/<name> only when it is attached to a property this way and its URI template has variables, i.e. it is not reachable from the entrypoint (same as what EntrypointNormalizer lists). A top-level collection a property points to (/reviews) stays on the entrypoint and is also attached to the link. A sub-resource no property points to keeps its entrypoint entry, as it is the only place its POST/GET collection is documented. Without a property uriTemplate the output is unchanged. No change to Link declarations is needed.

api-platform/hydra now requires api-platform/metadata ^5.0.1 (UriTemplateHelper).

Targets main because the docs output changes.

Tests: src/Hydra/Tests/Serializer/DocumentationNormalizerTest.php (sub-resource linked, no property uriTemplate, top-level collection linked, operation of another resource on the same template, unreadable property, jsonldContext @type) and PropertyCollectionIriOnlyTest::testDocumentationAttachesUriTemplateOperationsToLinks on the existing fixtures.

A property with an ApiProperty uriTemplate serializes as the IRI of a
sub-resource collection. Expose that collection's POST and GET
operations as supportedOperation on the hydra:Link, and stop listing
the sub-resource as a phantom #Entrypoint property when they are
attached.
@soyuka
soyuka force-pushed the fix/hydra-subresource-link-operations branch from 9035c63 to d1a7256 Compare November 26, 2026 08:53
Only drop an operation from #Entrypoint when a property links to it
and its URI template has variables, keyed by short name, method and
template. Skip unreadable properties, keep a jsonldContext @type, and
require api-platform/metadata ^5.0.1 for UriTemplateHelper.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant