Skip to content

Commit cd05dd6

Browse files
committed
Set current norm/denorm context directly on the TypeExtractor.
1 parent 536ebda commit cd05dd6

File tree

3 files changed

+27
-3
lines changed

3 files changed

+27
-3
lines changed

src/DoctrineEntityNormalizer.php

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,13 @@ public function normalize(
5959

6060
$context = $this->addDoctrineContext($object::class, $context);
6161

62-
return parent::normalize($object, $format, $context);
62+
$this->typeExtractor->setCurrentContext($context);
63+
64+
try {
65+
return parent::normalize($object, $format, $context);
66+
} finally {
67+
$this->typeExtractor->setCurrentContext();
68+
}
6369
}
6470

6571
/**
@@ -76,7 +82,13 @@ public function denormalize(mixed $data, string $type, ?string $format = null, a
7682
{
7783
$context = $this->addDoctrineContext($type, $context);
7884

79-
return parent::denormalize($data, $type, $format, $context);
85+
$this->typeExtractor->setCurrentContext($context);
86+
87+
try {
88+
return parent::denormalize($data, $type, $format, $context);
89+
} finally {
90+
$this->typeExtractor->setCurrentContext();
91+
}
8092
}
8193

8294
/**

src/TypeExtractor/EntityPropertyTypeResolver.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@
77
use Symfony\Component\TypeInfo\TypeContext\TypeContextFactory;
88
use Symfony\Component\TypeInfo\TypeResolver\TypeResolverInterface;
99

10+
/**
11+
* Resolves type for a given PropertyType reflection.
12+
*
13+
* TODO: This class is only overwritten to use getSettableType() instead of getType().
14+
*/
1015
final readonly class EntityPropertyTypeResolver implements TypeResolverInterface
1116
{
1217
public function __construct(

src/TypeExtractor/EntityTypeExtractor.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ final class EntityTypeExtractor implements PropertyTypeExtractorInterface
3030
'double' => TypeIdentifier::FLOAT->value,
3131
];
3232

33+
private ?array $currentContext = null;
34+
3335
public function __construct(
3436
?Inflector $inflector = null
3537
) {
@@ -48,6 +50,11 @@ public function __construct(
4850
$this->inflector = $inflector ?? InflectorFactory::create()->build();
4951
}
5052

53+
public function setCurrentContext(?array $value = null): void
54+
{
55+
$this->currentContext = $value;
56+
}
57+
5158
/**
5259
* @deprecated since Symfony 7.3, use "getType" instead
5360
*/
@@ -59,7 +66,7 @@ public function getTypes(string $class, string $property, array $context = []):
5966
public function getType(string $class, string $property, array $context = []): ?Type
6067
{
6168
// If the element is a Doctrine mapping, let the normalizer handle it.
62-
if (isset($context[DoctrineEntityNormalizer::ASSOCIATION_MAPPINGS][$property])) {
69+
if (isset($this->currentContext[DoctrineEntityNormalizer::ASSOCIATION_MAPPINGS][$property])) {
6370
return null;
6471
}
6572

0 commit comments

Comments
 (0)