Jelajahi Sumber

Fix in GraphNavigator for serialization of polymorphic interfaces

Christian Flothmann 11 tahun lalu
induk
melakukan
30c979229b

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

@@ -148,7 +148,7 @@ final class GraphNavigator
 
 
                     // If we're serializing a polymorphic type, then we'll be interested in the
                     // 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.
                     // metadata for the actual type of the object, not the base class.
-                    if (class_exists($type['name'], false)) {
+                    if (class_exists($type['name'], false) || interface_exists($type['name'], false)) {
                         if (is_subclass_of($data, $type['name'], false)) {
                         if (is_subclass_of($data, $type['name'], false)) {
                             $type = array('name' => get_class($data), 'params' => array());
                             $type = array('name' => get_class($data), 'params' => array());
                         }
                         }

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

@@ -18,6 +18,6 @@
 
 
 namespace JMS\Serializer\Tests\Fixtures\Discriminator;
 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;
 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/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;
+    }
+}

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

@@ -30,6 +30,7 @@ use JMS\Serializer\Tests\Fixtures\Garage;
 use JMS\Serializer\Tests\Fixtures\InlineChildEmpty;
 use JMS\Serializer\Tests\Fixtures\InlineChildEmpty;
 use JMS\Serializer\Tests\Fixtures\NamedDateTimeArraysObject;
 use JMS\Serializer\Tests\Fixtures\NamedDateTimeArraysObject;
 use JMS\Serializer\Tests\Fixtures\Tree;
 use JMS\Serializer\Tests\Fixtures\Tree;
+use JMS\Serializer\Tests\Fixtures\VehicleInterfaceGarage;
 use PhpCollection\Sequence;
 use PhpCollection\Sequence;
 use Symfony\Component\Form\FormFactoryBuilder;
 use Symfony\Component\Form\FormFactoryBuilder;
 use Symfony\Component\Translation\MessageSelector;
 use Symfony\Component\Translation\MessageSelector;
@@ -844,6 +845,28 @@ abstract class BaseSerializationTest extends \PHPUnit_Framework_TestCase
         }
         }
     }
     }
 
 
+    /**
+     * @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
      * @group polymorphic
      * @expectedException LogicException
      * @expectedException LogicException