Skip to content

fix(hydra): handle string and null serialization groups in DocumentationNormalizer#7890

Open
Roemerdt wants to merge 4 commits intoapi-platform:4.3from
Roemerdt:fix/hydra-string-groups
Open

fix(hydra): handle string and null serialization groups in DocumentationNormalizer#7890
Roemerdt wants to merge 4 commits intoapi-platform:4.3from
Roemerdt:fix/hydra-string-groups

Conversation

@Roemerdt
Copy link
Copy Markdown

Q A
Branch? 4.3
Tickets
License MIT
Doc PR

DocumentationNormalizer::getPropertyMetadataFactoryContext crashes when serialization groups are defined as a string rather than an array, or when no normalization/denormalization context is set.

Symfony's serializer supports string groups. AbstractNormalizer::getGroups() casts scalars to arrays, and the Symfony docs demonstrate the following is possible 'groups' => 'public_view'.

Reproducer:

#[ApiResource(
    normalizationContext: ['groups' => 'read'],
    denormalizationContext: ['groups' => 'write'],
)]
class DragonTreasure { ... }

Requesting the Hydra documentation endpoint produces:

  • Warning: foreach() argument must be of type array|object, string given (string groups passed to foreach)
  • Warning: Trying to access array offset on null (when context is null)

Fix:

  • Wrap getNormalizationContext() / getDenormalizationContext() with ?? [] to handle null contexts
  • Cast string group values to arrays before iterating, matching Symfony's AbstractNormalizer::getGroups() behaviour

@Roemerdt Roemerdt marked this pull request as draft March 28, 2026 15:36
@Roemerdt Roemerdt changed the title Fix/hydra string groups fix(hydra): handle string and null serialization groups in DocumentationNormalizer Mar 28, 2026
@Roemerdt Roemerdt marked this pull request as ready for review March 28, 2026 16:20
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