From 3102b2ed30df617e3eb1867467e619cd834ece7e Mon Sep 17 00:00:00 2001 From: scasei Date: Mon, 12 Sep 2016 20:37:35 +0200 Subject: [PATCH 1/5] fix discriminator for classes in the middle of hierarchy --- src/JMS/Serializer/Metadata/ClassMetadata.php | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/JMS/Serializer/Metadata/ClassMetadata.php b/src/JMS/Serializer/Metadata/ClassMetadata.php index 3d76f2563..ef03da9c8 100644 --- a/src/JMS/Serializer/Metadata/ClassMetadata.php +++ b/src/JMS/Serializer/Metadata/ClassMetadata.php @@ -167,6 +167,12 @@ public function merge(MergeableInterface $object) $this->discriminatorDisabled = $object->discriminatorDisabled; } + if ($object->discriminatorMap) { + $this->discriminatorFieldName = $object->discriminatorFieldName; + $this->discriminatorMap = $object->discriminatorMap; + $this->discriminatorBaseClass = $object->discriminatorBaseClass; + } + if ($this->discriminatorMap && ! $this->reflection->isAbstract()) { if (false === $typeValue = array_search($this->name, $this->discriminatorMap, true)) { throw new \LogicException(sprintf( From 54f546837fe2639a8a67ef3a01888a0becb0f268 Mon Sep 17 00:00:00 2001 From: dan Date: Tue, 13 Sep 2016 14:36:00 +0200 Subject: [PATCH 2/5] add discriminator in middle of hierarchy --- .../Fixtures/DiscriminatorMiddle/Base.php | 25 ++++++++++++ .../Fixtures/DiscriminatorMiddle/Chief.php | 23 +++++++++++ .../Fixtures/DiscriminatorMiddle/Employee.php | 23 +++++++++++ .../Fixtures/DiscriminatorMiddle/User.php | 38 +++++++++++++++++++ .../Serializer/BaseSerializationTest.php | 24 ++++++++++++ .../Serializer/JsonSerializationTest.php | 1 + .../Tests/Serializer/xml/user_hierarchy.xml | 11 ++++++ .../Tests/Serializer/yml/user_hierarchy.yml | 6 +++ 8 files changed, 151 insertions(+) create mode 100644 tests/JMS/Serializer/Tests/Fixtures/DiscriminatorMiddle/Base.php create mode 100644 tests/JMS/Serializer/Tests/Fixtures/DiscriminatorMiddle/Chief.php create mode 100644 tests/JMS/Serializer/Tests/Fixtures/DiscriminatorMiddle/Employee.php create mode 100644 tests/JMS/Serializer/Tests/Fixtures/DiscriminatorMiddle/User.php create mode 100644 tests/JMS/Serializer/Tests/Serializer/xml/user_hierarchy.xml create mode 100644 tests/JMS/Serializer/Tests/Serializer/yml/user_hierarchy.yml diff --git a/tests/JMS/Serializer/Tests/Fixtures/DiscriminatorMiddle/Base.php b/tests/JMS/Serializer/Tests/Fixtures/DiscriminatorMiddle/Base.php new file mode 100644 index 000000000..f46f75d45 --- /dev/null +++ b/tests/JMS/Serializer/Tests/Fixtures/DiscriminatorMiddle/Base.php @@ -0,0 +1,25 @@ + + * + * 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\DiscriminatorMiddle; + + +abstract class Base +{ + +} \ No newline at end of file diff --git a/tests/JMS/Serializer/Tests/Fixtures/DiscriminatorMiddle/Chief.php b/tests/JMS/Serializer/Tests/Fixtures/DiscriminatorMiddle/Chief.php new file mode 100644 index 000000000..9be1458b4 --- /dev/null +++ b/tests/JMS/Serializer/Tests/Fixtures/DiscriminatorMiddle/Chief.php @@ -0,0 +1,23 @@ + + * + * 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\DiscriminatorMiddle; + +class Chief extends User +{ +} diff --git a/tests/JMS/Serializer/Tests/Fixtures/DiscriminatorMiddle/Employee.php b/tests/JMS/Serializer/Tests/Fixtures/DiscriminatorMiddle/Employee.php new file mode 100644 index 000000000..153a62f7b --- /dev/null +++ b/tests/JMS/Serializer/Tests/Fixtures/DiscriminatorMiddle/Employee.php @@ -0,0 +1,23 @@ + + * + * 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\DiscriminatorMiddle; + +class Employee extends User +{ +} diff --git a/tests/JMS/Serializer/Tests/Fixtures/DiscriminatorMiddle/User.php b/tests/JMS/Serializer/Tests/Fixtures/DiscriminatorMiddle/User.php new file mode 100644 index 000000000..4517fe2b0 --- /dev/null +++ b/tests/JMS/Serializer/Tests/Fixtures/DiscriminatorMiddle/User.php @@ -0,0 +1,38 @@ + + * + * 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\DiscriminatorMiddle; + +use JMS\Serializer\Annotation as Serializer; + +/** + * @Serializer\Discriminator(field = "type", map = { + * "chief": "JMS\Serializer\Tests\Fixtures\DiscriminatorMiddle\Chief", + * "employee": "JMS\Serializer\Tests\Fixtures\DiscriminatorMiddle\Employee", + * }) + */ +abstract class User extends Base +{ + /** @Serializer\Type("integer") */ + public $hours; + + public function __construct($hours) + { + $this->hours = (integer) $hours; + } +} diff --git a/tests/JMS/Serializer/Tests/Serializer/BaseSerializationTest.php b/tests/JMS/Serializer/Tests/Serializer/BaseSerializationTest.php index 2ff38c9d7..ce571337e 100644 --- a/tests/JMS/Serializer/Tests/Serializer/BaseSerializationTest.php +++ b/tests/JMS/Serializer/Tests/Serializer/BaseSerializationTest.php @@ -102,6 +102,8 @@ use JMS\Serializer\Exclusion\DepthExclusionStrategy; use JMS\Serializer\Tests\Fixtures\Node; use JMS\Serializer\Tests\Fixtures\AuthorReadOnlyPerClass; +use JMS\Serializer\Tests\Fixtures\DiscriminatorMiddle\Chief; +use JMS\Serializer\Tests\Fixtures\DiscriminatorMiddle\Employee; abstract class BaseSerializationTest extends \PHPUnit_Framework_TestCase { @@ -951,6 +953,28 @@ public function testNestedPolymorphicObjects() } } + /** + * @group polymorphic + */ + public function testNestedMiddlePolymorphicObjects() + { + $garage = array(new Chief(3), new Employee(1)); + $this->assertEquals( + $this->getContent('user_hierarchy'), + $this->serialize($garage) + ); + + if ($this->hasDeserializer()) { + $this->assertEquals( + $garage, + $this->deserialize( + $this->getContent('user_hierarchy'), + 'array' + ) + ); + } + } + /** * @group polymorphic */ diff --git a/tests/JMS/Serializer/Tests/Serializer/JsonSerializationTest.php b/tests/JMS/Serializer/Tests/Serializer/JsonSerializationTest.php index 401ff101b..0115c4790 100644 --- a/tests/JMS/Serializer/Tests/Serializer/JsonSerializationTest.php +++ b/tests/JMS/Serializer/Tests/Serializer/JsonSerializationTest.php @@ -97,6 +97,7 @@ protected function getContent($key) $outputs['car'] = '{"km":5,"type":"car"}'; $outputs['car_without_type'] = '{"km":5}'; $outputs['garage'] = '{"vehicles":[{"km":3,"type":"car"},{"km":1,"type":"moped"}]}'; + $outputs['user_hierarchy'] = '[{"hours":3,"type":"chief"},{"hours":1,"type":"employee"}]'; $outputs['tree'] = '{"tree":{"children":[{"children":[{"children":[],"foo":"bar"}],"foo":"bar"}],"foo":"bar"}}'; $outputs['nullable_arrays'] = '{"empty_inline":[],"not_empty_inline":["not_empty_inline"],"empty_not_inline":[],"not_empty_not_inline":["not_empty_not_inline"],"empty_not_inline_skip":[],"not_empty_not_inline_skip":["not_empty_not_inline_skip"]}'; $outputs['object_with_object_property_no_array_to_author'] = '{"foo": "bar", "author": "baz"}'; diff --git a/tests/JMS/Serializer/Tests/Serializer/xml/user_hierarchy.xml b/tests/JMS/Serializer/Tests/Serializer/xml/user_hierarchy.xml new file mode 100644 index 000000000..c3f337846 --- /dev/null +++ b/tests/JMS/Serializer/Tests/Serializer/xml/user_hierarchy.xml @@ -0,0 +1,11 @@ + + + + 3 + + + + 1 + + + diff --git a/tests/JMS/Serializer/Tests/Serializer/yml/user_hierarchy.yml b/tests/JMS/Serializer/Tests/Serializer/yml/user_hierarchy.yml new file mode 100644 index 000000000..864ccfece --- /dev/null +++ b/tests/JMS/Serializer/Tests/Serializer/yml/user_hierarchy.yml @@ -0,0 +1,6 @@ +- + hours: 3 + type: chief +- + hours: 1 + type: employee From 91caff52e7f30096373519a01c0d1bd7fbeba578 Mon Sep 17 00:00:00 2001 From: dan Date: Fri, 25 Jul 2014 09:56:38 +0200 Subject: [PATCH 3/5] add / discriminator for classes in the middle of hierarchy --- src/JMS/Serializer/Metadata/ClassMetadata.php | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/JMS/Serializer/Metadata/ClassMetadata.php b/src/JMS/Serializer/Metadata/ClassMetadata.php index ef03da9c8..9438ac639 100644 --- a/src/JMS/Serializer/Metadata/ClassMetadata.php +++ b/src/JMS/Serializer/Metadata/ClassMetadata.php @@ -173,6 +173,13 @@ public function merge(MergeableInterface $object) $this->discriminatorBaseClass = $object->discriminatorBaseClass; } + if ($object->discriminatorMap) { + + $this->discriminatorFieldName = $object->discriminatorFieldName; + $this->discriminatorMap = $object->discriminatorMap; + $this->discriminatorBaseClass = $object->discriminatorBaseClass; + } + if ($this->discriminatorMap && ! $this->reflection->isAbstract()) { if (false === $typeValue = array_search($this->name, $this->discriminatorMap, true)) { throw new \LogicException(sprintf( From bfd80a98b5c2ab497301275e96351ded45567c88 Mon Sep 17 00:00:00 2001 From: dan Date: Tue, 13 Sep 2016 14:38:28 +0200 Subject: [PATCH 4/5] fix double insert --- src/JMS/Serializer/Metadata/ClassMetadata.php | 7 ------- 1 file changed, 7 deletions(-) diff --git a/src/JMS/Serializer/Metadata/ClassMetadata.php b/src/JMS/Serializer/Metadata/ClassMetadata.php index 9438ac639..dea9b7a06 100644 --- a/src/JMS/Serializer/Metadata/ClassMetadata.php +++ b/src/JMS/Serializer/Metadata/ClassMetadata.php @@ -172,13 +172,6 @@ public function merge(MergeableInterface $object) $this->discriminatorMap = $object->discriminatorMap; $this->discriminatorBaseClass = $object->discriminatorBaseClass; } - - if ($object->discriminatorMap) { - - $this->discriminatorFieldName = $object->discriminatorFieldName; - $this->discriminatorMap = $object->discriminatorMap; - $this->discriminatorBaseClass = $object->discriminatorBaseClass; - } if ($this->discriminatorMap && ! $this->reflection->isAbstract()) { if (false === $typeValue = array_search($this->name, $this->discriminatorMap, true)) { From 3155f7d249c2c1c19b677cc33fb547e1502fa180 Mon Sep 17 00:00:00 2001 From: dan Date: Wed, 14 Sep 2016 07:48:17 +0200 Subject: [PATCH 5/5] fix indentation --- src/JMS/Serializer/Metadata/ClassMetadata.php | 6 +-- .../Fixtures/DiscriminatorMiddle/Base.php | 48 +++++++++---------- .../Serializer/BaseSerializationTest.php | 28 +++++------ 3 files changed, 41 insertions(+), 41 deletions(-) diff --git a/src/JMS/Serializer/Metadata/ClassMetadata.php b/src/JMS/Serializer/Metadata/ClassMetadata.php index dea9b7a06..36a101dea 100644 --- a/src/JMS/Serializer/Metadata/ClassMetadata.php +++ b/src/JMS/Serializer/Metadata/ClassMetadata.php @@ -168,9 +168,9 @@ public function merge(MergeableInterface $object) } if ($object->discriminatorMap) { - $this->discriminatorFieldName = $object->discriminatorFieldName; - $this->discriminatorMap = $object->discriminatorMap; - $this->discriminatorBaseClass = $object->discriminatorBaseClass; + $this->discriminatorFieldName = $object->discriminatorFieldName; + $this->discriminatorMap = $object->discriminatorMap; + $this->discriminatorBaseClass = $object->discriminatorBaseClass; } if ($this->discriminatorMap && ! $this->reflection->isAbstract()) { diff --git a/tests/JMS/Serializer/Tests/Fixtures/DiscriminatorMiddle/Base.php b/tests/JMS/Serializer/Tests/Fixtures/DiscriminatorMiddle/Base.php index f46f75d45..8091c36ae 100644 --- a/tests/JMS/Serializer/Tests/Fixtures/DiscriminatorMiddle/Base.php +++ b/tests/JMS/Serializer/Tests/Fixtures/DiscriminatorMiddle/Base.php @@ -1,25 +1,25 @@ - - * - * 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\DiscriminatorMiddle; - - -abstract class Base -{ - + + * + * 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\DiscriminatorMiddle; + + +abstract class Base +{ + } \ No newline at end of file diff --git a/tests/JMS/Serializer/Tests/Serializer/BaseSerializationTest.php b/tests/JMS/Serializer/Tests/Serializer/BaseSerializationTest.php index ce571337e..18b598ab6 100644 --- a/tests/JMS/Serializer/Tests/Serializer/BaseSerializationTest.php +++ b/tests/JMS/Serializer/Tests/Serializer/BaseSerializationTest.php @@ -958,21 +958,21 @@ public function testNestedPolymorphicObjects() */ public function testNestedMiddlePolymorphicObjects() { - $garage = array(new Chief(3), new Employee(1)); - $this->assertEquals( - $this->getContent('user_hierarchy'), - $this->serialize($garage) - ); + $garage = array(new Chief(3), new Employee(1)); + $this->assertEquals( + $this->getContent('user_hierarchy'), + $this->serialize($garage) + ); - if ($this->hasDeserializer()) { - $this->assertEquals( - $garage, - $this->deserialize( - $this->getContent('user_hierarchy'), - 'array' - ) - ); - } + if ($this->hasDeserializer()) { + $this->assertEquals( + $garage, + $this->deserialize( + $this->getContent('user_hierarchy'), + 'array' + ) + ); + } } /**