Skip to content

Commit 566a1e0

Browse files
Merge branch '6.3' into 6.4
* 6.3: Add missing return type [HttpClient] Fix pausing responses before they start when using curl separate child and parent context in NotificationEmail on writes [Mailer] [Mailgun] Fix sender header encoding do not overwrite the cache key when it is false [Mailer] Throw TransportException when unable to read from socket [Serializer] Rewrite `AbstractObjectNormalizer::createChildContext()` to use the provided `cache_key` from original context when creating child contexts [HttpClient] Fix error chunk creation in passthru Adjusting and removing the 'review' attribute from the pt_br translation XML. [Serializer] Take unnamed variadic parameters into account when denormalizing Fix context data and display extra data
2 parents 27c8b3a + 7888af1 commit 566a1e0

File tree

5 files changed

+180
-2
lines changed

5 files changed

+180
-2
lines changed

Normalizer/AbstractNormalizer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -361,7 +361,7 @@ protected function instantiateObject(array &$data, string $class, array &$contex
361361
$variadicParameters[$parameterKey] = $this->denormalizeParameter($reflectionClass, $constructorParameter, $paramName, $parameterData, $attributeContext, $format);
362362
}
363363

364-
$params = array_merge($params, $variadicParameters);
364+
$params = array_merge(array_values($params), $variadicParameters);
365365
$unsetKeys[] = $key;
366366
}
367367
} elseif ($allowed && !$ignored && (isset($data[$key]) || \array_key_exists($key, $data))) {

Normalizer/AbstractObjectNormalizer.php

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -734,7 +734,11 @@ private function isMaxDepthReached(array $attributesMetadata, string $class, str
734734
protected function createChildContext(array $parentContext, string $attribute, ?string $format): array
735735
{
736736
$context = parent::createChildContext($parentContext, $attribute, $format);
737-
$context['cache_key'] = $this->getCacheKey($format, $context);
737+
if ($context['cache_key'] ?? false) {
738+
$context['cache_key'] .= '-'.$attribute;
739+
} elseif (false !== ($context['cache_key'] ?? null)) {
740+
$context['cache_key'] = $this->getCacheKey($format, $context);
741+
}
738742

739743
return $context;
740744
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
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+
class DummyWithWithVariadicParameterConstructor
15+
{
16+
private $foo;
17+
18+
private $bar;
19+
20+
private $baz;
21+
22+
public function __construct(string $foo, int $bar = 1, Dummy ...$baz)
23+
{
24+
$this->foo = $foo;
25+
$this->bar = $bar;
26+
$this->baz = $baz;
27+
}
28+
29+
public function getFoo(): string
30+
{
31+
return $this->foo;
32+
}
33+
34+
public function getBar(): int
35+
{
36+
return $this->bar;
37+
}
38+
39+
/** @return Dummy[] */
40+
public function getBaz(): array
41+
{
42+
return $this->baz;
43+
}
44+
}

Tests/Normalizer/AbstractNormalizerTest.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
use Symfony\Component\Serializer\Tests\Fixtures\AbstractNormalizerDummy;
3131
use Symfony\Component\Serializer\Tests\Fixtures\Attributes\IgnoreDummy;
3232
use Symfony\Component\Serializer\Tests\Fixtures\Dummy;
33+
use Symfony\Component\Serializer\Tests\Fixtures\DummyWithWithVariadicParameterConstructor;
3334
use Symfony\Component\Serializer\Tests\Fixtures\NullableConstructorArgumentDummy;
3435
use Symfony\Component\Serializer\Tests\Fixtures\NullableOptionalConstructorArgumentDummy;
3536
use Symfony\Component\Serializer\Tests\Fixtures\StaticConstructorDummy;
@@ -248,6 +249,25 @@ public static function getNormalizer()
248249
yield [new ObjectNormalizer(null, null, null, $extractor)];
249250
}
250251

252+
public function testVariadicConstructorDenormalization()
253+
{
254+
$data = [
255+
'foo' => 'woo',
256+
'baz' => [
257+
['foo' => null, 'bar' => null, 'baz' => null, 'qux' => null],
258+
['foo' => null, 'bar' => null, 'baz' => null, 'qux' => null],
259+
],
260+
];
261+
262+
$normalizer = new ObjectNormalizer();
263+
$normalizer->setSerializer(new Serializer([$normalizer]));
264+
265+
$expected = new DummyWithWithVariadicParameterConstructor('woo', 1, new Dummy(), new Dummy());
266+
$actual = $normalizer->denormalize($data, DummyWithWithVariadicParameterConstructor::class);
267+
268+
$this->assertEquals($expected, $actual);
269+
}
270+
251271
public static function getNormalizerWithCustomNameConverter()
252272
{
253273
$extractor = new PhpDocExtractor();

Tests/Normalizer/AbstractObjectNormalizerTest.php

Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -915,6 +915,116 @@ public function testDenormalizeUntypedStringObject()
915915
$this->assertEquals(new DummyWithStringObject(new DummyString()), $actual);
916916
$this->assertEquals('', $actual->value->value);
917917
}
918+
919+
public function testProvidingContextCacheKeyGeneratesSameChildContextCacheKey()
920+
{
921+
$foobar = new Dummy();
922+
$foobar->foo = new EmptyDummy();
923+
$foobar->bar = 'bar';
924+
$foobar->baz = 'baz';
925+
926+
$normalizer = new class() extends AbstractObjectNormalizerDummy {
927+
public $childContextCacheKey;
928+
929+
protected function extractAttributes(object $object, string $format = null, array $context = []): array
930+
{
931+
return array_keys((array) $object);
932+
}
933+
934+
protected function getAttributeValue(object $object, string $attribute, string $format = null, array $context = [])
935+
{
936+
return $object->{$attribute};
937+
}
938+
939+
protected function createChildContext(array $parentContext, string $attribute, ?string $format): array
940+
{
941+
$childContext = parent::createChildContext($parentContext, $attribute, $format);
942+
$this->childContextCacheKey = $childContext['cache_key'];
943+
944+
return $childContext;
945+
}
946+
};
947+
948+
$serializer = new Serializer([$normalizer]);
949+
950+
$serializer->normalize($foobar, null, ['cache_key' => 'hardcoded', 'iri' => '/dummy/1']);
951+
$firstChildContextCacheKey = $normalizer->childContextCacheKey;
952+
953+
$serializer->normalize($foobar, null, ['cache_key' => 'hardcoded', 'iri' => '/dummy/2']);
954+
$secondChildContextCacheKey = $normalizer->childContextCacheKey;
955+
956+
$this->assertSame($firstChildContextCacheKey, $secondChildContextCacheKey);
957+
}
958+
959+
public function testChildContextKeepsOriginalContextCacheKey()
960+
{
961+
$foobar = new Dummy();
962+
$foobar->foo = new EmptyDummy();
963+
$foobar->bar = 'bar';
964+
$foobar->baz = 'baz';
965+
966+
$normalizer = new class() extends AbstractObjectNormalizerDummy {
967+
public $childContextCacheKey;
968+
969+
protected function extractAttributes(object $object, string $format = null, array $context = []): array
970+
{
971+
return array_keys((array) $object);
972+
}
973+
974+
protected function getAttributeValue(object $object, string $attribute, string $format = null, array $context = [])
975+
{
976+
return $object->{$attribute};
977+
}
978+
979+
protected function createChildContext(array $parentContext, string $attribute, ?string $format): array
980+
{
981+
$childContext = parent::createChildContext($parentContext, $attribute, $format);
982+
$this->childContextCacheKey = $childContext['cache_key'];
983+
984+
return $childContext;
985+
}
986+
};
987+
988+
$serializer = new Serializer([$normalizer]);
989+
$serializer->normalize($foobar, null, ['cache_key' => 'hardcoded', 'iri' => '/dummy/1']);
990+
991+
$this->assertSame('hardcoded-foo', $normalizer->childContextCacheKey);
992+
}
993+
994+
public function testChildContextCacheKeyStaysFalseWhenOriginalCacheKeyIsFalse()
995+
{
996+
$foobar = new Dummy();
997+
$foobar->foo = new EmptyDummy();
998+
$foobar->bar = 'bar';
999+
$foobar->baz = 'baz';
1000+
1001+
$normalizer = new class() extends AbstractObjectNormalizerDummy {
1002+
public $childContextCacheKey;
1003+
1004+
protected function extractAttributes(object $object, string $format = null, array $context = []): array
1005+
{
1006+
return array_keys((array) $object);
1007+
}
1008+
1009+
protected function getAttributeValue(object $object, string $attribute, string $format = null, array $context = [])
1010+
{
1011+
return $object->{$attribute};
1012+
}
1013+
1014+
protected function createChildContext(array $parentContext, string $attribute, ?string $format): array
1015+
{
1016+
$childContext = parent::createChildContext($parentContext, $attribute, $format);
1017+
$this->childContextCacheKey = $childContext['cache_key'];
1018+
1019+
return $childContext;
1020+
}
1021+
};
1022+
1023+
$serializer = new Serializer([$normalizer]);
1024+
$serializer->normalize($foobar, null, ['cache_key' => false]);
1025+
1026+
$this->assertFalse($normalizer->childContextCacheKey);
1027+
}
9181028
}
9191029

9201030
class AbstractObjectNormalizerDummy extends AbstractObjectNormalizer

0 commit comments

Comments
 (0)