Browse Source

Merge pull request #1 from schmittjoh/master

rebase
Yury Kozyrev 10 năm trước cách đây
mục cha
commit
6a89385e56

+ 8 - 0
src/JMS/Serializer/GraphNavigator.php

@@ -145,6 +145,14 @@ final class GraphNavigator
                         }
                         $context->startVisiting($data);
                     }
+
+                    // If we're serializing a polymorphic type, then we'll be interested in the
+                    // metadata for the actual type of the object, not the base class.
+                    if (class_exists($type['name'], false) || interface_exists($type['name'], false)) {
+                        if (is_subclass_of($data, $type['name'], false)) {
+                            $type = array('name' => get_class($data), 'params' => array());
+                        }
+                    }
                 } elseif ($context instanceof DeserializationContext) {
                     $context->increaseDepth();
                 }

+ 1 - 1
tests/JMS/Serializer/Tests/Fixtures/Discriminator/Car.php

@@ -18,6 +18,6 @@
 
 namespace JMS\Serializer\Tests\Fixtures\Discriminator;
 
-class Car extends Vehicle
+class Car extends Vehicle implements VehicleInterface
 {
 }

+ 1 - 1
tests/JMS/Serializer/Tests/Fixtures/Discriminator/Moped.php

@@ -18,6 +18,6 @@
 
 namespace JMS\Serializer\Tests\Fixtures\Discriminator;
 
-class Moped extends Vehicle
+class Moped extends Vehicle implements VehicleInterface
 {
 }

+ 31 - 0
tests/JMS/Serializer/Tests/Fixtures/Discriminator/VehicleInterface.php

@@ -0,0 +1,31 @@
+<?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\Discriminator;
+
+use JMS\Serializer\Annotation as Serializer;
+
+/**
+ * @Serializer\Discriminator(field = "type", map = {
+ *    "car": "JMS\Serializer\Tests\Fixtures\Discriminator\Car",
+ *    "moped": "JMS\Serializer\Tests\Fixtures\Discriminator\Moped",
+ * })
+ */
+interface VehicleInterface
+{
+}

+ 33 - 0
tests/JMS/Serializer/Tests/Fixtures/Garage.php

@@ -0,0 +1,33 @@
+<?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\Type;
+
+class Garage
+{
+    /**
+     * @Type("array<JMS\Serializer\Tests\Fixtures\Discriminator\Vehicle>")
+     */
+    public $vehicles;
+
+    public function __construct($vehicles) {
+        $this->vehicles = $vehicles;
+    }
+}

+ 33 - 0
tests/JMS/Serializer/Tests/Fixtures/VehicleInterfaceGarage.php

@@ -0,0 +1,33 @@
+<?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\Type;
+
+class VehicleInterfaceGarage
+{
+    /**
+     * @Type("array<JMS\Serializer\Tests\Fixtures\Discriminator\VehicleInterface>")
+     */
+    public $vehicles;
+
+    public function __construct($vehicles) {
+        $this->vehicles = $vehicles;
+    }
+}

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

@@ -25,9 +25,12 @@ use JMS\Serializer\Handler\PhpCollectionHandler;
 use JMS\Serializer\SerializationContext;
 use JMS\Serializer\Tests\Fixtures\DateTimeArraysObject;
 use JMS\Serializer\Tests\Fixtures\Discriminator\Car;
+use JMS\Serializer\Tests\Fixtures\Discriminator\Moped;
+use JMS\Serializer\Tests\Fixtures\Garage;
 use JMS\Serializer\Tests\Fixtures\InlineChildEmpty;
 use JMS\Serializer\Tests\Fixtures\NamedDateTimeArraysObject;
 use JMS\Serializer\Tests\Fixtures\Tree;
+use JMS\Serializer\Tests\Fixtures\VehicleInterfaceGarage;
 use PhpCollection\Sequence;
 use Symfony\Component\Form\FormFactoryBuilder;
 use Symfony\Component\Translation\MessageSelector;
@@ -820,6 +823,50 @@ abstract class BaseSerializationTest extends \PHPUnit_Framework_TestCase
         }
     }
 
+    /**
+     * @group polymorphic
+     */
+    public function testNestedPolymorphicObjects()
+    {
+        $garage = new Garage(array(new Car(3), new Moped(1)));
+        $this->assertEquals(
+            $this->getContent('garage'),
+            $this->serialize($garage)
+        );
+
+        if ($this->hasDeserializer()) {
+            $this->assertEquals(
+                $garage,
+                $this->deserialize(
+                    $this->getContent('garage'),
+                    'JMS\Serializer\Tests\Fixtures\Garage'
+                )
+            );
+        }
+    }
+
+    /**
+     * @group polymorphic
+     */
+    public function testNestedPolymorphicInterfaces()
+    {
+        $garage = new VehicleInterfaceGarage(array(new Car(3), new Moped(1)));
+        $this->assertEquals(
+            $this->getContent('garage'),
+            $this->serialize($garage)
+        );
+
+        if ($this->hasDeserializer()) {
+            $this->assertEquals(
+                $garage,
+                $this->deserialize(
+                    $this->getContent('garage'),
+                    'JMS\Serializer\Tests\Fixtures\VehicleInterfaceGarage'
+                )
+            );
+        }
+    }
+
     /**
      * @group polymorphic
      * @expectedException LogicException

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

@@ -91,6 +91,7 @@ class JsonSerializationTest extends BaseSerializationTest
             $outputs['date_interval'] = '"PT45M"';
             $outputs['car'] = '{"km":5,"type":"car"}';
             $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"}}';
         }
 

+ 13 - 0
tests/JMS/Serializer/Tests/Serializer/xml/garage.xml

@@ -0,0 +1,13 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<result>
+  <vehicles>
+    <entry>
+      <km>3</km>
+      <type><![CDATA[car]]></type>
+    </entry>
+    <entry>
+      <km>1</km>
+      <type><![CDATA[moped]]></type>
+    </entry>
+  </vehicles>
+</result>

+ 7 - 0
tests/JMS/Serializer/Tests/Serializer/yml/garage.yml

@@ -0,0 +1,7 @@
+vehicles:
+    -
+        km: 3
+        type: car
+    -
+        km: 1
+        type: moped