Skip to content

Commit d3cf71f

Browse files
Merge branch '7.2' into 7.3
* 7.2: [Validator] Remove comment to GitHub issue [Serializer] Add support for discriminator map in property normalizer [DependencyInjection] Fix inlining when public services are involved fix contracts directory name Fix TraceableSerializer when collected caller from array map [HttpClient] Limit curl's connection cache size [FrameworkBundle] Fix argument not provided to `add_bus_name_stamp_middleware`
2 parents 2d86f81 + 8d5af62 commit d3cf71f

File tree

7 files changed

+113
-27
lines changed

7 files changed

+113
-27
lines changed

Debug/TraceableSerializer.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -171,8 +171,8 @@ private function getCaller(string $method, string $interface): array
171171
&& $method === $trace[$i]['function']
172172
&& is_a($trace[$i]['class'], $interface, true)
173173
) {
174-
$file = $trace[$i]['file'];
175-
$line = $trace[$i]['line'];
174+
$file = $trace[$i]['file'] ?? $trace[$i + 1]['file'];
175+
$line = $trace[$i]['line'] ?? $trace[$i + 1]['line'];
176176

177177
break;
178178
}

Normalizer/AbstractObjectNormalizer.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
use Symfony\Component\Serializer\Exception\LogicException;
2727
use Symfony\Component\Serializer\Exception\MissingConstructorArgumentsException;
2828
use Symfony\Component\Serializer\Exception\NotNormalizableValueException;
29+
use Symfony\Component\Serializer\Mapping\AttributeMetadata;
2930
use Symfony\Component\Serializer\Mapping\AttributeMetadataInterface;
3031
use Symfony\Component\Serializer\Mapping\ClassDiscriminatorFromClassMetadata;
3132
use Symfony\Component\Serializer\Mapping\ClassDiscriminatorResolverInterface;
@@ -1093,6 +1094,30 @@ protected function createChildContext(array $parentContext, string $attribute, ?
10931094
return $context;
10941095
}
10951096

