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

Fix warning array_key_exists in deserialization. #398

Merged
merged 3 commits into from
Aug 12, 2016
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion src/JMS/Serializer/GenericDeserializationVisitor.php
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,15 @@ public function visitProperty(PropertyMetadata $metadata, $data, Context $contex
{
$name = $this->namingStrategy->translateName($metadata);

if (null === $data || ! array_key_exists($name, $data)) {
if (null === $data) {
return;
}

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

if ( ! array_key_exists($name, $data)) {
return;
}

Expand Down
53 changes: 53 additions & 0 deletions tests/JMS/Serializer/Tests/Fixtures/ObjectWithObjectProperty.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

/*
* Copyright 2013 Johannes M. Schmitt <[email protected]>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

namespace JMS\Serializer\Tests\Fixtures;

use JMS\Serializer\Annotation\Type;


class ObjectWithObjectProperty
{
/**
* @Type("string")
*/
private $foo;

/**
* @Type("JMS\Serializer\Tests\Fixtures\Author")
*/
private $author;

/**
* @return string
*/
public function getFoo()
{
return $this->foo;
}

/**
* @return \JMS\Serializer\Tests\Fixtures\Author
*/
public function getAuthor()
{
return $this->author;
}


}
24 changes: 24 additions & 0 deletions tests/JMS/Serializer/Tests/Serializer/JsonSerializationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,8 @@ protected function getContent($key)
$outputs['car_without_type'] = '{"km":5}';
$outputs['garage'] = '{"vehicles":[{"km":3,"type":"car"},{"km":1,"type":"moped"}]}';
$outputs['tree'] = '{"tree":{"children":[{"children":[{"children":[],"foo":"bar"}],"foo":"bar"}],"foo":"bar"}}';
$outputs['object_with_object_property_no_array_to_author'] = '{"foo": "bar", "author": "baz"}';
$outputs['object_with_object_property'] = '{"foo": "bar", "author": {"full_name": "baz"}}';
}

if (!isset($outputs[$key])) {
Expand All @@ -118,6 +120,28 @@ function(VisitorInterface $visitor, AuthorList $data, array $type, Context $cont
$this->assertEquals('[{"full_name":"foo","_links":{"details":"http:\/\/foo.bar\/details\/foo","comments":"http:\/\/foo.bar\/details\/foo\/comments"}},{"full_name":"bar","_links":{"details":"http:\/\/foo.bar\/details\/bar","comments":"http:\/\/foo.bar\/details\/bar\/comments"}}]', $this->serialize($list));
}


/**
* @expectedException RuntimeException
* @expectedExceptionMessage Invalid data "baz"(string), expected "JMS\Serializer\Tests\Fixtures\Author".
*/
public function testDeserializingObjectWithObjectPropertyWithNoArrayToObject()
{
$content = $this->getContent('object_with_object_property_no_array_to_author');
$object = $this->deserialize($content, 'JMS\Serializer\Tests\Fixtures\ObjectWithObjectProperty');
$this->assertEquals('bar', $object->getFoo());
$this->assertInstanceOf('JMS\Serializer\Tests\Fixtures\Author', $object->getAuthor());
}

public function testDeserializingObjectWithObjectProperty()
{
$content = $this->getContent('object_with_object_property');
$object = $this->deserialize($content, 'JMS\Serializer\Tests\Fixtures\ObjectWithObjectProperty');
$this->assertEquals('bar', $object->getFoo());
$this->assertInstanceOf('JMS\Serializer\Tests\Fixtures\Author', $object->getAuthor());
$this->assertEquals('baz', $object->getAuthor()->getName());
}

public function getPrimitiveTypes()
{
return array(
Expand Down