Skip to content

Commit

Permalink
Merge pull request schmittjoh#1410 from simPod/fix-readonly-annotation
Browse files Browse the repository at this point in the history
Fix compatibility of ReadOnly annotation for PHP8.1
  • Loading branch information
goetas authored May 11, 2022
2 parents 404e9cd + 053267d commit 0207dc2
Show file tree
Hide file tree
Showing 7 changed files with 153 additions and 16 deletions.
2 changes: 1 addition & 1 deletion phpstan-7-4.neon.dist
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ parameters:
- '~Class Doctrine\\ODM\\MongoDB\\PersistentCollection not found~'
- '~Class Symfony\\(Contracts|Component)\\Translation\\TranslatorInterface not found~'
- '#Constructor of class JMS\\Serializer\\Annotation\\.*? has an unused parameter#'
- '#Class JMS\\Serializer\\Annotation\\DeprecatedReadOnly extends @final class JMS\\Serializer\\Annotation\\ReadOnlyProperty.#'

paths:
- %currentWorkingDirectory%/src
- %currentWorkingDirectory%/tests
excludePaths:
- %currentWorkingDirectory%/src/Annotation/ReadOnly.php
- %currentWorkingDirectory%/tests/Fixtures/TypedProperties/UnionTypedProperties.php
- %currentWorkingDirectory%/tests/Fixtures/TypedProperties/UnionTypedProperties.php
- %currentWorkingDirectory%/tests/Metadata/Driver/UnionTypedPropertiesDriverTest.php
Expand Down
4 changes: 1 addition & 3 deletions phpstan.neon.dist
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,8 @@ parameters:
- '~Class Doctrine\\ODM\\MongoDB\\PersistentCollection not found~'
- '~Class Symfony\\(Contracts|Component)\\Translation\\TranslatorInterface not found~'
- '#Constructor of class JMS\\Serializer\\Annotation\\.*? has an unused parameter#'
- '#Class JMS\\Serializer\\Annotation\\DeprecatedReadOnly extends @final class JMS\\Serializer\\Annotation\\ReadOnlyProperty.#'

paths:
- %currentWorkingDirectory%/src
- %currentWorkingDirectory%/tests
excludePaths:
- %currentWorkingDirectory%/src/Annotation/ReadOnly.php

16 changes: 16 additions & 0 deletions src/Annotation/DeprecatedReadOnly.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

declare(strict_types=1);

namespace JMS\Serializer\Annotation;

/**
* @Annotation
* @Target({"CLASS","PROPERTY"})
*
* @deprecated use `@ReadOnlyProperty` instead
*/
#[\Attribute(\Attribute::TARGET_CLASS | \Attribute::TARGET_PROPERTY)]
final class DeprecatedReadOnly extends ReadOnlyProperty
{
}
13 changes: 1 addition & 12 deletions src/Annotation/ReadOnly.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,4 @@

declare(strict_types=1);

namespace JMS\Serializer\Annotation;

/**
* @Annotation
* @Target({"CLASS","PROPERTY"})
*
* @deprecated use `@ReadOnlyProperty` instead
*/
#[\Attribute(\Attribute::TARGET_CLASS | \Attribute::TARGET_PROPERTY)]
final class ReadOnly extends ReadOnlyProperty
{
}
class_alias('JMS\Serializer\Annotation\DeprecatedReadOnly', 'JMS\Serializer\Annotation\ReadOnly');
53 changes: 53 additions & 0 deletions tests/Fixtures/AuthorDeprecatedReadOnly.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

declare(strict_types=1);

namespace JMS\Serializer\Tests\Fixtures;

use JMS\Serializer\Annotation\Accessor;
use JMS\Serializer\Annotation\ReadOnly;
use JMS\Serializer\Annotation\SerializedName;
use JMS\Serializer\Annotation\Type;
use JMS\Serializer\Annotation\XmlRoot;

