Skip to content

fix(symfony): keep error serialization mapping when enable_attributes is disabled#8231

Merged
soyuka merged 1 commit into
api-platform:4.3from
soyuka:fix/enable-attributes-false-error-8174
Jun 3, 2026
Merged

fix(symfony): keep error serialization mapping when enable_attributes is disabled#8231
soyuka merged 1 commit into
api-platform:4.3from
soyuka:fix/enable-attributes-false-error-8174

Conversation

@soyuka

@soyuka soyuka commented Jun 3, 2026

Copy link
Copy Markdown
Member

Summary

When framework.serializer.enable_attributes: false, Symfony's attribute loader is configured with no mapped classes and returns early for every class, so api-platform's Error and ValidationException lose the #[Groups]/#[SerializedName]/#[Ignore] metadata that the error normalizers rely on, leaving error responses empty.

Reproduction

Set framework.serializer.enable_attributes: false on the demo project and trigger a validation error — the response body is empty instead of the structured error payload.

Test plan

  • Added failing test covering the bug.
  • Test passes after the fix.
  • Related test classes still pass locally.

Fixes #8174

… is disabled

When `framework.serializer.enable_attributes: false`, Symfony registers its built-in
attribute loader with `allowAnyClass: false` and no mapped classes, so it returns
early for every class. As a result, api-platform's `Error` and `ValidationException`
lose their `#[Groups]`, `#[SerializedName]`, and `#[Ignore]` metadata, and the
problem/hydra/json:api error normalizers emit an empty payload.

Register a dedicated `AttributeLoader` in `serializer.mapping.chain_loader` (and the
cache warmer) that hard-codes the api-platform error classes via the `mappedClasses`
argument. The loader runs regardless of the global `enable_attributes` flag, so error
responses keep their structure without re-enabling attribute discovery for user
classes.

Fixes api-platform#8174
@soyuka soyuka merged commit 277589d into api-platform:4.3 Jun 3, 2026
107 of 108 checks passed
@soyuka soyuka deleted the fix/enable-attributes-false-error-8174 branch June 3, 2026 14:45
soyuka added a commit to soyuka/core that referenced this pull request Jun 5, 2026
The pass introduced in api-platform#8231 wires AttributeLoader with the Symfony 7.x
constructor signature (bool $allowAnyClass, array $mappedClasses). On
Symfony 6.4 the constructor is (?Doctrine\Common\Annotations\Reader),
so cache warmup crashed with a TypeError on every container build.

Guard the pass via class_exists(AnnotationLoader::class) — that class
only exists on the 6.4 branch (removed in 7.0). Also skip the pass when
an AttributeLoader is already present in the chain (enable_attributes:
true) to avoid duplicating work the framework has already wired.

Fixes api-platform#8244
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