Skip to content

Commit

Permalink
[Serializer] Improve messages for unexpected resources values
Browse files Browse the repository at this point in the history
  • Loading branch information
fancyweb committed Oct 29, 2019
1 parent 2ecd793 commit ad2ce27
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/Symfony/Component/Serializer/Encoder/XmlEncoder.php
Expand Up @@ -426,7 +426,7 @@ private function buildXml(\DOMNode $parentNode, $data, $xmlRootNodeName = null)
return $this->appendNode($parentNode, $data, 'data');
}

throw new NotEncodableValueException(sprintf('An unexpected value could not be serialized: %s', var_export($data, true)));
throw new NotEncodableValueException(sprintf('An unexpected value could not be serialized: %s', !\is_resource($data) ? var_export($data, true) : sprintf('%s resource', get_resource_type($data))));
}

/**
Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Component/Serializer/Serializer.php
Expand Up @@ -164,7 +164,7 @@ public function normalize($data, $format = null, array $context = [])
throw new NotNormalizableValueException(sprintf('Could not normalize object of type %s, no supporting normalizer found.', \get_class($data)));
}

throw new NotNormalizableValueException(sprintf('An unexpected value could not be normalized: %s', var_export($data, true)));
throw new NotNormalizableValueException(sprintf('An unexpected value could not be normalized: %s', !\is_resource($data) ? var_export($data, true) : sprintf('%s resource', get_resource_type($data))));
}

/**
Expand Down
Expand Up @@ -14,6 +14,7 @@
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Serializer\Encoder\XmlEncoder;
use Symfony\Component\Serializer\Exception\NotEncodableValueException;
use Symfony\Component\Serializer\Normalizer\CustomNormalizer;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
use Symfony\Component\Serializer\Serializer;
Expand Down Expand Up @@ -679,6 +680,14 @@ public function testEncodeXmlWithDateTimeObjectField()
$this->assertEquals($this->createXmlWithDateTimeField(), $actualXml);
}

public function testNotEncodableValueExceptionMessageForAResource()
{
$this->expectException(NotEncodableValueException::class);
$this->expectExceptionMessage('An unexpected value could not be serialized: stream resource');

(new XmlEncoder())->encode(tmpfile(), 'xml');
}

/**
* @return XmlEncoder
*/
Expand Down
9 changes: 9 additions & 0 deletions src/Symfony/Component/Serializer/Tests/SerializerTest.php
Expand Up @@ -13,6 +13,7 @@

use PHPUnit\Framework\TestCase;
use Symfony\Component\Serializer\Encoder\JsonEncoder;
use Symfony\Component\Serializer\Exception\NotNormalizableValueException;
use Symfony\Component\Serializer\Normalizer\ArrayDenormalizer;
use Symfony\Component\Serializer\Normalizer\CustomNormalizer;
use Symfony\Component\Serializer\Normalizer\DenormalizerAwareInterface;
Expand Down Expand Up @@ -328,6 +329,14 @@ public function testDeserializeObjectConstructorWithObjectTypeHint()

$this->assertEquals(new Foo(new Bar('baz')), $serializer->deserialize($jsonData, Foo::class, 'json'));
}

public function testNotNormalizableValueExceptionMessageForAResource()
{
$this->expectException(NotNormalizableValueException::class);
$this->expectExceptionMessage('An unexpected value could not be normalized: stream resource');

(new Serializer())->normalize(tmpfile());
}
}

class Model
Expand Down

0 comments on commit ad2ce27

Please sign in to comment.