/**
* @deprecated ReadOnly annotation is deprecated
*
* @XmlRoot("author")
*/
#[XmlRoot(name: 'author')]
class AuthorDeprecatedReadOnly
{
/**
* @JMS\Serializer\Annotation\ReadOnly
* @SerializedName("id")
*/
#[SerializedName(name: 'id')]
private $id;

/**
* @Type("string")
* @SerializedName("full_name")
* @Accessor("getName")
*/
#[Type(name: 'string')]
#[SerializedName(name: 'full_name')]
#[Accessor(getter: 'getName')]
private $name;

public function __construct($id, $name)
{
$this->id = $id;
$this->name = $name;
}

public function getId()
{
return $this->id;
}

public function getName()
{
return $this->name;
}
}
55 changes: 55 additions & 0 deletions tests/Fixtures/AuthorDeprecatedReadOnlyPerClass.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

declare(strict_types=1);

namespace JMS\Serializer\Tests\Fixtures;

use JMS\Serializer\Annotation\Accessor;
use JMS\Serializer\Annotation\ReadOnly;
use JMS\Serializer\Annotation\SerializedName;
use JMS\Serializer\Annotation\Type;
use JMS\Serializer\Annotation\XmlRoot;

/**
* @deprecated ReadOnly annotation is deprecated
*
* @XmlRoot("author")
* @ReadOnly
*/
#[XmlRoot(name: 'author')]
class AuthorDeprecatedReadOnlyPerClass
{
/**
* @ReadOnly
* @SerializedName("id")
*/
#[SerializedName(name: 'id')]
private $id;

public function __construct($id, $name)
{
$this->id = $id;
$this->name = $name;
}

/**
* @Type("string")
* @SerializedName("full_name")
* @Accessor("getName")
* @ReadOnly(false)
*/
#[Type(name: 'string')]
#[SerializedName(name: 'full_name')]
#[Accessor(getter: 'getName')]
private $name;

public function getId()
{
return $this->id;
}

public function getName()
{
return $this->name;
}
}
26 changes: 26 additions & 0 deletions tests/Serializer/BaseSerializationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@
use JMS\Serializer\Tests\Fixtures\AccessorOrderMethod;
use JMS\Serializer\Tests\Fixtures\AccessorOrderParent;
use JMS\Serializer\Tests\Fixtures\Author;
use JMS\Serializer\Tests\Fixtures\AuthorDeprecatedReadOnly;
use JMS\Serializer\Tests\Fixtures\AuthorDeprecatedReadOnlyPerClass;
use JMS\Serializer\Tests\Fixtures\AuthorExpressionAccess;
use JMS\Serializer\Tests\Fixtures\AuthorExpressionAccessContext;
use JMS\Serializer\Tests\Fixtures\AuthorList;
Expand Down Expand Up @@ -967,6 +969,18 @@ public function testReadOnly()
}
}

public function testDeprecatedReadOnly()
{
$author = new AuthorDeprecatedReadOnly(123, 'Ruud Kamphuis');
self::assertEquals($this->getContent('readonly'), $this->serialize($author));

if ($this->hasDeserializer()) {
$deserialized = $this->deserialize($this->getContent('readonly'), get_class($author));
self::assertNull($this->getField($deserialized, 'id'));
self::assertEquals('Ruud Kamphuis', $this->getField($deserialized, 'name'));
}
}

public function testReadOnlyClass()
{
$author = new AuthorReadOnlyPerClass(123, 'Ruud Kamphuis');
Expand All @@ -979,6 +993,18 @@ public function testReadOnlyClass()
}
}

public function testDeprecatedReadOnlyClass()
{
$author = new AuthorDeprecatedReadOnlyPerClass(123, 'Ruud Kamphuis');
self::assertEquals($this->getContent('readonly'), $this->serialize($author));

if ($this->hasDeserializer()) {
$deserialized = $this->deserialize($this->getContent('readonly'), get_class($author));
self::assertNull($this->getField($deserialized, 'id'));
self::assertEquals('Ruud Kamphuis', $this->getField($deserialized, 'name'));
}
}

public function testPrice()
{
$price = new Price(3);
Expand Down

0 comments on commit 0207dc2

Please sign in to comment.