1097+
protected function getAllowedAttributes(string|object $classOrObject, array $context, bool $attributesAsString = false): array|bool
1098+
{
1099+
if (false === $allowedAttributes = parent::getAllowedAttributes($classOrObject, $context, $attributesAsString)) {
1100+
return false;
1101+
}
1102+
1103+
if (null !== $this->classDiscriminatorResolver) {
1104+
$class = \is_object($classOrObject) ? $classOrObject::class : $classOrObject;
1105+
if (null !== $discriminatorMapping = $this->classDiscriminatorResolver->getMappingForMappedObject($classOrObject)) {
1106+
$allowedAttributes[] = $attributesAsString ? $discriminatorMapping->getTypeProperty() : new AttributeMetadata($discriminatorMapping->getTypeProperty());
1107+
}
1108+
1109+
if (null !== $discriminatorMapping = $this->classDiscriminatorResolver->getMappingForClass($class)) {
1110+
$attributes = [];
1111+
foreach ($discriminatorMapping->getTypesMapping() as $mappedClass) {
1112+
$attributes[] = parent::getAllowedAttributes($mappedClass, $context, $attributesAsString);
1113+
}
1114+
$allowedAttributes = array_merge($allowedAttributes, ...$attributes);
1115+
}
1116+
}
1117+
1118+
return $allowedAttributes;
1119+
}
1120+
10961121
/**
10971122
* Builds the cache key for the attributes cache.
10981123
*

Normalizer/ObjectNormalizer.php

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
use Symfony\Component\PropertyInfo\PropertyWriteInfo;
2121
use Symfony\Component\Serializer\Annotation\Ignore;
2222
use Symfony\Component\Serializer\Exception\LogicException;
23-
use Symfony\Component\Serializer\Mapping\AttributeMetadata;
2423
use Symfony\Component\Serializer\Mapping\ClassDiscriminatorResolverInterface;
2524
use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactoryInterface;
2625
use Symfony\Component\Serializer\NameConverter\NameConverterInterface;
@@ -149,30 +148,6 @@ protected function setAttributeValue(object $object, string $attribute, mixed $v
149148
}
150149
}
151150

152-
protected function getAllowedAttributes(string|object $classOrObject, array $context, bool $attributesAsString = false): array|bool
153-
{
154-
if (false === $allowedAttributes = parent::getAllowedAttributes($classOrObject, $context, $attributesAsString)) {
155-
return false;
156-
}
157-
158-
if (null !== $this->classDiscriminatorResolver) {
159-
$class = \is_object($classOrObject) ? $classOrObject::class : $classOrObject;
160-
if (null !== $discriminatorMapping = $this->classDiscriminatorResolver->getMappingForMappedObject($classOrObject)) {
161-
$allowedAttributes[] = $attributesAsString ? $discriminatorMapping->getTypeProperty() : new AttributeMetadata($discriminatorMapping->getTypeProperty());
162-
}
163-
164-
if (null !== $discriminatorMapping = $this->classDiscriminatorResolver->getMappingForClass($class)) {
165-
$attributes = [];
166-
foreach ($discriminatorMapping->getTypesMapping() as $mappedClass) {
167-
$attributes[] = parent::getAllowedAttributes($mappedClass, $context, $attributesAsString);
168-
}
169-
$allowedAttributes = array_merge($allowedAttributes, ...$attributes);
170-
}
171-
}
172-
173-
return $allowedAttributes;
174-
}
175-
176151
protected function isAllowedAttribute($classOrObject, string $attribute, ?string $format = null, array $context = []): bool
177152
{
178153
if (!parent::isAllowedAttribute($classOrObject, $attribute, $format, $context)) {

Tests/Debug/TraceableSerializerTest.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,40 @@ public function testAddDebugTraceIdInContext()
128128
$traceableSerializer->encode('data', 'format');
129129
$traceableSerializer->decode('data', 'format');
130130
}
131+
132+
public function testCollectedCaller()
133+
{
134+
$serializer = new \Symfony\Component\Serializer\Serializer();
135+
136+
$collector = new SerializerDataCollector();
137+
$traceableSerializer = new TraceableSerializer($serializer, $collector);
138+
139+
$traceableSerializer->normalize('data');
140+
$collector->lateCollect();
141+
142+
$this->assertSame([
143+
'name' => 'TraceableSerializerTest.php',
144+
'file' => __FILE__,
145+
'line' => __LINE__ - 6,
146+
], $collector->getData()['normalize'][0]['caller']);
147+
}
148+
149+
public function testCollectedCallerFromArrayMap()
150+
{
151+
$serializer = new \Symfony\Component\Serializer\Serializer();
152+
153+
$collector = new SerializerDataCollector();
154+
$traceableSerializer = new TraceableSerializer($serializer, $collector);
155+
156+
array_map([$traceableSerializer, 'normalize'], ['data']);
157+
$collector->lateCollect();
158+
159+
$this->assertSame([
160+
'name' => 'TraceableSerializerTest.php',
161+
'file' => __FILE__,
162+
'line' => __LINE__ - 6,
163+
], $collector->getData()['normalize'][0]['caller']);
164+
}
131165
}
132166

133167
class Serializer implements SerializerInterface, NormalizerInterface, DenormalizerInterface, EncoderInterface, DecoderInterface

Tests/Fixtures/DummyMessageInterface.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
'one' => DummyMessageNumberOne::class,
2121
'two' => DummyMessageNumberTwo::class,
2222
'three' => DummyMessageNumberThree::class,
23+
'four' => DummyMessageNumberFour::class,
2324
])]
2425
interface DummyMessageInterface
2526
{
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Serializer\Tests\Fixtures;
13+
14+
use Symfony\Component\Serializer\Attribute\Ignore;
15+
16+
abstract class SomeAbstract {
17+
#[Ignore]
18+
public function getDescription()
19+
{
20+
return 'Hello, World!';
21+
}
22+
}
23+
24+
class DummyMessageNumberFour extends SomeAbstract implements DummyMessageInterface
25+
{
26+
public function __construct(public $one)
27+
{
28+
}
29+
}

Tests/Normalizer/AbstractObjectNormalizerTest.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,16 @@
4545
use Symfony\Component\Serializer\Normalizer\DateTimeNormalizer;
4646
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
4747
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
48+
use Symfony\Component\Serializer\Normalizer\PropertyNormalizer;
4849
use Symfony\Component\Serializer\Serializer;
4950
use Symfony\Component\Serializer\SerializerAwareInterface;
5051
use Symfony\Component\Serializer\SerializerInterface;
5152
use Symfony\Component\Serializer\Tests\Fixtures\Attributes\AbstractDummy;
5253
use Symfony\Component\Serializer\Tests\Fixtures\Attributes\AbstractDummyFirstChild;
5354
use Symfony\Component\Serializer\Tests\Fixtures\Attributes\AbstractDummySecondChild;
5455
use Symfony\Component\Serializer\Tests\Fixtures\DummyFirstChildQuux;
56+
use Symfony\Component\Serializer\Tests\Fixtures\DummyMessageInterface;
57+
use Symfony\Component\Serializer\Tests\Fixtures\DummyMessageNumberFour;
5558
use Symfony\Component\Serializer\Tests\Fixtures\DummySecondChildQuux;
5659
use Symfony\Component\Serializer\Tests\Fixtures\DummyString;
5760
use Symfony\Component\Serializer\Tests\Fixtures\DummyWithNotNormalizable;
@@ -1235,6 +1238,25 @@ public static function provideBooleanTypesData()
12351238
];
12361239
}
12371240

1241+
public function testDeserializeAndSerializeConstructorAndIgnoreAndInterfacedObjectsWithTheClassMetadataDiscriminator()
1242+
{
1243+
$example = new DummyMessageNumberFour('Hello');
1244+
1245+
$classMetadataFactory = new ClassMetadataFactory(new AttributeLoader());
1246+
1247+
$normalizer = new PropertyNormalizer(
1248+
$classMetadataFactory,
1249+
null,
1250+
new PropertyInfoExtractor([], [new PhpDocExtractor(), new ReflectionExtractor()]),
1251+
new ClassDiscriminatorFromClassMetadata($classMetadataFactory),
1252+
);
1253+
1254+
$serialized = $normalizer->normalize($example, 'json');
1255+
$deserialized = $normalizer->denormalize($serialized, DummyMessageInterface::class, 'json');
1256+
1257+
$this->assertEquals($example, $deserialized);
1258+
}
1259+
12381260
/**
12391261
* @dataProvider provideDenormalizeWithFilterBoolData
12401262
*/

0 commit comments

Comments
 (0)