Skip to content

Commit 5d62dea

Browse files
committed
[Serializer] fix regression where nullable int cannot be serialized
1 parent a0e10fd commit 5d62dea

File tree

3 files changed

+36
-1
lines changed

3 files changed

+36
-1
lines changed

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

Lines 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);
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+
/**
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

Lines 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)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy