Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Deprecate @ReadOnly annotation in favor of @ReadOnlyProperty #1333

Merged
merged 1 commit into from
Aug 1, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions UPGRADING.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ Unreleased
==========

- Use symfony/cache for FileSystem cache implementation instead of doctrine/cache
- Deprecated the `@ReadOnly` annotation due to `readonly` becoming a keyword in PHP 8.1, use the `@ReadOnlyProperty` annotation instead

From 2.x to 3.0.0
=================
Expand Down
11 changes: 6 additions & 5 deletions doc/reference/annotations.rst
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ be called to retrieve, or set the value of the given property:
.. note ::

If you need only to serialize your data, you can avoid providing a setter by
setting the property as read-only using the ``@ReadOnly`` annotation.
setting the property as read-only using the ``@ReadOnlyProperty`` annotation.

@AccessorOrder
~~~~~~~~~~~~~~
Expand Down Expand Up @@ -273,12 +273,13 @@ should be inlined.

**Note**: AccessorOrder will be using the name of the property to determine the order.

@ReadOnly
~~~~~~~~~
@ReadOnlyProperty
~~~~~~~~~~~~~~~~~
This annotation can be defined on a property to indicate that the data of the property
is read only and cannot be set during deserialization.

A property can be marked as non read only with ``@ReadOnly(false)`` annotation (useful when a class is marked as read only).
A property can be marked as non read only with ``@ReadOnlyProperty(false)`` annotation
(useful when a class is marked as read only).

@PreSerialize
~~~~~~~~~~~~~
Expand Down Expand Up @@ -444,7 +445,7 @@ Available Types:

(*) If the standalone jms/serializer is used then default format is `\DateTime::ISO8601` (which is not compatible with ISO-8601 despite the name). For jms/serializer-bundle the default format is `\DateTime::ATOM` (the real ISO-8601 format) but it can be changed in `configuration`_.

(**) The key type K for array-linke formats as ``array``. ``ArrayCollection``, ``iterable``, etc., is only used for deserialization,
(**) The key type K for array-linke formats as ``array``. ``ArrayCollection``, ``iterable``, etc., is only used for deserialization,
for serializaiton is treated as ``string``.

Examples:
Expand Down
8 changes: 3 additions & 5 deletions src/Annotation/ReadOnly.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,9 @@
/**
* @Annotation
* @Target({"CLASS","PROPERTY"})
*
* @deprecated use `@ReadOnlyProperty` instead
*/
final class ReadOnly
final class ReadOnly extends ReadOnlyProperty
{
/**
* @var bool
*/
public $readOnly = true;
}
19 changes: 19 additions & 0 deletions src/Annotation/ReadOnlyProperty.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

declare(strict_types=1);

namespace JMS\Serializer\Annotation;

/**
* @Annotation
* @Target({"CLASS","PROPERTY"})
*
* @final
*/
/* final */ class ReadOnlyProperty
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is purposefully left as not final and only annotated @final to help with B/C and code complexity. Otherwise, there'd have to be a lot of PHP version conditionals and checks for both annotation classes. In 4.0, when the deprecated @ReadOnly annotation is removed the final should be uncommented.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

makes sense! i'm ok with it

