Skip to content

Commit 044caa7

Browse files
committed
[Serializer] Convert names using denormalize during denormalization
1 parent a839bec commit 044caa7

File tree

2 files changed

+35
-0
lines changed

2 files changed

+35
-0
lines changed
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
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+
class Php80ReadonlyDummy
15+
{
16+
public function __construct(
17+
public readonly string $someValue
18+
) {
19+
}
20+
}

src/Symfony/Component/Serializer/Tests/Normalizer/AbstractNormalizerTest.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactory;
2121
use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactoryInterface;
2222
use Symfony\Component\Serializer\Mapping\Loader\LoaderChain;
23+
use Symfony\Component\Serializer\NameConverter\CamelCaseToSnakeCaseNameConverter;
2324
use Symfony\Component\Serializer\Normalizer\AbstractNormalizer;
2425
use Symfony\Component\Serializer\Normalizer\ObjectNormalizer;
2526
use Symfony\Component\Serializer\Normalizer\PropertyNormalizer;
@@ -29,6 +30,7 @@
2930
use Symfony\Component\Serializer\Tests\Fixtures\Dummy;
3031
use Symfony\Component\Serializer\Tests\Fixtures\NullableConstructorArgumentDummy;
3132
use Symfony\Component\Serializer\Tests\Fixtures\NullableOptionalConstructorArgumentDummy;
33+
use Symfony\Component\Serializer\Tests\Fixtures\Php80ReadonlyDummy;
3234
use Symfony\Component\Serializer\Tests\Fixtures\StaticConstructorDummy;
3335
use Symfony\Component\Serializer\Tests\Fixtures\StaticConstructorNormalizer;
3436
use Symfony\Component\Serializer\Tests\Fixtures\VariadicConstructorTypedArgsDummy;
@@ -233,6 +235,19 @@ public function testVariadicSerializationWithPreservingKeys(AbstractNormalizer $
233235
$this->assertEquals($arr, $dummy->getFoo());
234236
}
235237

238+
/**
239+
* @requires PHP 8
240+
*/
241+
public function testWithNameConverter()
242+
{
243+
$normalizer = new ObjectNormalizer(null, new CamelCaseToSnakeCaseNameConverter());
244+
245+
$object = new Php80ReadonlyDummy('foo');
246+
$this->assertEquals(['some_value' => 'foo'], $normalizer->normalize($object));
247+
$this->assertEquals($object, $normalizer->denormalize(['some_value' => 'foo'], Php80ReadonlyDummy::class));
248+
$this->assertEquals($object, $normalizer->denormalize(['someValue' => 'foo'], Php80ReadonlyDummy::class));
249+
}
250+
236251
public static function getNormalizer()
237252
{
238253
$extractor = new PhpDocExtractor();

0 commit comments

Comments
 (0)