Skip to content

Commit

Permalink
Only reverse order of serializers for promoted properties.
Browse files Browse the repository at this point in the history
  • Loading branch information
frankdejonge committed Jun 5, 2022
1 parent 0817787 commit 5b745cd
Show file tree
Hide file tree
Showing 3 changed files with 11 additions and 5 deletions.
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -612,10 +612,10 @@ It's important to know that serialization and hydration hooks are triggered befo
happens. If you wish to operate on serialized or hydrated data, you can hydrate/serialize the inner
data/objects.

### Caster and serializer order
### ⚠️ Caster and serializer order

In order to make hydration and serialization symmetrical (allowing back and forth conversion), the order
of serializers called is reversed.
of serializers called is reversed for _promoted_ properties.

## Maximizing performance

Expand Down
10 changes: 8 additions & 2 deletions src/DefinitionProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use ReflectionProperty;
use ReflectionUnionType;
use function array_key_exists;
use function array_reverse;
use function count;
use function is_a;

Expand Down Expand Up @@ -161,10 +162,15 @@ public function provideSerializationDefinition(string $className): ClassSerializ
$key = $this->keyFormatter->propertyNameToKey($property->getName());
$propertyType = $property->getType();
$attributes = $property->getAttributes();
$serializers = $this->resolveSerializers($propertyType, $attributes);

if ($property->isPromoted()) {
$serializers = array_reverse($serializers);
}

$properties[] = new PropertySerializationDefinition(
PropertySerializationDefinition::TYPE_PROPERTY,
$property->getName(),
$this->resolveSerializers($propertyType, $attributes),
$property->getName(), $serializers,
PropertyType::fromReflectionType($propertyType),
$propertyType->allowsNull(),
$this->resolveKeys($key, $attributes),
Expand Down
2 changes: 1 addition & 1 deletion src/PropertySerializationDefinition.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public function __construct(
public bool $nullable,
public array $keys = [],
) {
$this->serializers = array_reverse(array_filter($this->serializers));
$this->serializers = array_filter($this->serializers);
}

public function formattedAccessor(): string
Expand Down

0 comments on commit 5b745cd

Please sign in to comment.