Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Revert "Move type check when deserializing into the graph navigator" #1103

Merged
merged 4 commits into from Jun 28, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 8 additions & 0 deletions src/Exception/RuntimeException.php
Expand Up @@ -11,4 +11,12 @@
*/
class RuntimeException extends \RuntimeException implements Exception
{
public static function noMetadataForProperty(string $class, string $prop): self
{
return new RuntimeException(sprintf(
'You must define a type for %s::$%s.',
$class,
$prop
));
}
}
4 changes: 0 additions & 4 deletions src/GraphNavigator/DeserializationGraphNavigator.php
Expand Up @@ -198,10 +198,6 @@ public function accept($data, ?array $type = null)
continue;
}

if (!$propertyMetadata->type) {
throw new RuntimeException(sprintf('You must define a type for %s::$%s.', $propertyMetadata->class, $propertyMetadata->name));
}

$this->context->pushPropertyMetadata($propertyMetadata);
try {
$v = $this->visitor->visitProperty($propertyMetadata, $data);
Expand Down
12 changes: 10 additions & 2 deletions src/JsonDeserializationVisitor.php
Expand Up @@ -152,23 +152,31 @@ public function startVisitingObject(ClassMetadata $metadata, object $object, arr
*/
public function visitProperty(PropertyMetadata $metadata, $data)
{
$name = $metadata->serializedName;

if (null === $data) {
return null;
return;
}

if (!\is_array($data)) {
throw new RuntimeException(sprintf('Invalid data %s (%s), expected "%s".', json_encode($data), $metadata->type['name'], $metadata->class));
}

if (true === $metadata->inline) {
if (!$metadata->type) {
throw RuntimeException::noMetadataForProperty($metadata->class, $metadata->name);
}
return $this->navigator->accept($data, $metadata->type);
}

$name = $metadata->serializedName;
if (!array_key_exists($name, $data)) {
throw new NotAcceptableException();
}

if (!$metadata->type) {
throw RuntimeException::noMetadataForProperty($metadata->class, $metadata->name);
}

return null !== $data[$name] ? $this->navigator->accept($data[$name], $metadata->type) : null;
}

Expand Down
15 changes: 15 additions & 0 deletions src/XmlDeserializationVisitor.php
Expand Up @@ -282,18 +282,27 @@ public function visitProperty(PropertyMetadata $metadata, $data)
$name = $metadata->serializedName;

if (true === $metadata->inline) {
if (!$metadata->type) {
throw RuntimeException::noMetadataForProperty($metadata->class, $metadata->name);
}
return $this->navigator->accept($data, $metadata->type);
}
if ($metadata->xmlAttribute) {
$attributes = $data->attributes($metadata->xmlNamespace);
if (isset($attributes[$name])) {
if (!$metadata->type) {
throw RuntimeException::noMetadataForProperty($metadata->class, $metadata->name);
}
return $this->navigator->accept($attributes[$name], $metadata->type);
}

throw new NotAcceptableException();
}

if ($metadata->xmlValue) {
if (!$metadata->type) {
throw RuntimeException::noMetadataForProperty($metadata->class, $metadata->name);
}
return $this->navigator->accept($data, $metadata->type);
}

Expand All @@ -304,6 +313,9 @@ public function visitProperty(PropertyMetadata $metadata, $data)
}

$this->setCurrentMetadata($metadata);
if (!$metadata->type) {
throw RuntimeException::noMetadataForProperty($metadata->class, $metadata->name);
}
$v = $this->navigator->accept($enclosingElem, $metadata->type);
$this->revertCurrentMetadata();
return $v;
Expand Down Expand Up @@ -341,6 +353,9 @@ public function visitProperty(PropertyMetadata $metadata, $data)
$this->setCurrentMetadata($metadata);
}

if (!$metadata->type) {
throw RuntimeException::noMetadataForProperty($metadata->class, $metadata->name);
}
return $this->navigator->accept($node, $metadata->type);
}

Expand Down
10 changes: 10 additions & 0 deletions tests/Fixtures/ParentNoMetadata.php
@@ -0,0 +1,10 @@
<?php

declare(strict_types=1);

namespace JMS\Serializer\Tests\Fixtures;

class ParentNoMetadata
{
public $foo;
}
17 changes: 17 additions & 0 deletions tests/Fixtures/ParentNoMetadataChildObject.php
@@ -0,0 +1,17 @@
<?php

declare(strict_types=1);

namespace JMS\Serializer\Tests\Fixtures;

use JMS\Serializer\Annotation as Serializer;

class ParentNoMetadataChildObject extends ParentNoMetadata
{
/**
* @Serializer\Type("string")
*
* @var string
*/
public $bar;
}
10 changes: 10 additions & 0 deletions tests/Serializer/BaseSerializationTest.php
Expand Up @@ -86,6 +86,7 @@
use JMS\Serializer\Tests\Fixtures\ObjectWithVirtualProperties;
use JMS\Serializer\Tests\Fixtures\Order;
use JMS\Serializer\Tests\Fixtures\ParentDoNotSkipWithEmptyChild;
use JMS\Serializer\Tests\Fixtures\ParentNoMetadataChildObject;
use JMS\Serializer\Tests\Fixtures\ParentSkipWithEmptyChild;
use JMS\Serializer\Tests\Fixtures\PersonSecret;
use JMS\Serializer\Tests\Fixtures\PersonSecretMore;
Expand Down Expand Up @@ -148,6 +149,15 @@ public function testSerializeNullArray()
);
}

public function testNoMetadataNeededWhenDeSerializingNotUsedProperty()
{
/** @var ParentNoMetadataChildObject $dObj */
$object = $this->deserialize($this->getContent('ParentNoMetadataChildObject'), ParentNoMetadataChildObject::class);

self::assertSame('John', $object->bar);
self::assertNull($object->foo);
}

public function testDeserializeObjectWithMissingTypedArrayProp()
{
/** @var ObjectWithTypedArraySetter $dObj */
Expand Down
6 changes: 0 additions & 6 deletions tests/Serializer/GraphNavigatorTest.php
Expand Up @@ -6,7 +6,6 @@

use Doctrine\Common\Annotations\AnnotationReader;
use JMS\Serializer\Accessor\DefaultAccessorStrategy;
use JMS\Serializer\Annotation as Serializer;
use JMS\Serializer\Construction\UnserializeObjectConstructor;
use JMS\Serializer\DeserializationContext;
use JMS\Serializer\EventDispatcher\EventDispatcher;
Expand Down Expand Up @@ -164,11 +163,6 @@ protected function setUp()

class SerializableClass
{
/**
* @Serializer\Type("string")
*
* @var string
*/
public $foo = 'bar';
}

Expand Down
1 change: 1 addition & 0 deletions tests/Serializer/JsonSerializationTest.php
Expand Up @@ -123,6 +123,7 @@ protected function getContent($key)
$outputs['iterator'] = '{"iterator":{"foo":"bar","bar":"foo"}}';
$outputs['array_iterator'] = '{"iterator":{"foo":"bar","bar":"foo"}}';
$outputs['generator'] = '{"generator":{"foo":"bar","bar":"foo"}}';
$outputs['ParentNoMetadataChildObject'] = '{"bar":"John"}';
}

if (!isset($outputs[$key])) {
Expand Down
4 changes: 4 additions & 0 deletions tests/Serializer/xml/ParentNoMetadataChildObject.xml
@@ -0,0 +1,4 @@
<?xml version="1.0" encoding="UTF-8"?>
<result>
<bar><![CDATA[John]]></bar>
</result>