{
/**
* @var bool
*/
public $readOnly = true;
}
6 changes: 3 additions & 3 deletions src/Metadata/Driver/AnnotationDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
use JMS\Serializer\Annotation\PostDeserialize;
use JMS\Serializer\Annotation\PostSerialize;
use JMS\Serializer\Annotation\PreSerialize;
use JMS\Serializer\Annotation\ReadOnly;
use JMS\Serializer\Annotation\ReadOnlyProperty;
use JMS\Serializer\Annotation\SerializedName;
use JMS\Serializer\Annotation\Since;
use JMS\Serializer\Annotation\SkipWhenEmpty;
Expand Down Expand Up @@ -106,7 +106,7 @@ public function loadMetadataForClass(\ReflectionClass $class): ?BaseClassMetadat
}
} elseif ($annot instanceof AccessType) {
$classAccessType = $annot->type;
} elseif ($annot instanceof ReadOnly) {
} elseif ($annot instanceof ReadOnlyProperty) {
$readOnlyClass = true;
} elseif ($annot instanceof AccessorOrder) {
$classMetadata->setAccessorOrder($annot->order, $annot->custom);
Expand Down Expand Up @@ -225,7 +225,7 @@ public function loadMetadataForClass(\ReflectionClass $class): ?BaseClassMetadat
$propertyMetadata->xmlElementCData = $annot->cdata;
} elseif ($annot instanceof AccessType) {
$accessType = $annot->type;
} elseif ($annot instanceof ReadOnly) {
} elseif ($annot instanceof ReadOnlyProperty) {
$propertyMetadata->readOnly = $annot->readOnly;
} elseif ($annot instanceof Accessor) {
$accessor = [$annot->getter, $annot->setter];
Expand Down
4 changes: 2 additions & 2 deletions tests/Fixtures/AuthorReadOnly.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
namespace JMS\Serializer\Tests\Fixtures;

use JMS\Serializer\Annotation\Accessor;
use JMS\Serializer\Annotation\ReadOnly;
use JMS\Serializer\Annotation\ReadOnlyProperty;
use JMS\Serializer\Annotation\SerializedName;
use JMS\Serializer\Annotation\Type;
use JMS\Serializer\Annotation\XmlRoot;
Expand All @@ -14,7 +14,7 @@
class AuthorReadOnly
{
/**
* @ReadOnly
* @ReadOnlyProperty
* @SerializedName("id")
*/
private $id;
Expand Down
8 changes: 4 additions & 4 deletions tests/Fixtures/AuthorReadOnlyPerClass.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,19 @@
namespace JMS\Serializer\Tests\Fixtures;

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

/**
* @XmlRoot("author")
* @ReadOnly
* @ReadOnlyProperty
*/
class AuthorReadOnlyPerClass
{
/**
* @ReadOnly
* @ReadOnlyProperty
* @SerializedName("id")
*/
private $id;
Expand All @@ -26,7 +26,7 @@ class AuthorReadOnlyPerClass
* @Type("string")
* @SerializedName("full_name")
* @Accessor("getName")
* @ReadOnly(false)
* @ReadOnlyProperty(false)
*/
private $name;

Expand Down
4 changes: 2 additions & 2 deletions tests/Fixtures/ExcludePublicAccessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@

use JMS\Serializer\Annotation\AccessType;
use JMS\Serializer\Annotation\Exclude;
use JMS\Serializer\Annotation\ReadOnly;
use JMS\Serializer\Annotation\ReadOnlyProperty;

/**
* @AccessType("public_method")
* @ReadOnly
* @ReadOnlyProperty
*/
class ExcludePublicAccessor
{
Expand Down
4 changes: 2 additions & 2 deletions tests/Fixtures/GetSetObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

use JMS\Serializer\Annotation\AccessType;
use JMS\Serializer\Annotation\Exclude;
use JMS\Serializer\Annotation\ReadOnly;
use JMS\Serializer\Annotation\ReadOnlyProperty;
use JMS\Serializer\Annotation\Type;

/** @AccessType("public_method") */
Expand All @@ -19,7 +19,7 @@ class GetSetObject
private $name = 'Foo';

/**
* @ReadOnly
* @ReadOnlyProperty
*/
private $readOnlyProperty = 42;

Expand Down
4 changes: 2 additions & 2 deletions tests/Fixtures/TypedProperties/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,11 @@ class User
public \DateTime $created;

/**
* @Serializer\ReadOnly()
* @Serializer\ReadOnlyProperty()
*/
public ?\DateTimeInterface $updated = null;
/**
* @Serializer\ReadOnly()
* @Serializer\ReadOnlyProperty()
*/
public iterable $tags = [];
}