Skip to content

Commit

Permalink
Traverse non-type-configured arrays
Browse files Browse the repository at this point in the history
* Fix #641
  • Loading branch information
scaytrase committed Sep 8, 2016
1 parent bab8c48 commit 6db2a00
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
5 changes: 3 additions & 2 deletions src/JMS/Serializer/GenericSerializationVisitor.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,12 +98,13 @@ public function visitArray($data, array $type, Context $context)
$rs = array();
}

$isList = isset($type['params'][0]) && ! isset($type['params'][1]);
$isList = (isset($type['params'][0]) && ! isset($type['params'][1]))
|| array_keys($data) === range(0, count($data) - 1);

foreach ($data as $k => $v) {
$v = $this->navigator->accept($v, $this->getElementType($type), $context);

if (null === $v && ( ! is_string($k) || $context->shouldSerializeNull() !== true)) {
if (null === $v && !$context->shouldSerializeNull()) {
continue;
}

Expand Down
11 changes: 10 additions & 1 deletion tests/JMS/Serializer/Tests/Serializer/ArrayTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
namespace JMS\Serializer\Tests\Serializer;

use JMS\Serializer\Handler\HandlerRegistry;
use JMS\Serializer\SerializationContext;
use JMS\Serializer\Tests\Fixtures\Author;
use JMS\Serializer\Tests\Fixtures\AuthorList;
use JMS\Serializer\Tests\Fixtures\Order;
Expand Down Expand Up @@ -120,4 +121,12 @@ public function testToArrayConversNestedArrayObjects()
$result = $this->serializer->toArray($list);
$this->assertSame(array('authors' => array(array())), $result);
}
}

public function testNullArraySerialization()
{
$context = SerializationContext::create();
$context->setSerializeNull(true);
$array = [0, null, 'a'];
$this->assertSame($array, $this->serializer->toArray($array, $context));
}
}

0 comments on commit 6db2a00

Please sign in to comment.