Skip to content

Commit

Permalink
move type check when deserializing into the graph navigator
Browse files Browse the repository at this point in the history
  • Loading branch information
goetas committed May 3, 2019
1 parent 5dd1b1e commit f597012
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 10 deletions.
4 changes: 4 additions & 0 deletions src/GraphNavigator/DeserializationGraphNavigator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
9 changes: 2 additions & 7 deletions src/JsonDeserializationVisitor.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand All @@ -173,14 +171,11 @@ public function visitProperty(PropertyMetadata $metadata, $data)
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;
}

Expand Down
3 changes: 0 additions & 3 deletions src/XmlDeserializationVisitor.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
6 changes: 6 additions & 0 deletions tests/Serializer/GraphNavigatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -163,6 +164,11 @@ protected function setUp()

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

Expand Down

0 comments on commit f597012

Please sign in to comment.