Skip to content

Commit

Permalink
schmittjoh#784 fix with inline array of type array<K, V>
Browse files Browse the repository at this point in the history
  • Loading branch information
aviortm committed Jun 7, 2017
1 parent 3a41588 commit d80a173
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 2 deletions.
4 changes: 2 additions & 2 deletions src/JsonSerializationVisitor.php
Original file line number Diff line number Diff line change
Expand Up @@ -167,8 +167,8 @@ public function visitProperty(PropertyMetadata $metadata, $data, Context $contex
$k = $this->namingStrategy->translateName($metadata);

if ($metadata->inline) {
if (is_array($v)) {
$this->data = array_merge($this->data, $v);
if (is_array($v) || ($v instanceof \ArrayObject)) {
$this->data = array_merge($this->data, (array) $v);
}
} else {
$this->data[$k] = $v;
Expand Down
24 changes: 24 additions & 0 deletions tests/JMS/Serializer/Tests/Fixtures/ObjectWithInlineArray.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace JMS\Serializer\Tests\Fixtures;

use JMS\Serializer\Annotation as Serializer;

final class ObjectWithInlineArray
{
/**
* @Serializer\Inline()
* @Serializer\Type("array<string,string>")
*/
public $array;

/**
* @param array $array
*/
public function __construct(array $array)
{
$this->array = $array;
}


}
8 changes: 8 additions & 0 deletions tests/Serializer/JsonSerializationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
use JMS\Serializer\Tests\Fixtures\Author;
use JMS\Serializer\Tests\Fixtures\AuthorList;
use JMS\Serializer\Tests\Fixtures\ObjectWithEmptyArrayAndHash;
use JMS\Serializer\Tests\Fixtures\ObjectWithInlineArray;
use JMS\Serializer\Tests\Fixtures\Tag;
use JMS\Serializer\VisitorInterface;

Expand Down Expand Up @@ -280,6 +281,13 @@ public function testSerializeArrayWithEmptyObject()
$this->assertEquals('[{}]', $this->serialize(array(new \stdClass())));
}

public function testInlineArray()
{
$object = new ObjectWithInlineArray(['a' => 'b', 'c' => 'd']);

$this->assertEquals('{"a":"b","c":"d"}', $this->serialize($object));
}

public function testSerializeRootArrayWithDefinedKeys()
{
$author1 = new Author("Jim");
Expand Down

0 comments on commit d80a173

Please sign in to comment.