Skip to content

Commit

Permalink
bug #34144 [Serializer] Improve messages for unexpected resources val…
Browse files Browse the repository at this point in the history
…ues (fancyweb)

This PR was merged into the 3.4 branch.

Discussion
----------

[Serializer] Improve messages for unexpected resources values

| Q             | A
| ------------- | ---
| Branch?       | 3.4
| Bug fix?      | no
| New feature?  | no
| Deprecations? | no
| Tickets       | -
| License       | MIT
| Doc PR        | -

Before: `An unexpected value could not be serialized: NULL` - very misleading

After: `An unexpected value could not be serialized: a "stream" resource`

Commits
-------

ad2ce27 [Serializer] Improve messages for unexpected resources values
  • Loading branch information
nicolas-grekas committed Oct 30, 2019
2 parents 2ecd793 + ad2ce27 commit 1a92f44
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 1a92f44

Please sign in to comment.