Movatterモバイル変換


[0]ホーム

URL:


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

Sign up
Appearance settings

Commitb407e62

Browse files
committed
[Serializer] fix regression where nullable int cannot be serialized
1 parentabf4125 commitb407e62

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+
namespaceSymfony\Component\Serializer\Tests\Fixtures;
13+
14+
/**
15+
* @author Nicolas PHILIPPE <nikophil@gmail.com>
16+
*/
17+
class DummyNullableInt
18+
{
19+
publicfunction__construct(
20+
publicint|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
useSymfony\Component\PropertyInfo\PropertyInfoExtractor;
1919
useSymfony\Component\Serializer\Encoder\CsvEncoder;
2020
useSymfony\Component\Serializer\Encoder\JsonEncoder;
21+
useSymfony\Component\Serializer\Encoder\XmlEncoder;
2122
useSymfony\Component\Serializer\Exception\ExtraAttributesException;
2223
useSymfony\Component\Serializer\Exception\InvalidArgumentException;
2324
useSymfony\Component\Serializer\Exception\LogicException;
@@ -56,6 +57,7 @@
5657
useSymfony\Component\Serializer\Tests\Fixtures\DummyMessageNumberOne;
5758
useSymfony\Component\Serializer\Tests\Fixtures\DummyMessageNumberThree;
5859
useSymfony\Component\Serializer\Tests\Fixtures\DummyMessageNumberTwo;
60+
useSymfony\Component\Serializer\Tests\Fixtures\DummyNullableInt;
5961
useSymfony\Component\Serializer\Tests\Fixtures\DummyObjectWithEnumConstructor;
6062
useSymfony\Component\Serializer\Tests\Fixtures\DummyObjectWithEnumProperty;
6163
useSymfony\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+
publicfunctiontestDeserializeNullableIntInXml()
757+
{
758+
$extractor =newPropertyInfoExtractor([], [newReflectionExtractor()]);
759+
$serializer =newSerializer([newObjectNormalizer(null,null,null,$extractor)], ['xml' =>newXmlEncoder()]);
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
publicfunctiontestUnionTypeDeserializable()
755767
{
756768
$classMetadataFactory =newClassMetadataFactory(newAttributeLoader());

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp