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 c3c55e8

Browse filesBrowse files
committed
[Serializer] fix regression where nullable int cannot be serialized
1 parent abf4125 commit c3c55e8
Copy full SHA for c3c55e8

File tree

3 files changed

+37
-6
lines changed
Filter options

3 files changed

+37
-6
lines changed

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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Serializer/Normalizer/AbstractObjectNormalizer.php
+1-6Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -292,8 +292,6 @@ abstract protected function extractAttributes(object $object, string $format = n
292292

293293
/**
294294
* Gets the attribute value.
295-
*
296-
* @return mixed
297295
*/
298296
abstract protected function getAttributeValue(object $object, string $attribute, string $format = null, array $context = []);
299297

@@ -307,9 +305,6 @@ public function supportsDenormalization(mixed $data, string $type, string $forma
307305
return class_exists($type) || (interface_exists($type, false) && null !== $this->classDiscriminatorResolver?->getMappingForClass($type));
308306
}
309307

310-
/**
311-
* @return mixed
312-
*/
313308
public function denormalize(mixed $data, string $type, string $format = null, array $context = [])
314309
{
315310
if (!isset($context['cache_key'])) {
@@ -491,7 +486,7 @@ private function validateAndDenormalize(array $types, string $currentClass, stri
491486
}
492487
break;
493488
case Type::BUILTIN_TYPE_INT:
494-
if (ctype_digit('-' === $data[0] ? substr($data, 1) : $data)) {
489+
if (isset($data[0]) && ctype_digit('-' === $data[0] ? substr($data, 1) : $data)) {
495490
$data = (int) $data;
496491
} else {
497492
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);
+24Lines 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

Copy file name to clipboardExpand all lines: src/Symfony/Component/Serializer/Tests/SerializerTest.php
+12Lines 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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.