Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit e26d257

Browse files
committed
[Serializer] Improve messages for unexpected resources values
1 parent fb70e0a commit e26d257

File tree

4 files changed

+20
-2
lines changed

4 files changed

+20
-2
lines changed

src/Symfony/Component/Serializer/Encoder/XmlEncoder.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -426,7 +426,7 @@ private function buildXml(\DOMNode $parentNode, $data, $xmlRootNodeName = null)
426426
return $this->appendNode($parentNode, $data, 'data');
427427
}
428428

429-
throw new NotEncodableValueException(sprintf('An unexpected value could not be serialized: %s', var_export($data, true)));
429+
throw new NotEncodableValueException(sprintf('An unexpected value could not be serialized: %s', !is_resource($data) ? var_export($data, true) : sprintf('a "%s" resource', get_resource_type($data))));
430430
}
431431

432432
/**

src/Symfony/Component/Serializer/Serializer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ public function normalize($data, $format = null, array $context = [])
164164
throw new NotNormalizableValueException(sprintf('Could not normalize object of type %s, no supporting normalizer found.', \get_class($data)));
165165
}
166166

167-
throw new NotNormalizableValueException(sprintf('An unexpected value could not be normalized: %s', var_export($data, true)));
167+
throw new NotNormalizableValueException(sprintf('An unexpected value could not be normalized: %s', !is_resource($data) ? var_export($data, true) : sprintf('a "%s" resource', get_resource_type($data))));
168168
}
169169

170170
/**

src/Symfony/Component/Serializer/Tests/Encoder/XmlEncoderTest.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use PHPUnit\Framework\MockObject\MockObject;
1515
use PHPUnit\Framework\TestCase;
1616
use Symfony\Component\Serializer\Encoder\XmlEncoder;
17+
use Symfony\Component\Serializer\Exception\NotEncodableValueException;
1718
use Symfony\Component\Serializer\Normalizer\CustomNormalizer;
1819
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
1920
use Symfony\Component\Serializer\Serializer;
@@ -679,6 +680,14 @@ public function testEncodeXmlWithDateTimeObjectField()
679680
$this->assertEquals($this->createXmlWithDateTimeField(), $actualXml);
680681
}
681682

683+
public function testNotEncodableValueExceptionMessageForAResource()
684+
{
685+
$this->expectException(NotEncodableValueException::class);
686+
$this->expectExceptionMessage('An unexpected value could not be serialized: a "stream" resource');
687+
688+
(new XmlEncoder())->encode(tmpfile(), 'xml');
689+
}
690+
682691
/**
683692
* @return XmlEncoder
684693
*/

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use PHPUnit\Framework\TestCase;
1515
use Symfony\Component\Serializer\Encoder\JsonEncoder;
16+
use Symfony\Component\Serializer\Exception\NotNormalizableValueException;
1617
use Symfony\Component\Serializer\Normalizer\ArrayDenormalizer;
1718
use Symfony\Component\Serializer\Normalizer\CustomNormalizer;
1819
use Symfony\Component\Serializer\Normalizer\DenormalizerAwareInterface;
@@ -328,6 +329,14 @@ public function testDeserializeObjectConstructorWithObjectTypeHint()
328329

329330
$this->assertEquals(new Foo(new Bar('baz')), $serializer->deserialize($jsonData, Foo::class, 'json'));
330331
}
332+
333+
public function testNotNormalizableValueExceptionMessageForAResource()
334+
{
335+
$this->expectException(NotNormalizableValueException::class);
336+
$this->expectExceptionMessage('An unexpected value could not be normalized: a "stream" resource');
337+
338+
(new Serializer())->normalize(tmpfile());
339+
}
331340
}
332341

333342
class Model

0 commit comments

Comments
 (0)