Jelajahi Sumber

Fix inline bug with empty child

Adrien Brault 11 tahun lalu
induk
melakukan
4d54aaee1c

+ 4 - 2
src/JMS/Serializer/GenericSerializationVisitor.php

@@ -144,8 +144,10 @@ abstract class GenericSerializationVisitor extends AbstractVisitor
 
         $k = $this->namingStrategy->translateName($metadata);
 
-        if ($metadata->inline && is_array($v)) {
-            $this->data = array_merge($this->data, $v);
+        if ($metadata->inline) {
+            if (is_array($v)) {
+                $this->data = array_merge($this->data, $v);
+            }
         } else {
             $this->data[$k] = $v;
         }

+ 1 - 1
src/JMS/Serializer/YamlSerializationVisitor.php

@@ -176,7 +176,7 @@ class YamlSerializationVisitor extends AbstractVisitor
                 ->rtrim(false)
                 ->writeln(' '.$v)
             ;
-        } elseif ($count === $this->writer->changeCount) {
+        } elseif ($count === $this->writer->changeCount && !$metadata->inline) {
             $this->writer->revert();
         }
 

+ 26 - 0
tests/JMS/Serializer/Tests/Fixtures/InlineChildEmpty.php

@@ -0,0 +1,26 @@
+<?php
+
+/*
+ * Copyright 2013 Johannes M. Schmitt <schmittjoh@gmail.com>
+ *
+ * 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 as Serializer;
+
+class InlineChildEmpty
+{
+
+}

+ 2 - 3
tests/JMS/Serializer/Tests/Fixtures/InlineParent.php

@@ -35,13 +35,12 @@ class InlineParent
     private $d = 'd';
 
     /**
-     * @Type("JMS\Serializer\Tests\Fixtures\InlineChild")
      * @Serializer\Inline
      */
     private $child;
 
-    public function __construct()
+    public function __construct($child = null)
     {
-        $this->child = new InlineChild();
+        $this->child = $child ?: new InlineChild();
     }
 }

+ 11 - 0
tests/JMS/Serializer/Tests/Serializer/BaseSerializationTest.php

@@ -24,6 +24,7 @@ use JMS\Serializer\GraphNavigator;
 use JMS\Serializer\Handler\PhpCollectionHandler;
 use JMS\Serializer\SerializationContext;
 use JMS\Serializer\Tests\Fixtures\Discriminator\Car;
+use JMS\Serializer\Tests\Fixtures\InlineChildEmpty;
 use JMS\Serializer\Tests\Fixtures\Tree;
 use PhpCollection\Sequence;
 use Symfony\Component\Translation\MessageSelector;
@@ -407,6 +408,16 @@ abstract class BaseSerializationTest extends \PHPUnit_Framework_TestCase
         // no deserialization support
     }
 
+    public function testInlineEmptyChild()
+    {
+        $inline = new InlineParent(new InlineChildEmpty());
+
+        $result = $this->serialize($inline);
+        $this->assertEquals($this->getContent('inline_child_empty'), $result);
+
+        // no deserialization support
+    }
+
     /**
      * @group log
      */

+ 1 - 0
tests/JMS/Serializer/Tests/Serializer/JsonSerializationTest.php

@@ -69,6 +69,7 @@ class JsonSerializationTest extends BaseSerializationTest
             $outputs['accessor_order_parent'] = '{"a":"a","b":"b"}';
             $outputs['accessor_order_methods'] = '{"foo":"c","b":"b","a":"a"}';
             $outputs['inline'] = '{"c":"c","a":"a","b":"b","d":"d"}';
+            $outputs['inline_child_empty'] = '{"c":"c","d":"d"}';
             $outputs['groups_all'] = '{"foo":"foo","foobar":"foobar","bar":"bar","none":"none"}';
             $outputs['groups_foo'] = '{"foo":"foo","foobar":"foobar"}';
             $outputs['groups_foobar'] = '{"foo":"foo","foobar":"foobar","bar":"bar"}';

+ 5 - 0
tests/JMS/Serializer/Tests/Serializer/xml/inline_child_empty.xml

@@ -0,0 +1,5 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<result>
+  <c><![CDATA[c]]></c>
+  <d><![CDATA[d]]></d>
+</result>

+ 2 - 0
tests/JMS/Serializer/Tests/Serializer/yml/inline_child_empty.yml

@@ -0,0 +1,2 @@
+c: c
+d: d