Repository navigation
Don't use the ObjectMapNormalizer as a class normalizer - #243
Merged
Merged
Conversation
|
Hello 👋 Here are the most recent benchmark results, compared against StackHydrator
StackHydratorWithCryptography
StackHydratorWithLazy
This comment gets updated every time a new commit comes in. |
DanielBadura
approved these changes
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.
…Zsb44N Merge release 1.24.4 into 2.0.x
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
force-pushed
the
object-map-class-normalizer
branch
from
November 7, 2026 10:33
6f47f6b to
9e296d8
Compare
Member
Author
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Since 2.0 the class normalizer lookup walks interfaces and parents, so a concrete class picked up an
ObjectMapNormalizerdeclared 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 theObjectNormalizer, 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 Idandfinal 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.