From edba2d19f3e83c9e29457b0cf3d943b3a7b6691f Mon Sep 17 00:00:00 2001 From: Arnaud Tarroux Date: Thu, 6 Feb 2020 10:11:48 +0100 Subject: [PATCH 01/11] feat: Add serializer versioning --- .../Component/Serializer/Annotation/Since.php | 48 ++++++++++++++++ .../Component/Serializer/Annotation/Until.php | 48 ++++++++++++++++ .../Serializer/Mapping/AttributeMetadata.php | 52 +++++++++++++++++- .../Mapping/AttributeMetadataInterface.php | 20 +++++++ .../serializer-mapping-1.0.xsd | 14 +++++ .../Normalizer/AbstractNormalizer.php | 20 ++++++- .../Serializer/Tests/Annotation/SinceTest.php | 55 +++++++++++++++++++ .../Serializer/Tests/Annotation/UntilTest.php | 55 +++++++++++++++++++ 8 files changed, 310 insertions(+), 2 deletions(-) create mode 100644 src/Symfony/Component/Serializer/Annotation/Since.php create mode 100644 src/Symfony/Component/Serializer/Annotation/Until.php create mode 100644 src/Symfony/Component/Serializer/Tests/Annotation/SinceTest.php create mode 100644 src/Symfony/Component/Serializer/Tests/Annotation/UntilTest.php diff --git a/src/Symfony/Component/Serializer/Annotation/Since.php b/src/Symfony/Component/Serializer/Annotation/Since.php new file mode 100644 index 0000000000000..c953f956db781 --- /dev/null +++ b/src/Symfony/Component/Serializer/Annotation/Since.php @@ -0,0 +1,48 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\Serializer\Annotation; + +use Symfony\Component\Serializer\Exception\InvalidArgumentException; + +/** + * Annotation class for @Since(). + * + * @Annotation + * @Target({"PROPERTY", "METHOD"}) + * + * @author Arnaud Tarroux + */ +class Since +{ + /** + * @var string + */ + private $version; + + public function __construct(array $data) + { + if (!isset($data['value'])) { + throw new InvalidArgumentException(sprintf('Parameter of annotation "%s" should be set.', \get_class($this))); + } + + if (!\is_string($data['value']) || empty($data['value'])) { + throw new InvalidArgumentException(sprintf('Parameter of annotation "%s" must be a non-empty string.', \get_class($this))); + } + + $this->version = $data['value']; + } + + public function getVersion(): string + { + return $this->version; + } +} diff --git a/src/Symfony/Component/Serializer/Annotation/Until.php b/src/Symfony/Component/Serializer/Annotation/Until.php new file mode 100644 index 0000000000000..a9e8730b461ed --- /dev/null +++ b/src/Symfony/Component/Serializer/Annotation/Until.php @@ -0,0 +1,48 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\Serializer\Annotation; + +use Symfony\Component\Serializer\Exception\InvalidArgumentException; + +/** + * Annotation class for @Until(). + * + * @Annotation + * @Target({"PROPERTY", "METHOD"}) + * + * @author Arnaud Tarroux + */ +class Until +{ + /** + * @var string + */ + private $version; + + public function __construct(array $data) + { + if (!isset($data['value'])) { + throw new InvalidArgumentException(sprintf('Parameter of annotation "%s" should be set.', \get_class($this))); + } + + if (!\is_string($data['value']) || empty($data['value'])) { + throw new InvalidArgumentException(sprintf('Parameter of annotation "%s" must be a non-empty string.', \get_class($this))); + } + + $this->version = $data['value']; + } + + public function getVersion(): string + { + return $this->version; + } +} diff --git a/src/Symfony/Component/Serializer/Mapping/AttributeMetadata.php b/src/Symfony/Component/Serializer/Mapping/AttributeMetadata.php index 732e0bd5908cc..fd3ff0d5ca614 100644 --- a/src/Symfony/Component/Serializer/Mapping/AttributeMetadata.php +++ b/src/Symfony/Component/Serializer/Mapping/AttributeMetadata.php @@ -59,6 +59,24 @@ class AttributeMetadata implements AttributeMetadataInterface */ public $ignore = false; + /** + * @var string|null + * + * @internal This property is public in order to reduce the size of the + * class' serialized representation. Do not access it. Use + * {@link getSince()} instead. + */ + public $since; + + /** + * @var string|null + * + * @internal This property is public in order to reduce the size of the + * class' serialized representation. Do not access it. Use + * {@link getUntil()} instead. + */ + public $until; + public function __construct(string $name) { $this->name = $name; @@ -162,6 +180,38 @@ public function merge(AttributeMetadataInterface $attributeMetadata) } } + /** + * {@inheritdoc} + */ + public function setSince(string $version) + { + $this->since = $version; + } + + /** + * {@inheritdoc} + */ + public function getSince(): ?string + { + return $this->since; + } + + /** + * {@inheritdoc} + */ + public function setUntil(string $version) + { + $this->until = $version; + } + + /** + * {@inheritdoc} + */ + public function getUntil(): ?string + { + return $this->until; + } + /** * Returns the names of the properties that should be serialized. * @@ -169,6 +219,6 @@ public function merge(AttributeMetadataInterface $attributeMetadata) */ public function __sleep() { - return ['name', 'groups', 'maxDepth', 'serializedName', 'ignore']; + return ['name', 'groups', 'maxDepth', 'serializedName', 'ignore', 'since', 'until']; } } diff --git a/src/Symfony/Component/Serializer/Mapping/AttributeMetadataInterface.php b/src/Symfony/Component/Serializer/Mapping/AttributeMetadataInterface.php index 9e78cf0d31743..00f8608fd73c5 100644 --- a/src/Symfony/Component/Serializer/Mapping/AttributeMetadataInterface.php +++ b/src/Symfony/Component/Serializer/Mapping/AttributeMetadataInterface.php @@ -75,4 +75,24 @@ public function isIgnored(): bool; * Merges an {@see AttributeMetadataInterface} with in the current one. */ public function merge(self $attributeMetadata); + + /** + * Sets the version number from which the attribute must be serialized. + */ + public function setSince(string $version); + + /** + * Gets the version number from which the attribute must be serialized. + */ + public function getSince(): ?string; + + /** + * Sets the version number after which the attribute must not be serialized. + */ + public function setUntil(string $version); + + /** + * Gets the version number after which the attribute must not be serialized. + */ + public function getUntil(): ?string; } diff --git a/src/Symfony/Component/Serializer/Mapping/Loader/schema/dic/serializer-mapping/serializer-mapping-1.0.xsd b/src/Symfony/Component/Serializer/Mapping/Loader/schema/dic/serializer-mapping/serializer-mapping-1.0.xsd index b427a36e368c1..b5065c6b7b63a 100644 --- a/src/Symfony/Component/Serializer/Mapping/Loader/schema/dic/serializer-mapping/serializer-mapping-1.0.xsd +++ b/src/Symfony/Component/Serializer/Mapping/Loader/schema/dic/serializer-mapping/serializer-mapping-1.0.xsd @@ -79,6 +79,20 @@ + + + + + + + + + + + + + + diff --git a/src/Symfony/Component/Serializer/Normalizer/AbstractNormalizer.php b/src/Symfony/Component/Serializer/Normalizer/AbstractNormalizer.php index 4a03ab851a3a2..973dd89036f96 100644 --- a/src/Symfony/Component/Serializer/Normalizer/AbstractNormalizer.php +++ b/src/Symfony/Component/Serializer/Normalizer/AbstractNormalizer.php @@ -251,7 +251,8 @@ protected function getAllowedAttributes($classOrObject, array $context, bool $at if ( !$ignore && (false === $groups || array_intersect(array_merge($attributeMetadata->getGroups(), ['*']), $groups)) && - $this->isAllowedAttribute($classOrObject, $name = $attributeMetadata->getName(), null, $context) + $this->isAllowedAttribute($classOrObject, $name = $attributeMetadata->getName(), null, $context) && + $this->attributeAllowedWithVersion($context, $attributeMetadata->getSince(), $attributeMetadata->getUntil()) ) { $allowedAttributes[] = $attributesAsString ? $name : $attributeMetadata; } @@ -447,4 +448,21 @@ protected function createChildContext(array $parentContext, string $attribute, ? return $parentContext; } + + protected function attributeAllowedWithVersion(array $context, ?string $sinceVersion, ?string $untilVersion) + { + if (!isset($context['version'])) { + return true; + } + + if (null !== $sinceVersion && version_compare($sinceVersion, $context['version'], '>')) { + return false; + } + + if (null !== $untilVersion && version_compare($untilVersion, $context['version'], '<')) { + return false; + } + + return true; + } } diff --git a/src/Symfony/Component/Serializer/Tests/Annotation/SinceTest.php b/src/Symfony/Component/Serializer/Tests/Annotation/SinceTest.php new file mode 100644 index 0000000000000..25011b0e721e2 --- /dev/null +++ b/src/Symfony/Component/Serializer/Tests/Annotation/SinceTest.php @@ -0,0 +1,55 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\Serializer\Tests\Annotation; + +use PHPUnit\Framework\TestCase; +use Symfony\Component\Serializer\Annotation\Since; + +/** + * @author Arnaud Tarroux + */ +class SinceTest extends TestCase +{ + /** + * @expectedException \Symfony\Component\Serializer\Exception\InvalidArgumentException + * @expectedExceptionMessage Parameter of annotation "Symfony\Component\Serializer\Annotation\Since" should be set. + */ + public function testNotSetVersionParameter() + { + new Since([]); + } + + public function provideInvalidValues() + { + return [ + [''], + [0], + ]; + } + + /** + * @dataProvider provideInvalidValues + * + * @expectedException \Symfony\Component\Serializer\Exception\InvalidArgumentException + * @expectedExceptionMessage Parameter of annotation "Symfony\Component\Serializer\Annotation\Since" must be a non-empty string. + */ + public function testNotAStringVersionParameter($value) + { + new Since(['value' => $value]); + } + + public function testVersionParameters() + { + $since = new Since(['value' => '1.1.2']); + $this->assertEquals('1.1.2', $since->getVersion()); + } +} diff --git a/src/Symfony/Component/Serializer/Tests/Annotation/UntilTest.php b/src/Symfony/Component/Serializer/Tests/Annotation/UntilTest.php new file mode 100644 index 0000000000000..95622b1a77b8c --- /dev/null +++ b/src/Symfony/Component/Serializer/Tests/Annotation/UntilTest.php @@ -0,0 +1,55 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Component\Serializer\Tests\Annotation; + +use PHPUnit\Framework\TestCase; +use Symfony\Component\Serializer\Annotation\Until; + +/** + * @author Arnaud Tarroux + */ +class UntilTest extends TestCase +{ + /** + * @expectedException \Symfony\Component\Serializer\Exception\InvalidArgumentException + * @expectedExceptionMessage Parameter of annotation "Symfony\Component\Serializer\Annotation\Until" should be set. + */ + public function testNotSetVersionParameter() + { + new Until([]); + } + + public function provideInvalidValues() + { + return [ + [''], + [0], + ]; + } + + /** + * @dataProvider provideInvalidValues + * + * @expectedException \Symfony\Component\Serializer\Exception\InvalidArgumentException + * @expectedExceptionMessage Parameter of annotation "Symfony\Component\Serializer\Annotation\Until" must be a non-empty string. + */ + public function testNotAStringVersionParameter($value) + { + new Until(['value' => $value]); + } + + public function testVersionParameters() + { + $since = new Until(['value' => '1.1.2']); + $this->assertEquals('1.1.2', $since->getVersion()); + } +} From 6460568c211b0bdaae2b534adadf947b0fac92e1 Mon Sep 17 00:00:00 2001 From: Arnaud Tarroux Date: Thu, 6 Feb 2020 10:16:11 +0100 Subject: [PATCH 02/11] feat: Since and Until loader support --- .../Mapping/Loader/AnnotationLoader.php | 18 +++++++++ .../Mapping/Loader/XmlFileLoader.php | 8 ++++ .../Mapping/Loader/YamlFileLoader.php | 8 ++++ .../Tests/Fixtures/VersioningDummy.php | 38 +++++++++++++++++++ .../Tests/Fixtures/serialization.xml | 4 ++ .../Tests/Fixtures/serialization.yml | 6 +++ .../Mapping/Loader/AnnotationLoaderTest.php | 13 +++++++ .../Mapping/Loader/XmlFileLoaderTest.php | 11 ++++++ .../Mapping/Loader/YamlFileLoaderTest.php | 11 ++++++ 9 files changed, 117 insertions(+) create mode 100644 src/Symfony/Component/Serializer/Tests/Fixtures/VersioningDummy.php diff --git a/src/Symfony/Component/Serializer/Mapping/Loader/AnnotationLoader.php b/src/Symfony/Component/Serializer/Mapping/Loader/AnnotationLoader.php index 978fe659bbbce..7883be8200c4e 100644 --- a/src/Symfony/Component/Serializer/Mapping/Loader/AnnotationLoader.php +++ b/src/Symfony/Component/Serializer/Mapping/Loader/AnnotationLoader.php @@ -17,6 +17,8 @@ use Symfony\Component\Serializer\Annotation\Ignore; use Symfony\Component\Serializer\Annotation\MaxDepth; use Symfony\Component\Serializer\Annotation\SerializedName; +use Symfony\Component\Serializer\Annotation\Since; +use Symfony\Component\Serializer\Annotation\Until; use Symfony\Component\Serializer\Exception\MappingException; use Symfony\Component\Serializer\Mapping\AttributeMetadata; use Symfony\Component\Serializer\Mapping\ClassDiscriminatorMapping; @@ -74,6 +76,10 @@ public function loadClassMetadata(ClassMetadataInterface $classMetadata) $attributesMetadata[$property->name]->setSerializedName($annotation->getSerializedName()); } elseif ($annotation instanceof Ignore) { $attributesMetadata[$property->name]->setIgnore(true); + } elseif ($annotation instanceof Since) { + $attributesMetadata[$property->name]->setSince($annotation->getVersion()); + } elseif ($annotation instanceof Until) { + $attributesMetadata[$property->name]->setUntil($annotation->getVersion()); } $loaded = true; @@ -121,6 +127,18 @@ public function loadClassMetadata(ClassMetadataInterface $classMetadata) $attributeMetadata->setSerializedName($annotation->getSerializedName()); } elseif ($annotation instanceof Ignore) { $attributeMetadata->setIgnore(true); + } elseif ($annotation instanceof Since) { + if (!$accessorOrMutator) { + throw new MappingException(sprintf('Since on "%s::%s" cannot be added. Since can only be added on methods beginning with "get", "is", "has" or "set".', $className, $method->name)); + } + + $attributeMetadata->setSince($annotation->getVersion()); + } elseif ($annotation instanceof Until) { + if (!$accessorOrMutator) { + throw new MappingException(sprintf('Until on "%s::%s" cannot be added. Until can only be added on methods beginning with "get", "is", "has" or "set".', $className, $method->name)); + } + + $attributeMetadata->setUntil($annotation->getVersion()); } $loaded = true; diff --git a/src/Symfony/Component/Serializer/Mapping/Loader/XmlFileLoader.php b/src/Symfony/Component/Serializer/Mapping/Loader/XmlFileLoader.php index 696007afb83ad..ed4fe080fd6bd 100644 --- a/src/Symfony/Component/Serializer/Mapping/Loader/XmlFileLoader.php +++ b/src/Symfony/Component/Serializer/Mapping/Loader/XmlFileLoader.php @@ -74,6 +74,14 @@ public function loadClassMetadata(ClassMetadataInterface $classMetadata) if (isset($attribute['ignore'])) { $attributeMetadata->setIgnore((bool) $attribute['ignore']); } + + if (isset($attribute['since'])) { + $attributeMetadata->setSince((string) $attribute['since']); + } + + if (isset($attribute['until'])) { + $attributeMetadata->setUntil((string) $attribute['until']); + } } if (isset($xml->{'discriminator-map'})) { diff --git a/src/Symfony/Component/Serializer/Mapping/Loader/YamlFileLoader.php b/src/Symfony/Component/Serializer/Mapping/Loader/YamlFileLoader.php index ff50e622eeadf..997f81ae65044 100644 --- a/src/Symfony/Component/Serializer/Mapping/Loader/YamlFileLoader.php +++ b/src/Symfony/Component/Serializer/Mapping/Loader/YamlFileLoader.php @@ -101,6 +101,14 @@ public function loadClassMetadata(ClassMetadataInterface $classMetadata) $attributeMetadata->setIgnore($data['ignore']); } + + if (isset($data['since'])) { + $attributeMetadata->setSince((string) $data['since']); + } + + if (isset($data['until'])) { + $attributeMetadata->setUntil((string) $data['until']); + } } } diff --git a/src/Symfony/Component/Serializer/Tests/Fixtures/VersioningDummy.php b/src/Symfony/Component/Serializer/Tests/Fixtures/VersioningDummy.php new file mode 100644 index 0000000000000..b07bfe3ab9394 --- /dev/null +++ b/src/Symfony/Component/Serializer/Tests/Fixtures/VersioningDummy.php @@ -0,0 +1,38 @@ +foo; + } + + /** + * @Until("1.3.0") + */ + public function getUsername() + { + return $this->username; + } +} diff --git a/src/Symfony/Component/Serializer/Tests/Fixtures/serialization.xml b/src/Symfony/Component/Serializer/Tests/Fixtures/serialization.xml index 257d838b4965e..cf93a19afc2f8 100644 --- a/src/Symfony/Component/Serializer/Tests/Fixtures/serialization.xml +++ b/src/Symfony/Component/Serializer/Tests/Fixtures/serialization.xml @@ -39,4 +39,8 @@ + + + + diff --git a/src/Symfony/Component/Serializer/Tests/Fixtures/serialization.yml b/src/Symfony/Component/Serializer/Tests/Fixtures/serialization.yml index 4d98c73b04c16..91719d8eeb4c3 100644 --- a/src/Symfony/Component/Serializer/Tests/Fixtures/serialization.yml +++ b/src/Symfony/Component/Serializer/Tests/Fixtures/serialization.yml @@ -30,3 +30,9 @@ ignore: true ignored2: ignore: true + +'Symfony\Component\Serializer\Tests\Fixtures\VersionDummy': + attributes: + foo: + since: '1.0.0' + until: '1.1.9' diff --git a/src/Symfony/Component/Serializer/Tests/Mapping/Loader/AnnotationLoaderTest.php b/src/Symfony/Component/Serializer/Tests/Mapping/Loader/AnnotationLoaderTest.php index 9c5a9943e31ef..81c26c9b75f1f 100644 --- a/src/Symfony/Component/Serializer/Tests/Mapping/Loader/AnnotationLoaderTest.php +++ b/src/Symfony/Component/Serializer/Tests/Mapping/Loader/AnnotationLoaderTest.php @@ -118,4 +118,17 @@ public function testLoadIgnore() $this->assertTrue($attributesMetadata['ignored1']->isIgnored()); $this->assertTrue($attributesMetadata['ignored2']->isIgnored()); } + + public function testLoadVersion() + { + $classMetadata = new ClassMetadata('Symfony\Component\Serializer\Tests\Fixtures\VersioningDummy'); + $this->loader->loadClassMetadata($classMetadata); + + $attributesMetadata = $classMetadata->getAttributesMetadata(); + + $this->assertEquals('1.0.0', $attributesMetadata['foo']->getSince()); + $this->assertEquals('1.1.2', $attributesMetadata['bar']->getSince()); + $this->assertEquals('1.1.9', $attributesMetadata['foo']->getUntil()); + $this->assertEquals('1.3.0', $attributesMetadata['username']->getUntil()); + } } diff --git a/src/Symfony/Component/Serializer/Tests/Mapping/Loader/XmlFileLoaderTest.php b/src/Symfony/Component/Serializer/Tests/Mapping/Loader/XmlFileLoaderTest.php index 4d30c8e2cbfe9..ddd013c396b6e 100644 --- a/src/Symfony/Component/Serializer/Tests/Mapping/Loader/XmlFileLoaderTest.php +++ b/src/Symfony/Component/Serializer/Tests/Mapping/Loader/XmlFileLoaderTest.php @@ -103,4 +103,15 @@ public function testLoadIgnore() $this->assertTrue($attributesMetadata['ignored1']->isIgnored()); $this->assertTrue($attributesMetadata['ignored2']->isIgnored()); } + + public function testVersion() + { + $classMetadata = new ClassMetadata('Symfony\Component\Serializer\Tests\Fixtures\VersionDummy'); + $this->loader->loadClassMetadata($classMetadata); + + $attributesMetadata = $classMetadata->getAttributesMetadata(); + + $this->assertEquals('1.2.0', $attributesMetadata['foo']->getSince()); + $this->assertEquals('1.9.1', $attributesMetadata['foo']->getUntil()); + } } diff --git a/src/Symfony/Component/Serializer/Tests/Mapping/Loader/YamlFileLoaderTest.php b/src/Symfony/Component/Serializer/Tests/Mapping/Loader/YamlFileLoaderTest.php index 6d2ff5c0bdb8e..345733ef0846d 100644 --- a/src/Symfony/Component/Serializer/Tests/Mapping/Loader/YamlFileLoaderTest.php +++ b/src/Symfony/Component/Serializer/Tests/Mapping/Loader/YamlFileLoaderTest.php @@ -125,4 +125,15 @@ public function testLoadInvalidIgnore() (new YamlFileLoader(__DIR__.'/../../Fixtures/invalid-ignore.yml'))->loadClassMetadata(new ClassMetadata(IgnoreDummy::class)); } + + public function testVersion() + { + $classMetadata = new ClassMetadata('Symfony\Component\Serializer\Tests\Fixtures\VersionDummy'); + $this->loader->loadClassMetadata($classMetadata); + + $attributesMetadata = $classMetadata->getAttributesMetadata(); + + $this->assertEquals('1.0.0', $attributesMetadata['foo']->getSince()); + $this->assertEquals('1.1.9', $attributesMetadata['foo']->getUntil()); + } } From d850b8ca3c3130ea922ccbbf58299bb7783f1373 Mon Sep 17 00:00:00 2001 From: Arnaud Tarroux Date: Thu, 6 Feb 2020 10:37:40 +0100 Subject: [PATCH 03/11] refacto: remove deprecated annotations --- .../Serializer/Tests/Annotation/SinceTest.php | 16 +++++++++------- .../Serializer/Tests/Annotation/UntilTest.php | 16 +++++++++------- 2 files changed, 18 insertions(+), 14 deletions(-) diff --git a/src/Symfony/Component/Serializer/Tests/Annotation/SinceTest.php b/src/Symfony/Component/Serializer/Tests/Annotation/SinceTest.php index 25011b0e721e2..539510143c696 100644 --- a/src/Symfony/Component/Serializer/Tests/Annotation/SinceTest.php +++ b/src/Symfony/Component/Serializer/Tests/Annotation/SinceTest.php @@ -13,18 +13,19 @@ use PHPUnit\Framework\TestCase; use Symfony\Component\Serializer\Annotation\Since; +use Symfony\Component\Serializer\Exception\InvalidArgumentException; /** * @author Arnaud Tarroux */ class SinceTest extends TestCase { - /** - * @expectedException \Symfony\Component\Serializer\Exception\InvalidArgumentException - * @expectedExceptionMessage Parameter of annotation "Symfony\Component\Serializer\Annotation\Since" should be set. - */ public function testNotSetVersionParameter() { + $this->expectException(InvalidArgumentException::class); + $this->expectExceptionMessage( + 'Parameter of annotation "Symfony\Component\Serializer\Annotation\Since" should be set.' + ); new Since([]); } @@ -38,12 +39,13 @@ public function provideInvalidValues() /** * @dataProvider provideInvalidValues - * - * @expectedException \Symfony\Component\Serializer\Exception\InvalidArgumentException - * @expectedExceptionMessage Parameter of annotation "Symfony\Component\Serializer\Annotation\Since" must be a non-empty string. */ public function testNotAStringVersionParameter($value) { + $this->expectException(InvalidArgumentException::class); + $this->expectExceptionMessage( + 'Parameter of annotation "Symfony\Component\Serializer\Annotation\Since" must be a non-empty string.' + ); new Since(['value' => $value]); } diff --git a/src/Symfony/Component/Serializer/Tests/Annotation/UntilTest.php b/src/Symfony/Component/Serializer/Tests/Annotation/UntilTest.php index 95622b1a77b8c..b00ec441aabad 100644 --- a/src/Symfony/Component/Serializer/Tests/Annotation/UntilTest.php +++ b/src/Symfony/Component/Serializer/Tests/Annotation/UntilTest.php @@ -13,18 +13,19 @@ use PHPUnit\Framework\TestCase; use Symfony\Component\Serializer\Annotation\Until; +use Symfony\Component\Serializer\Exception\InvalidArgumentException; /** * @author Arnaud Tarroux */ class UntilTest extends TestCase { - /** - * @expectedException \Symfony\Component\Serializer\Exception\InvalidArgumentException - * @expectedExceptionMessage Parameter of annotation "Symfony\Component\Serializer\Annotation\Until" should be set. - */ public function testNotSetVersionParameter() { + $this->expectException(InvalidArgumentException::class); + $this->expectExceptionMessage( + 'Parameter of annotation "Symfony\Component\Serializer\Annotation\Until" should be set.' + ); new Until([]); } @@ -38,12 +39,13 @@ public function provideInvalidValues() /** * @dataProvider provideInvalidValues - * - * @expectedException \Symfony\Component\Serializer\Exception\InvalidArgumentException - * @expectedExceptionMessage Parameter of annotation "Symfony\Component\Serializer\Annotation\Until" must be a non-empty string. */ public function testNotAStringVersionParameter($value) { + $this->expectException(InvalidArgumentException::class); + $this->expectExceptionMessage( + 'Parameter of annotation "Symfony\Component\Serializer\Annotation\Until" must be a non-empty string.' + ); new Until(['value' => $value]); } From 5c8fe81f492f9d833c2d9ad0a86e85e2358cefa4 Mon Sep 17 00:00:00 2001 From: Arnaud Tarroux Date: Thu, 6 Feb 2020 10:46:45 +0100 Subject: [PATCH 04/11] feat: Version comparison using semver --- .../Normalizer/AbstractNormalizer.php | 5 ++- .../Normalizer/AbstractNormalizerTest.php | 38 +++++++++++++++++++ .../Component/Serializer/composer.json | 3 +- 3 files changed, 43 insertions(+), 3 deletions(-) diff --git a/src/Symfony/Component/Serializer/Normalizer/AbstractNormalizer.php b/src/Symfony/Component/Serializer/Normalizer/AbstractNormalizer.php index 973dd89036f96..a3c7825ee810c 100644 --- a/src/Symfony/Component/Serializer/Normalizer/AbstractNormalizer.php +++ b/src/Symfony/Component/Serializer/Normalizer/AbstractNormalizer.php @@ -11,6 +11,7 @@ namespace Symfony\Component\Serializer\Normalizer; +use Composer\Semver\Comparator; use Symfony\Component\Serializer\Exception\CircularReferenceException; use Symfony\Component\Serializer\Exception\InvalidArgumentException; use Symfony\Component\Serializer\Exception\LogicException; @@ -455,11 +456,11 @@ protected function attributeAllowedWithVersion(array $context, ?string $sinceVer return true; } - if (null !== $sinceVersion && version_compare($sinceVersion, $context['version'], '>')) { + if (null !== $sinceVersion && Comparator::lessThan($context['version'], $sinceVersion)) { return false; } - if (null !== $untilVersion && version_compare($untilVersion, $context['version'], '<')) { + if (null !== $untilVersion && Comparator::greaterThanOrEqualTo($context['version'], $untilVersion)) { return false; } diff --git a/src/Symfony/Component/Serializer/Tests/Normalizer/AbstractNormalizerTest.php b/src/Symfony/Component/Serializer/Tests/Normalizer/AbstractNormalizerTest.php index 082b427ab7caa..3bba00aa27f17 100644 --- a/src/Symfony/Component/Serializer/Tests/Normalizer/AbstractNormalizerTest.php +++ b/src/Symfony/Component/Serializer/Tests/Normalizer/AbstractNormalizerTest.php @@ -190,4 +190,42 @@ public function testIgnore() $this->assertSame([], $normalizer->normalize($dummy)); } + + public function testObjectWithVersioning() + { + $classMetadata = new ClassMetadata('c'); + + $a = new AttributeMetadata('a'); + $a->setSince('1.8.0'); + $a->addGroup('test'); + $classMetadata->addAttributeMetadata($a); + + $b = new AttributeMetadata('b'); + $b->setUntil('2.1.0'); + $b->addGroup('test'); + $classMetadata->addAttributeMetadata($b); + + $c = new AttributeMetadata('c'); + $c->setSince('1.8.0'); + $c->setUntil('2.0.0'); + $c->addGroup('test'); + $classMetadata->addAttributeMetadata($c); + + $this->classMetadata->method('getMetadataFor')->willReturn($classMetadata); + + $result = $this->normalizer->getAllowedAttributes('c', [ + AbstractNormalizer::GROUPS => ['test'], 'version' => '1.8.0', + ], true); + $this->assertEquals(['a', 'b', 'c'], $result); + + $result = $this->normalizer->getAllowedAttributes('c', [ + AbstractNormalizer::GROUPS => ['test'], 'version' => '1.7.0', + ], true); + $this->assertEquals(['b'], $result); + + $result = $this->normalizer->getAllowedAttributes('c', [ + AbstractNormalizer::GROUPS => ['test'], 'version' => '2.3.0', + ], true); + $this->assertEquals(['a'], $result); + } } diff --git a/src/Symfony/Component/Serializer/composer.json b/src/Symfony/Component/Serializer/composer.json index 9e6b2d60ab5e4..0ba6b514a08f7 100644 --- a/src/Symfony/Component/Serializer/composer.json +++ b/src/Symfony/Component/Serializer/composer.json @@ -18,7 +18,8 @@ "require": { "php": ">=7.2.5", "symfony/polyfill-ctype": "~1.8", - "symfony/polyfill-php80": "^1.15" + "symfony/polyfill-php80": "^1.15", + "composer/semver": "^1.0@dev" }, "require-dev": { "doctrine/annotations": "~1.0", From 2f96866684858ddcaba4adb9d7e2a84c8b6a55d3 Mon Sep 17 00:00:00 2001 From: Arnaud Tarroux Date: Tue, 25 Aug 2020 11:58:37 +0200 Subject: [PATCH 05/11] refacto: require composer/semver 3.0 --- .../Component/Serializer/Normalizer/AbstractNormalizer.php | 3 +++ src/Symfony/Component/Serializer/composer.json | 2 +- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/src/Symfony/Component/Serializer/Normalizer/AbstractNormalizer.php b/src/Symfony/Component/Serializer/Normalizer/AbstractNormalizer.php index a3c7825ee810c..d3107c33d3f42 100644 --- a/src/Symfony/Component/Serializer/Normalizer/AbstractNormalizer.php +++ b/src/Symfony/Component/Serializer/Normalizer/AbstractNormalizer.php @@ -450,6 +450,9 @@ protected function createChildContext(array $parentContext, string $attribute, ? return $parentContext; } + /** + * @internal + */ protected function attributeAllowedWithVersion(array $context, ?string $sinceVersion, ?string $untilVersion) { if (!isset($context['version'])) { diff --git a/src/Symfony/Component/Serializer/composer.json b/src/Symfony/Component/Serializer/composer.json index 0ba6b514a08f7..2fc8347fbb732 100644 --- a/src/Symfony/Component/Serializer/composer.json +++ b/src/Symfony/Component/Serializer/composer.json @@ -19,7 +19,7 @@ "php": ">=7.2.5", "symfony/polyfill-ctype": "~1.8", "symfony/polyfill-php80": "^1.15", - "composer/semver": "^1.0@dev" + "composer/semver": "^3.0" }, "require-dev": { "doctrine/annotations": "~1.0", From e6057d87ae7fcbc9dee4d01997b63b8b3e83b64f Mon Sep 17 00:00:00 2001 From: Arnaud Tarroux Date: Tue, 25 Aug 2020 14:53:07 +0200 Subject: [PATCH 06/11] refacto: Fabbot's required fixes --- src/Symfony/Component/Serializer/Annotation/Since.php | 4 ++-- src/Symfony/Component/Serializer/Annotation/Until.php | 4 ++-- .../Serializer/Tests/Mapping/Loader/AnnotationLoaderTest.php | 2 +- 3 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/Symfony/Component/Serializer/Annotation/Since.php b/src/Symfony/Component/Serializer/Annotation/Since.php index c953f956db781..7117e9ccef50a 100644 --- a/src/Symfony/Component/Serializer/Annotation/Since.php +++ b/src/Symfony/Component/Serializer/Annotation/Since.php @@ -31,11 +31,11 @@ class Since public function __construct(array $data) { if (!isset($data['value'])) { - throw new InvalidArgumentException(sprintf('Parameter of annotation "%s" should be set.', \get_class($this))); + throw new InvalidArgumentException(sprintf('Parameter of annotation "%s" should be set.', static::class)); } if (!\is_string($data['value']) || empty($data['value'])) { - throw new InvalidArgumentException(sprintf('Parameter of annotation "%s" must be a non-empty string.', \get_class($this))); + throw new InvalidArgumentException(sprintf('Parameter of annotation "%s" must be a non-empty string.', static::class)); } $this->version = $data['value']; diff --git a/src/Symfony/Component/Serializer/Annotation/Until.php b/src/Symfony/Component/Serializer/Annotation/Until.php index a9e8730b461ed..16f1616addfe5 100644 --- a/src/Symfony/Component/Serializer/Annotation/Until.php +++ b/src/Symfony/Component/Serializer/Annotation/Until.php @@ -31,11 +31,11 @@ class Until public function __construct(array $data) { if (!isset($data['value'])) { - throw new InvalidArgumentException(sprintf('Parameter of annotation "%s" should be set.', \get_class($this))); + throw new InvalidArgumentException(sprintf('Parameter of annotation "%s" should be set.', static::class)); } if (!\is_string($data['value']) || empty($data['value'])) { - throw new InvalidArgumentException(sprintf('Parameter of annotation "%s" must be a non-empty string.', \get_class($this))); + throw new InvalidArgumentException(sprintf('Parameter of annotation "%s" must be a non-empty string.', static::class)); } $this->version = $data['value']; diff --git a/src/Symfony/Component/Serializer/Tests/Mapping/Loader/AnnotationLoaderTest.php b/src/Symfony/Component/Serializer/Tests/Mapping/Loader/AnnotationLoaderTest.php index 81c26c9b75f1f..bf67d45be168d 100644 --- a/src/Symfony/Component/Serializer/Tests/Mapping/Loader/AnnotationLoaderTest.php +++ b/src/Symfony/Component/Serializer/Tests/Mapping/Loader/AnnotationLoaderTest.php @@ -20,8 +20,8 @@ use Symfony\Component\Serializer\Tests\Fixtures\AbstractDummy; use Symfony\Component\Serializer\Tests\Fixtures\AbstractDummyFirstChild; use Symfony\Component\Serializer\Tests\Fixtures\AbstractDummySecondChild; -use Symfony\Component\Serializer\Tests\Fixtures\IgnoreDummy; use Symfony\Component\Serializer\Tests\Fixtures\AbstractDummyThirdChild; +use Symfony\Component\Serializer\Tests\Fixtures\IgnoreDummy; use Symfony\Component\Serializer\Tests\Mapping\TestClassMetadataFactory; /** From 7421f5d6f34be221e27ae1fb1b616b47fc55bf54 Mon Sep 17 00:00:00 2001 From: Arnaud Tarroux Date: Mon, 31 Aug 2020 21:49:59 +0200 Subject: [PATCH 07/11] refacto, use version_compare instead of Compsoer/Semver --- .../Component/Serializer/Normalizer/AbstractNormalizer.php | 5 ++--- src/Symfony/Component/Serializer/composer.json | 3 +-- 2 files changed, 3 insertions(+), 5 deletions(-) diff --git a/src/Symfony/Component/Serializer/Normalizer/AbstractNormalizer.php b/src/Symfony/Component/Serializer/Normalizer/AbstractNormalizer.php index d3107c33d3f42..d65d9932806b1 100644 --- a/src/Symfony/Component/Serializer/Normalizer/AbstractNormalizer.php +++ b/src/Symfony/Component/Serializer/Normalizer/AbstractNormalizer.php @@ -11,7 +11,6 @@ namespace Symfony\Component\Serializer\Normalizer; -use Composer\Semver\Comparator; use Symfony\Component\Serializer\Exception\CircularReferenceException; use Symfony\Component\Serializer\Exception\InvalidArgumentException; use Symfony\Component\Serializer\Exception\LogicException; @@ -459,11 +458,11 @@ protected function attributeAllowedWithVersion(array $context, ?string $sinceVer return true; } - if (null !== $sinceVersion && Comparator::lessThan($context['version'], $sinceVersion)) { + if (null !== $sinceVersion && version_compare($context['version'], $sinceVersion, '<')) { return false; } - if (null !== $untilVersion && Comparator::greaterThanOrEqualTo($context['version'], $untilVersion)) { + if (null !== $untilVersion && version_compare($context['version'], $untilVersion, '>=')) { return false; } diff --git a/src/Symfony/Component/Serializer/composer.json b/src/Symfony/Component/Serializer/composer.json index 2fc8347fbb732..9e6b2d60ab5e4 100644 --- a/src/Symfony/Component/Serializer/composer.json +++ b/src/Symfony/Component/Serializer/composer.json @@ -18,8 +18,7 @@ "require": { "php": ">=7.2.5", "symfony/polyfill-ctype": "~1.8", - "symfony/polyfill-php80": "^1.15", - "composer/semver": "^3.0" + "symfony/polyfill-php80": "^1.15" }, "require-dev": { "doctrine/annotations": "~1.0", From 4915cefa0466e5252d5d9d9a2928840824118176 Mon Sep 17 00:00:00 2001 From: Arnaud Tarroux Date: Mon, 31 Aug 2020 21:51:06 +0200 Subject: [PATCH 08/11] tests: use assertSame instead of assertEquals --- .../Component/Serializer/Tests/Annotation/SinceTest.php | 2 +- .../Component/Serializer/Tests/Annotation/UntilTest.php | 2 +- .../Tests/Mapping/Loader/AnnotationLoaderTest.php | 8 ++++---- .../Serializer/Tests/Mapping/Loader/XmlFileLoaderTest.php | 4 ++-- .../Tests/Mapping/Loader/YamlFileLoaderTest.php | 4 ++-- .../Tests/Normalizer/AbstractNormalizerTest.php | 6 +++--- 6 files changed, 13 insertions(+), 13 deletions(-) diff --git a/src/Symfony/Component/Serializer/Tests/Annotation/SinceTest.php b/src/Symfony/Component/Serializer/Tests/Annotation/SinceTest.php index 539510143c696..8fa8cf8c5339f 100644 --- a/src/Symfony/Component/Serializer/Tests/Annotation/SinceTest.php +++ b/src/Symfony/Component/Serializer/Tests/Annotation/SinceTest.php @@ -52,6 +52,6 @@ public function testNotAStringVersionParameter($value) public function testVersionParameters() { $since = new Since(['value' => '1.1.2']); - $this->assertEquals('1.1.2', $since->getVersion()); + $this->assertSame('1.1.2', $since->getVersion()); } } diff --git a/src/Symfony/Component/Serializer/Tests/Annotation/UntilTest.php b/src/Symfony/Component/Serializer/Tests/Annotation/UntilTest.php index b00ec441aabad..9021a23b56f4c 100644 --- a/src/Symfony/Component/Serializer/Tests/Annotation/UntilTest.php +++ b/src/Symfony/Component/Serializer/Tests/Annotation/UntilTest.php @@ -52,6 +52,6 @@ public function testNotAStringVersionParameter($value) public function testVersionParameters() { $since = new Until(['value' => '1.1.2']); - $this->assertEquals('1.1.2', $since->getVersion()); + $this->assertSame('1.1.2', $since->getVersion()); } } diff --git a/src/Symfony/Component/Serializer/Tests/Mapping/Loader/AnnotationLoaderTest.php b/src/Symfony/Component/Serializer/Tests/Mapping/Loader/AnnotationLoaderTest.php index bf67d45be168d..3e3299250e25b 100644 --- a/src/Symfony/Component/Serializer/Tests/Mapping/Loader/AnnotationLoaderTest.php +++ b/src/Symfony/Component/Serializer/Tests/Mapping/Loader/AnnotationLoaderTest.php @@ -126,9 +126,9 @@ public function testLoadVersion() $attributesMetadata = $classMetadata->getAttributesMetadata(); - $this->assertEquals('1.0.0', $attributesMetadata['foo']->getSince()); - $this->assertEquals('1.1.2', $attributesMetadata['bar']->getSince()); - $this->assertEquals('1.1.9', $attributesMetadata['foo']->getUntil()); - $this->assertEquals('1.3.0', $attributesMetadata['username']->getUntil()); + $this->assertSame('1.0.0', $attributesMetadata['foo']->getSince()); + $this->assertSame('1.1.2', $attributesMetadata['bar']->getSince()); + $this->assertSame('1.1.9', $attributesMetadata['foo']->getUntil()); + $this->assertSame('1.3.0', $attributesMetadata['username']->getUntil()); } } diff --git a/src/Symfony/Component/Serializer/Tests/Mapping/Loader/XmlFileLoaderTest.php b/src/Symfony/Component/Serializer/Tests/Mapping/Loader/XmlFileLoaderTest.php index ddd013c396b6e..b71fbfd27a44e 100644 --- a/src/Symfony/Component/Serializer/Tests/Mapping/Loader/XmlFileLoaderTest.php +++ b/src/Symfony/Component/Serializer/Tests/Mapping/Loader/XmlFileLoaderTest.php @@ -111,7 +111,7 @@ public function testVersion() $attributesMetadata = $classMetadata->getAttributesMetadata(); - $this->assertEquals('1.2.0', $attributesMetadata['foo']->getSince()); - $this->assertEquals('1.9.1', $attributesMetadata['foo']->getUntil()); + $this->assertSame('1.2.0', $attributesMetadata['foo']->getSince()); + $this->assertSame('1.9.1', $attributesMetadata['foo']->getUntil()); } } diff --git a/src/Symfony/Component/Serializer/Tests/Mapping/Loader/YamlFileLoaderTest.php b/src/Symfony/Component/Serializer/Tests/Mapping/Loader/YamlFileLoaderTest.php index 345733ef0846d..32d5d217b5f89 100644 --- a/src/Symfony/Component/Serializer/Tests/Mapping/Loader/YamlFileLoaderTest.php +++ b/src/Symfony/Component/Serializer/Tests/Mapping/Loader/YamlFileLoaderTest.php @@ -133,7 +133,7 @@ public function testVersion() $attributesMetadata = $classMetadata->getAttributesMetadata(); - $this->assertEquals('1.0.0', $attributesMetadata['foo']->getSince()); - $this->assertEquals('1.1.9', $attributesMetadata['foo']->getUntil()); + $this->assertSame('1.0.0', $attributesMetadata['foo']->getSince()); + $this->assertSame('1.1.9', $attributesMetadata['foo']->getUntil()); } } diff --git a/src/Symfony/Component/Serializer/Tests/Normalizer/AbstractNormalizerTest.php b/src/Symfony/Component/Serializer/Tests/Normalizer/AbstractNormalizerTest.php index 3bba00aa27f17..3f308591e946c 100644 --- a/src/Symfony/Component/Serializer/Tests/Normalizer/AbstractNormalizerTest.php +++ b/src/Symfony/Component/Serializer/Tests/Normalizer/AbstractNormalizerTest.php @@ -216,16 +216,16 @@ public function testObjectWithVersioning() $result = $this->normalizer->getAllowedAttributes('c', [ AbstractNormalizer::GROUPS => ['test'], 'version' => '1.8.0', ], true); - $this->assertEquals(['a', 'b', 'c'], $result); + $this->assertSame(['a', 'b', 'c'], $result); $result = $this->normalizer->getAllowedAttributes('c', [ AbstractNormalizer::GROUPS => ['test'], 'version' => '1.7.0', ], true); - $this->assertEquals(['b'], $result); + $this->assertSame(['b'], $result); $result = $this->normalizer->getAllowedAttributes('c', [ AbstractNormalizer::GROUPS => ['test'], 'version' => '2.3.0', ], true); - $this->assertEquals(['a'], $result); + $this->assertSame(['a'], $result); } } From 834c1d935e2e85ddcb8898b485a58847f0775c0d Mon Sep 17 00:00:00 2001 From: Arnaud Tarroux Date: Mon, 31 Aug 2020 21:52:13 +0200 Subject: [PATCH 09/11] refacto: make attributeAllowedWithVersion function private --- .../Component/Serializer/Normalizer/AbstractNormalizer.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Symfony/Component/Serializer/Normalizer/AbstractNormalizer.php b/src/Symfony/Component/Serializer/Normalizer/AbstractNormalizer.php index d65d9932806b1..a9bb29f797d66 100644 --- a/src/Symfony/Component/Serializer/Normalizer/AbstractNormalizer.php +++ b/src/Symfony/Component/Serializer/Normalizer/AbstractNormalizer.php @@ -452,7 +452,7 @@ protected function createChildContext(array $parentContext, string $attribute, ? /** * @internal */ - protected function attributeAllowedWithVersion(array $context, ?string $sinceVersion, ?string $untilVersion) + private function attributeAllowedWithVersion(array $context, ?string $sinceVersion, ?string $untilVersion) { if (!isset($context['version'])) { return true; From 1ac9c80747681351bd427479dc560296505667b7 Mon Sep 17 00:00:00 2001 From: Arnaud Tarroux Date: Tue, 1 Sep 2020 09:49:26 +0200 Subject: [PATCH 10/11] feature: Added the possibility to specify a default version in the framework config --- .../FrameworkBundle/DependencyInjection/Configuration.php | 1 + .../DependencyInjection/FrameworkExtension.php | 6 ++++++ .../Resources/config/schema/symfony-1.0.xsd | 1 + .../Tests/DependencyInjection/Fixtures/php/full.php | 1 + .../Tests/DependencyInjection/Fixtures/xml/full.xml | 2 +- .../Tests/DependencyInjection/Fixtures/yml/full.yml | 1 + .../Tests/DependencyInjection/FrameworkExtensionTest.php | 2 ++ .../Component/Serializer/Normalizer/AbstractNormalizer.php | 7 ++++--- 8 files changed, 17 insertions(+), 4 deletions(-) diff --git a/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Configuration.php b/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Configuration.php index 085ceb5dafabb..62c19e9ee75d7 100644 --- a/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Configuration.php +++ b/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/Configuration.php @@ -934,6 +934,7 @@ private function addSerializerSection(ArrayNodeDefinition $rootNode) ->end() ->end() ->end() + ->scalarNode('default_version')->end() ->end() ->end() ->end() diff --git a/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php b/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php index e929724c7fd86..700eb4da3ca98 100644 --- a/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php +++ b/src/Symfony/Bundle/FrameworkBundle/DependencyInjection/FrameworkExtension.php @@ -1598,6 +1598,12 @@ private function registerSerializerConfiguration(array $config, ContainerBuilder $defaultContext += ['max_depth_handler' => new Reference($config['max_depth_handler'])]; $container->getDefinition('serializer.normalizer.object')->replaceArgument(6, $defaultContext); } + + if (isset($config['default_version']) && $config['default_version']) { + $defaultContext = $container->getDefinition('serializer.normalizer.object')->getArgument(6); + $defaultContext += ['version' => $config['default_version']]; + $container->getDefinition('serializer.normalizer.object')->replaceArgument(6, $defaultContext); + } } private function registerPropertyInfoConfiguration(ContainerBuilder $container, PhpFileLoader $loader) diff --git a/src/Symfony/Bundle/FrameworkBundle/Resources/config/schema/symfony-1.0.xsd b/src/Symfony/Bundle/FrameworkBundle/Resources/config/schema/symfony-1.0.xsd index 08cea8ecee7ab..5514e09204588 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Resources/config/schema/symfony-1.0.xsd +++ b/src/Symfony/Bundle/FrameworkBundle/Resources/config/schema/symfony-1.0.xsd @@ -251,6 +251,7 @@ + diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/php/full.php b/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/php/full.php index 647044e613798..91f522fb1eae8 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/php/full.php +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/php/full.php @@ -67,6 +67,7 @@ 'name_converter' => 'serializer.name_converter.camel_case_to_snake_case', 'circular_reference_handler' => 'my.circular.reference.handler', 'max_depth_handler' => 'my.max.depth.handler', + 'default_version' => '1.0.0', ], 'property_info' => true, 'ide' => 'file%%link%%format', diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/xml/full.xml b/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/xml/full.xml index 9207066f1c183..cd38bbab6e2f5 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/xml/full.xml +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/xml/full.xml @@ -33,7 +33,7 @@ - + diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/yml/full.yml b/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/yml/full.yml index 2206585863baa..390aaaca73df4 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/yml/full.yml +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/yml/full.yml @@ -55,6 +55,7 @@ framework: name_converter: serializer.name_converter.camel_case_to_snake_case circular_reference_handler: my.circular.reference.handler max_depth_handler: my.max.depth.handler + default_version: 1.0.0 property_info: ~ ide: file%%link%%format request: diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/FrameworkExtensionTest.php b/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/FrameworkExtensionTest.php index 99054524d004c..f013178fea31a 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/FrameworkExtensionTest.php +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/FrameworkExtensionTest.php @@ -1108,6 +1108,8 @@ public function testSerializerEnabled() $this->assertArrayHasKey('circular_reference_handler', $container->getDefinition('serializer.normalizer.object')->getArgument(6)); $this->assertArrayHasKey('max_depth_handler', $container->getDefinition('serializer.normalizer.object')->getArgument(6)); $this->assertEquals($container->getDefinition('serializer.normalizer.object')->getArgument(6)['max_depth_handler'], new Reference('my.max.depth.handler')); + $this->assertArrayHasKey('version', $container->getDefinition('serializer.normalizer.object')->getArgument(6)); + $this->assertEquals($container->getDefinition('serializer.normalizer.object')->getArgument(6)['version'], '1.0.0'); } public function testRegisterSerializerExtractor() diff --git a/src/Symfony/Component/Serializer/Normalizer/AbstractNormalizer.php b/src/Symfony/Component/Serializer/Normalizer/AbstractNormalizer.php index a9bb29f797d66..1a91a9d0a33d2 100644 --- a/src/Symfony/Component/Serializer/Normalizer/AbstractNormalizer.php +++ b/src/Symfony/Component/Serializer/Normalizer/AbstractNormalizer.php @@ -454,15 +454,16 @@ protected function createChildContext(array $parentContext, string $attribute, ? */ private function attributeAllowedWithVersion(array $context, ?string $sinceVersion, ?string $untilVersion) { - if (!isset($context['version'])) { + if (!isset($context['version']) && !isset($this->defaultContext['version'])) { return true; } - if (null !== $sinceVersion && version_compare($context['version'], $sinceVersion, '<')) { + $version = $context['version'] ?? $this->defaultContext['version']; + if (null !== $sinceVersion && version_compare($version, $sinceVersion, '<')) { return false; } - if (null !== $untilVersion && version_compare($context['version'], $untilVersion, '>=')) { + if (null !== $untilVersion && version_compare($version, $untilVersion, '>=')) { return false; } From d7317c62d8e01f9277cba0bec427f7bce8d40c90 Mon Sep 17 00:00:00 2001 From: Arnaud Tarroux Date: Tue, 1 Sep 2020 18:17:09 +0200 Subject: [PATCH 11/11] refacto: typo in xml and schema --- .../FrameworkBundle/Resources/config/schema/symfony-1.0.xsd | 2 +- .../Tests/DependencyInjection/Fixtures/xml/full.xml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Symfony/Bundle/FrameworkBundle/Resources/config/schema/symfony-1.0.xsd b/src/Symfony/Bundle/FrameworkBundle/Resources/config/schema/symfony-1.0.xsd index 5514e09204588..a54d5fc981fe3 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Resources/config/schema/symfony-1.0.xsd +++ b/src/Symfony/Bundle/FrameworkBundle/Resources/config/schema/symfony-1.0.xsd @@ -251,7 +251,7 @@ - + diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/xml/full.xml b/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/xml/full.xml index cd38bbab6e2f5..5ceccf58e9b47 100644 --- a/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/xml/full.xml +++ b/src/Symfony/Bundle/FrameworkBundle/Tests/DependencyInjection/Fixtures/xml/full.xml @@ -33,7 +33,7 @@ - +