Skip to content

Don't use the ObjectMapNormalizer as a class normalizer - #243

Merged
DavidBadura merged 16 commits into
2.1.xfrom
object-map-class-normalizer
Nov 7, 2026
Merged

DavidBadura merged 16 commits into
2.1.xfrom
object-map-class-normalizer

Conversation

@DavidBadura

@DavidBadura DavidBadura commented Nov 6, 2026 •

Copy link
Copy Markdown
Member

Since 2.0 the class normalizer lookup walks interfaces and parents, so a concrete class picked up an ObjectMapNormalizer declared on its interface or abstract parent, as the docs show it, as its own normalizer. The map hands the object back to the hydrator for that same class, so these classes could not be extracted or hydrated at all (MissingHydrator, and behind it an endless recursion). Like the ObjectNormalizer, the map is now ignored on class level and only applies to properties typed as the interface or parent.

The swapped parameters of mergeMetadata() are fixed with the 1.24.4 merge-up (#246), so the second commit only adds the tests for the 2.x side, where the parent's normalizer won over the child's. With #[IdNormalizer] interface Id, abstract class BaseId implements Id and final class SubId extends BaseId, hydrate(SubId::class, '1') failed with "Cannot instantiate abstract class". The third commit drops the extras merge there, which only ever merged two empty arrays.

Release note: classes mapped through an interface or abstract parent can be extracted and hydrated directly again, as plain fields without the type field. A top level hydrate() of the interface itself stays unsupported and now fails with PHP's "Cannot instantiate interface" error. A child's own class normalizer and #[Lazy] now win over the parent's, the lazy precedence was inverted since 1.11. Metadata cached with the PSR-6 or PSR-16 factories keeps the old result until the cache is cleared.

@github-actions

github-actions Bot commented Nov 6, 2026 •

Copy link
Copy Markdown

Hello 👋

Here are the most recent benchmark results, compared against 2.1.x. Time is the mode over the iterations in the its column, with the relative standard deviation in brackets.

StackHydrator

subject revs its memory base current diff
benchHydrate1Object 1000 5 1.856mb -21.21% 2.564μs (±1.08%) 2.556μs (±1.32%) -0.31%
benchExtract1Object 1000 5 1.856mb 0.00% 2.781μs (±0.82%) 2.811μs (±13.60%) +1.11%
benchHydrate1000Objects 3 5 1.856mb 0.00% 2.475ms (±1.53%) 2.442ms (±1.64%) -1.35%
benchExtract1000Objects 3 5 1.856mb 0.00% 2.310ms (±0.54%) 2.301ms (±0.94%) -0.42%

StackHydratorWithCryptography

subject revs its memory base current diff
benchHydrate1Object 1000 5 1.856mb 0.00% 3.764μs (±1.41%) 3.766μs (±1.50%) +0.06%
benchExtract1Object 1000 5 1.856mb 0.00% 8.175μs (±1.17%) 8.179μs (±2.04%) +0.05%
benchHydrate1000Objects 3 5 1.856mb 0.00% 3.647ms (±0.44%) 3.627ms (±0.74%) -0.55%
benchExtract1000Objects 3 5 1.856mb 0.00% 7.679ms (±1.05%) 7.671ms (±0.91%) -0.10%

StackHydratorWithLazy

subject revs its memory base current diff
benchHydrate1Object 1000 5 1.856mb 0.00% 0.415μs (±3.18%) 0.460μs (±1.46%) +10.81%
benchHydrate1ObjectTriggerInit 1000 5 1.856mb 0.00% 3.017μs (±0.43%) 2.992μs (±1.08%) -0.82%
benchHydrate1000Objects 3 5 1.856mb 0.00% 428.813μs (±3.32%) 423.978μs (±1.66%) -1.13%
benchHydrate1000ObjectsTriggerInit 3 5 1.856mb 0.00% 3.001ms (±0.32%) 2.955ms (±1.78%) -1.54%

This comment gets updated every time a new commit comes in.

@DavidBadura DavidBadura added the bug Something isn't working label Nov 6, 2026
@DavidBadura DavidBadura added this to the 2.1.1 milestone Nov 6, 2026
ArrayShapeNormalizer only returned the keys it had an inner normalizer for,
so every other key of the shape was silently dropped on extract and hydrate.
Keys holding null were dropped too because of isset(). The normalizer now
works on the given array and only converts the keys it has a normalizer for,
null included, and keeps everything else as it is, like ArrayNormalizer and
plain arrays do.
The collection branch unwrapped the nullable item type from the collection
itself instead of from the item type, so collections like list<?Skill> got no
normalizer at all and their items were passed through as raw objects.
An explicit ArrayShapeNormalizer map can contain keys that the property's
docblock shape doesn't know. handleType read those keys anyway and raised
undefined key warnings. Such fields are now left alone, like the whole map is
when the property type isn't an array shape.
The collection and array shape branches unwrapped nullable item types and
looked for a class normalizer before recursing, although inferNormalizerByType
does both on its own. Dropping the duplicate removes the place where the
nullable item bug came from. The shape branch is now also covered in the
metadata factory test.
The parameter names of mergeMetadata() were swapped relative to the call site,
so the lazy flag of the parent took precedence over the child's. A class with
#[Lazy(false)] extending a #[Lazy] class was still hydrated lazily. Everything
else keeps its current order: properties, callbacks and the data subject id of
the child still come first.
getMethods() already returns the public and protected methods a class inherits,
and mergeMetadata() added the parent's callbacks on top, so an inherited
#[PostHydrate] or #[PreExtract] method was called twice. An overridden one ran
as the child's version and then again as the parent's, because the parent's
reflection method was invoked directly. The merge now resolves every callback to
the method PHP would call on the child and keeps it once. An override runs as
the child's version, also when it doesn't repeat the attribute. Private methods
of the parent are not inherited and stay as they are.
The array and array shape examples used #[ArrayNormalizer] and
#[ArrayShapeNormalizer] without arguments, which fails because both constructors
require the inner normalizers. In these cases the normalizer is inferred from
the docblock, so the attribute is dropped and the text says so. A few other
examples had typos that made them invalid PHP, like a misspelled class name,
missing semicolons and a match arm written as a case.
Normalizers get null for nullable properties and, since the array shape fix,
for null values in shapes as well, so the example should not reject it.
The class normalizer lookup walks interfaces and parent classes, so a concrete
class picked up a map declared on its interface or abstract parent as its own
normalizer. The map calls extract() and hydrate() on that same class again,
which made those classes impossible to extract or hydrate. Like the
ObjectNormalizer, the map is now ignored on class level and only applies to
properties.
The parameter fix itself came with the 1.24.4 merge-up. These tests pin it for
2.x, where it also decides the class normalizer: a class extending an abstract
class that implements an interface with a class normalizer gets the normalizer
bound to itself and can be hydrated.
The factory never sets extras, enrichers only run on the final metadata, so
both sides of the merge were always empty.
@DavidBadura
DavidBadura force-pushed the object-map-class-normalizer branch from 6f47f6b to 9e296d8 Compare November 7, 2026 10:33
@DavidBadura

Copy link
Copy Markdown
Member Author

Rebased on top of the 2.0.x merge-up in #246, so please merge that one first. Until then the diff here also shows the commits from #246.

@DavidBadura
DavidBadura merged commit 9ccfabd into 2.1.x Nov 7, 2026
17 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants