Skip to content

Commit b407e62

Browse files
committed
[Serializer] fix regression where nullable int cannot be serialized
1 parent abf4125 commit b407e62

File tree

3 files changed

+37
-1
lines changed

3 files changed

+37
-1
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -491,7 +491,7 @@ private function validateAndDenormalize(array $types, string $currentClass, stri
491491
}
492492
break;
493493
case Type::BUILTIN_TYPE_INT:
494-
if (ctype_digit('-' === $data[0] ? substr($data, 1) : $data)) {
494+
if (isset($data[0]) && ctype_digit('-' === $data[0] ? substr($data, 1) : $data)) {
495495
$data = (int) $data;
496496
} else {
497497
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: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
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 function __construct(
20+
public int|null $value = null
21+
)
22+
{
23+
}
24+
}

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
use Symfony\Component\PropertyInfo\PropertyInfoExtractor;
1919
use Symfony\Component\Serializer\Encoder\CsvEncoder;
2020
use Symfony\Component\Serializer\Encoder\JsonEncoder;
21+
use Symfony\Component\Serializer\Encoder\XmlEncoder;
2122
use Symfony\Component\Serializer\Exception\ExtraAttributesException;
2223
use Symfony\Component\Serializer\Exception\InvalidArgumentException;
2324
use Symfony\Component\Serializer\Exception\LogicException;
@@ -56,6 +57,7 @@
5657
use Symfony\Component\Serializer\Tests\Fixtures\DummyMessageNumberOne;
5758
use Symfony\Component\Serializer\Tests\Fixtures\DummyMessageNumberThree;
5859
use Symfony\Component\Serializer\Tests\Fixtures\DummyMessageNumberTwo;
60+
use Symfony\Component\Serializer\Tests\Fixtures\DummyNullableInt;
5961
use Symfony\Component\Serializer\Tests\Fixtures\DummyObjectWithEnumConstructor;
6062
use Symfony\Component\Serializer\Tests\Fixtures\DummyObjectWithEnumProperty;
6163
use Symfony\Component\Serializer\Tests\Fixtures\DummyWithObjectOrNull;
@@ -751,6 +753,16 @@ public function testDeserializeWrappedScalar()
751753
$this->assertSame(42, $serializer->deserialize('{"wrapper": 42}', 'int', 'json', [UnwrappingDenormalizer::UNWRAP_PATH => '[wrapper]']));
752754
}
753755

756+
public function testDeserializeNullableIntInXml()
757+
{
758+
$extractor = new PropertyInfoExtractor([], [new ReflectionExtractor()]);
759+
$serializer = new Serializer([new ObjectNormalizer(null, null, null, $extractor)], ['xml' => new XmlEncoder()]);
760+
761+
$obj = $serializer->deserialize('<?xml version="1.0" encoding="UTF-8"?><DummyNullableInt><value/></DummyNullableInt>', DummyNullableInt::class, 'xml',);
762+
$this->assertInstanceOf(DummyNullableInt::class, $obj);
763+
$this->assertNull($obj->value);
764+
}
765+
754766
public function testUnionTypeDeserializable()
755767
{
756768
$classMetadataFactory = new ClassMetadataFactory(new AttributeLoader());

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