Skip to content

fix(serializer): honor readableLink false for array-backed relation getters#8256

Open
soyuka wants to merge 2 commits into
api-platform:4.3from
soyuka:fix/readable-link-array-collection-8179-v2
Open

fix(serializer): honor readableLink false for array-backed relation getters#8256
soyuka wants to merge 2 commits into
api-platform:4.3from
soyuka:fix/readable-link-array-collection-8179-v2

Conversation

@soyuka
Copy link
Copy Markdown
Member

@soyuka soyuka commented Jun 5, 2026

Summary

When a relation getter returns plain array (no phpstan/phpdoc-parser to refine the generic type), PropertyInfo only reports array<mixed>. The to-many resource arm of AbstractItemNormalizer::getAttributeValue misses the property and delegates to the inner serializer, which embeds the related resources even when readableLink: false. The fix inspects the runtime values: if any are resource objects and the user opted out of embedding, normalize the collection via normalizeCollectionOfRelations so IRIs are emitted.

Reproduction

A resource property exposed via public function getExchangeApis(): array with #[ApiProperty(readableLink: false)] still emitted embedded JSON-LD objects instead of IRI references.

Test plan

  • Added failing test covering array-backed getter + readableLink: false.
  • Test passes after the fix.
  • Surrounding serializer suite passes locally (43 tests).

Fixes #8179

soyuka added 2 commits June 5, 2026 17:39
…etters

When a property is exposed via an array-typed getter (e.g.
`getExchangeApis(): array`) without phpstan/phpdoc-parser installed,
PropertyInfo can only report `array<mixed>`. The to-many resource arm in
AbstractItemNormalizer::getAttributeValue therefore missed the property
and delegated to the inner serializer, which embedded the related
resources even though the user set `readableLink: false`. When the user
opts out of embedding and the runtime value is iterable, inspect the
items: if any are resource objects, normalize the collection via
normalizeCollectionOfRelations so IRIs are emitted.

Fixes api-platform#8179
…ters

Reproduces api-platform#8179 end-to-end: a Client resource exposes an array-backed
getter `getExchangeApis(): array` with `readableLink: false` and the
response must surface IRIs, not embedded objects. The companion to-one
relation acts as a sanity case.

The fixture has no `@return list<Api>` phpdoc on the getter on purpose,
so PropertyInfo cannot refine the element type even with
phpstan/phpdoc-parser installed — that is the exact path the production
fix in a6ed7ac closes.
@soyuka soyuka force-pushed the fix/readable-link-array-collection-8179-v2 branch from a6ed7ac to 6cb8079 Compare June 5, 2026 15:39
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