Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit a0b3913

Browse filesBrowse files
bug #52885 [Serializer] fix nullable int cannot be serialized (nikophil)
This PR was merged into the 5.4 branch. Discussion ---------- [Serializer] fix nullable int cannot be serialized | Q | A | ------------- | --- | Branch? | 5.4 | Bug fix? | yes | New feature? | no | Deprecations? | no | License | MIT Hello, previous to [this PR](#52172) such XML could be deserialized correctly, setting null in the value: ```xml <?xml version="1.0" encoding="UTF-8"?> <DummyNullableInt> <value/> </DummyNullableInt> ``` ```php class DummyNullableInt { public function __construct( public int|null $value = null ) {} } ``` but now it creates the following error: ``` Uninitialized string offset 0 /home/nikophil/works/symfony/src/Symfony/Component/Serializer/Normalizer/AbstractObjectNormalizer.php:495 /home/nikophil/works/symfony/src/Symfony/Component/Serializer/Normalizer/AbstractObjectNormalizer.php:630 /home/nikophil/works/symfony/src/Symfony/Component/Serializer/Normalizer/AbstractNormalizer.php:377 /home/nikophil/works/symfony/src/Symfony/Component/Serializer/Normalizer/AbstractObjectNormalizer.php:246 /home/nikophil/works/symfony/src/Symfony/Component/Serializer/Normalizer/AbstractObjectNormalizer.php:346 ``` I looked for any issue or PR mentioning this problem, but couldn't find it. So here is a fix ping `@Jeroeny` Commits ------- 5d62dea [Serializer] fix regression where nullable int cannot be serialized
2 parents a0e10fd + 5d62dea commit a0b3913
Copy full SHA for a0b3913

File tree

Expand file treeCollapse file tree

3 files changed

+36
-1
lines changed
Filter options
Expand file treeCollapse file tree

3 files changed

+36
-1
lines changed

‎src/Symfony/Component/Serializer/Normalizer/AbstractObjectNormalizer.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Serializer/Normalizer/AbstractObjectNormalizer.php
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -524,7 +524,7 @@ private function validateAndDenormalize(array $types, string $currentClass, stri
524524
}
525525
break;
526526
case Type::BUILTIN_TYPE_INT:
527-
if (ctype_digit($data) || '-' === $data[0] && ctype_digit(substr($data, 1))) {
527+
if (ctype_digit($data) || isset($data[0]) && '-' === $data[0] && ctype_digit(substr($data, 1))) {
528528
$data = (int) $data;
529529
} else {
530530
throw NotNormalizableValueException::createForUnexpectedDataType(sprintf('The type of the "%s" attribute for class "%s" must be int ("%s" given).', $attribute, $currentClass, $data), $data, [Type::BUILTIN_TYPE_INT], $context['deserialization_path'] ?? null);
+20Lines 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+
/**
15+
* @author Nicolas PHILIPPE <nikophil@gmail.com>
16+
*/
17+
class DummyNullableInt
18+
{
19+
public int|null $value = null;
20+
}

‎src/Symfony/Component/Serializer/Tests/SerializerTest.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Serializer/Tests/SerializerTest.php
+15Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
use Symfony\Component\Serializer\Encoder\DecoderInterface;
2222
use Symfony\Component\Serializer\Encoder\EncoderInterface;
2323
use Symfony\Component\Serializer\Encoder\JsonEncoder;
24+
use Symfony\Component\Serializer\Encoder\XmlEncoder;
2425
use Symfony\Component\Serializer\Exception\ExtraAttributesException;
2526
use Symfony\Component\Serializer\Exception\InvalidArgumentException;
2627
use Symfony\Component\Serializer\Exception\LogicException;
@@ -61,6 +62,7 @@
6162
use Symfony\Component\Serializer\Tests\Fixtures\DummyMessageNumberOne;
6263
use Symfony\Component\Serializer\Tests\Fixtures\DummyMessageNumberThree;
6364
use Symfony\Component\Serializer\Tests\Fixtures\DummyMessageNumberTwo;
65+
use Symfony\Component\Serializer\Tests\Fixtures\DummyNullableInt;
6466
use Symfony\Component\Serializer\Tests\Fixtures\DummyObjectWithEnumConstructor;
6567
use Symfony\Component\Serializer\Tests\Fixtures\DummyObjectWithEnumProperty;
6668
use Symfony\Component\Serializer\Tests\Fixtures\DummyWithObjectOrNull;
@@ -740,6 +742,19 @@ public function testDeserializeWrappedScalar()
740742
$this->assertSame(42, $serializer->deserialize('{"wrapper": 42}', 'int', 'json', [UnwrappingDenormalizer::UNWRAP_PATH => '[wrapper]']));
741743
}
742744

745+
/**
746+
* @requires PHP 8
747+
*/
748+
public function testDeserializeNullableIntInXml()
749+
{
750+
$extractor = new PropertyInfoExtractor([], [new ReflectionExtractor()]);
751+
$serializer = new Serializer([new ObjectNormalizer(null, null, null, $extractor)], ['xml' => new XmlEncoder()]);
752+
753+
$obj = $serializer->deserialize('<?xml version="1.0" encoding="UTF-8"?><DummyNullableInt><value/></DummyNullableInt>', DummyNullableInt::class, 'xml');
754+
$this->assertInstanceOf(DummyNullableInt::class, $obj);
755+
$this->assertNull($obj->value);
756+
}
757+
743758
public function testUnionTypeDeserializable()
744759
{
745760
$classMetadataFactory = new ClassMetadataFactory(new AnnotationLoader(new AnnotationReader()));

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.