Skip to content

Commit 8d5af62

Browse files
Merge branch '6.4' into 7.2
* 6.4: [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 33734cd + 492c751 commit 8d5af62

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;
@@ -1069,6 +1070,30 @@ protected function createChildContext(array $parentContext, string $attribute, ?
10691070
return $context;
10701071
}
10711072

1073+
protected function getAllowedAttributes(string|object $classOrObject, array $context, bool $attributesAsString = false): array|bool
1074+
{
1075+
if (false === $allowedAttributes = parent::getAllowedAttributes($classOrObject, $context, $attributesAsString)) {
1076+
return false;
1077+
}
1078+
1079+
if (null !== $this->classDiscriminatorResolver) {
1080+
$class = \is_object($classOrObject) ? $classOrObject::class : $classOrObject;
1081+
if (null !== $discriminatorMapping = $this->classDiscriminatorResolver->getMappingForMappedObject($classOrObject)) {
1082+
$allowedAttributes[] = $attributesAsString ? $discriminatorMapping->getTypeProperty() : new AttributeMetadata($discriminatorMapping->getTypeProperty());
1083+
}
1084+
1085+
if (null !== $discriminatorMapping = $this->classDiscriminatorResolver->getMappingForClass($class)) {
1086+
$attributes = [];
1087+
foreach ($discriminatorMapping->getTypesMapping() as $mappedClass) {
1088+
$attributes[] = parent::getAllowedAttributes($mappedClass, $context, $attributesAsString);
1089+
}
1090+
$allowedAttributes = array_merge($allowedAttributes, ...$attributes);
1091+
}
1092+
}
1093+
1094+
return $allowedAttributes;
1095+
}
1096+
10721097
/**
10731098
* Builds the cache key for the attributes cache.
10741099
*

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 <[email protected]>
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;
@@ -1200,6 +1203,25 @@ public static function provideBooleanTypesData()
12001203
];
12011204
}
12021205

1206+
public function testDeserializeAndSerializeConstructorAndIgnoreAndInterfacedObjectsWithTheClassMetadataDiscriminator()
1207+
{
1208+
$example = new DummyMessageNumberFour('Hello');
1209+
1210+
$classMetadataFactory = new ClassMetadataFactory(new AttributeLoader());
1211+
1212+
$normalizer = new PropertyNormalizer(
1213+
$classMetadataFactory,
1214+
null,
1215+
new PropertyInfoExtractor([], [new PhpDocExtractor(), new ReflectionExtractor()]),
1216+
new ClassDiscriminatorFromClassMetadata($classMetadataFactory),
1217+
);
1218+
1219+
$serialized = $normalizer->normalize($example, 'json');
1220+
$deserialized = $normalizer->denormalize($serialized, DummyMessageInterface::class, 'json');
1221+
1222+
$this->assertEquals($example, $deserialized);
1223+
}
1224+
12031225
/**
12041226
* @dataProvider provideDenormalizeWithFilterBoolData
12051227
*/

0 commit comments

Comments
 (0)