Selaa lähdekoodia

Fix not written empty arrays on Yaml

Sullivan SENECHAL 9 vuotta sitten
vanhempi
commit
a6443ad8f4

+ 5 - 0
src/JMS/Serializer/YamlSerializationVisitor.php

@@ -111,6 +111,11 @@ class YamlSerializationVisitor extends AbstractVisitor
                 ->rtrim(false)
                 ->writeln(' {}')
             ;
+        } elseif (empty($data)) {
+            $this->writer
+                ->rtrim(false)
+                ->writeln(' []')
+            ;
         }
     }
 

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

@@ -240,6 +240,20 @@ abstract class BaseSerializationTest extends \PHPUnit_Framework_TestCase
         }
     }
 
+    public function testArrayEmpty()
+    {
+        if ('xml' === $this->getFormat()) {
+            $this->markTestSkipped('XML can\'t be tested for empty array');
+        }
+
+        $data = array('array' => []);
+        $this->assertEquals($this->getContent('array_empty'), $this->serialize($data));
+
+        if ($this->hasDeserializer()) {
+            $this->assertEquals($data, $this->deserialize($this->getContent('array_empty')), 'array');
+        }
+    }
+
     public function testArrayFloats()
     {
         $data = array(1.34, 3.0, 6.42);

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

@@ -46,6 +46,7 @@ class JsonSerializationTest extends BaseSerializationTest
             $outputs['array_strings'] = '["foo","bar"]';
             $outputs['array_booleans'] = '[true,false]';
             $outputs['array_integers'] = '[1,3,4]';
+            $outputs['array_empty'] = '{"array":[]}';
             $outputs['array_floats'] = '[1.34,3,6.42]';
             $outputs['array_objects'] = '[{"foo":"foo","moo":"bar","camel_case":"boo"},{"foo":"baz","moo":"boo","camel_case":"boo"}]';
             $outputs['array_mixed'] = '["foo",1,true,{"foo":"foo","moo":"bar","camel_case":"boo"},[1,3,true]]';

+ 1 - 0
tests/JMS/Serializer/Tests/Serializer/yml/array_empty.yml

@@ -0,0 +1 @@
+array: []

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

@@ -3,6 +3,8 @@ title: 'This is a nice title.'
 created_at: '2011-07-30T00:00:00+0000'
 is_published: false
 etag: 1edf9bf60a32d89afbb85b2be849e3ceed5f5b10
+comments: []
+comments2: []
 metadata:
     foo: bar
 author: null