From 5fbf852d82c7340b70daaa54dc869313d386ab40 Mon Sep 17 00:00:00 2001 From: Asmir Mustafic Date: Fri, 3 May 2019 22:59:18 +0200 Subject: [PATCH] move type check when deserializing into the graph navigator --- .../DeserializationGraphNavigator.php | 4 ++++ src/JsonDeserializationVisitor.php | 16 ++-------------- src/XmlDeserializationVisitor.php | 3 --- tests/Serializer/GraphNavigatorTest.php | 6 ++++++ 4 files changed, 12 insertions(+), 17 deletions(-) diff --git a/src/GraphNavigator/DeserializationGraphNavigator.php b/src/GraphNavigator/DeserializationGraphNavigator.php index 399c79e4c..d25da8111 100644 --- a/src/GraphNavigator/DeserializationGraphNavigator.php +++ b/src/GraphNavigator/DeserializationGraphNavigator.php @@ -194,6 +194,10 @@ 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); diff --git a/src/JsonDeserializationVisitor.php b/src/JsonDeserializationVisitor.php index 4642c0873..ca91ff1e7 100644 --- a/src/JsonDeserializationVisitor.php +++ b/src/JsonDeserializationVisitor.php @@ -152,10 +152,8 @@ public function startVisitingObject(ClassMetadata $metadata, object $object, arr */ public function visitProperty(PropertyMetadata $metadata, $data) { - $name = $metadata->serializedName; - if (null === $data) { - return; + return null; } if (!\is_array($data)) { @@ -163,24 +161,14 @@ public function visitProperty(PropertyMetadata $metadata, $data) } if (true === $metadata->inline) { - if (!$metadata->type) { - throw new RuntimeException(sprintf( - 'You must define a type for %s::$%s.', - $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 new RuntimeException(sprintf('You must define a type for %s::$%s.', $metadata->class, $metadata->name)); - } - return null !== $data[$name] ? $this->navigator->accept($data[$name], $metadata->type) : null; } diff --git a/src/XmlDeserializationVisitor.php b/src/XmlDeserializationVisitor.php index 33132c4a5..d0c7d7ea8 100644 --- a/src/XmlDeserializationVisitor.php +++ b/src/XmlDeserializationVisitor.php @@ -281,9 +281,6 @@ public function visitProperty(PropertyMetadata $metadata, $data) { $name = $metadata->serializedName; - if (!$metadata->type) { - throw new RuntimeException(sprintf('You must define a type for %s::$%s.', $metadata->class, $metadata->name)); - } if (true === $metadata->inline) { return $this->navigator->accept($data, $metadata->type); } diff --git a/tests/Serializer/GraphNavigatorTest.php b/tests/Serializer/GraphNavigatorTest.php index 88ad4a73d..27286522a 100644 --- a/tests/Serializer/GraphNavigatorTest.php +++ b/tests/Serializer/GraphNavigatorTest.php @@ -6,6 +6,7 @@ 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; @@ -163,6 +164,11 @@ protected function setUp() class SerializableClass { + /** + * @Serializer\Type("string") + * + * @var string + */ public $foo = 'bar'; }