Explorar el Código

A property can be marked as non-read only

Asmir Mustafic hace 11 años
padre
commit
8866dc2ed7

+ 2 - 0
doc/reference/annotations.rst

@@ -191,6 +191,8 @@ to determine the order.
 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).
+
 @PreSerialize
 ~~~~~~~~~~~~~
 This annotation can be defined on a method which is supposed to be called before

+ 4 - 0
src/JMS/Serializer/Annotation/ReadOnly.php

@@ -24,4 +24,8 @@ namespace JMS\Serializer\Annotation;
  */
 final class ReadOnly
 {
+    /**
+     * @var boolean
+     */
+    public $readOnly = true;
 }

+ 2 - 2
src/JMS/Serializer/Metadata/Driver/AnnotationDriver.php

@@ -140,9 +140,9 @@ class AnnotationDriver implements DriverInterface
             }
 
             foreach ($propertiesMetadata as $propertyKey => $propertyMetadata) {
-                $propertyMetadata->readOnly = $propertyMetadata->readOnly || $readOnlyClass;
                 $isExclude = false;
                 $isExpose = $propertyMetadata instanceof VirtualPropertyMetadata;
+                $propertyMetadata->readOnly = $propertyMetadata->readOnly || $readOnlyClass;
                 $accessType = $classAccessType;
                 $accessor = array(null, null);
 
@@ -187,7 +187,7 @@ class AnnotationDriver implements DriverInterface
                     } elseif ($annot instanceof AccessType) {
                         $accessType = $annot->type;
                     } elseif ($annot instanceof ReadOnly) {
-                       $propertyMetadata->readOnly = true;
+                       $propertyMetadata->readOnly = $annot->readOnly;
                     } elseif ($annot instanceof Accessor) {
                         $accessor = array($annot->getter, $annot->setter);
                     } elseif ($annot instanceof Groups) {

+ 3 - 1
src/JMS/Serializer/Metadata/Driver/XmlDriver.php

@@ -220,7 +220,9 @@ class XmlDriver extends AbstractFileDriver
 
                     //we need read-only before setter and getter set, because that method depends on flag being set
                     if (null !== $readOnly = $pElem->attributes()->{'read-only'}) {
-                        $pMetadata->readOnly = $readOnlyClass || 'true' === strtolower($readOnly);
+                        $pMetadata->readOnly = 'true' === strtolower($readOnly);
+                    } else {
+                        $pMetadata->readOnly = $pMetadata->readOnly || $readOnlyClass;
                     }
 
                     $getter = $pElem->attributes()->{'accessor-getter'};

+ 3 - 1
src/JMS/Serializer/Metadata/Driver/YamlDriver.php

@@ -167,7 +167,9 @@ class YamlDriver extends AbstractFileDriver
 
                     //we need read_only before setter and getter set, because that method depends on flag being set
                     if (isset($pConfig['read_only'])) {
-                          $pMetadata->readOnly = $readOnlyClass || (Boolean) $pConfig['read_only'];
+                          $pMetadata->readOnly = (Boolean)$pConfig['read_only'];
+                    } else {
+                        $pMetadata->readOnly = $pMetadata->readOnly || $readOnlyClass;
                     }
 
                     $pMetadata->setAccessor(

+ 1 - 0
tests/JMS/Serializer/Tests/Fixtures/AuthorReadOnlyPerClass.php

@@ -40,6 +40,7 @@ class AuthorReadOnlyPerClass
      * @Type("string")
      * @SerializedName("full_name")
      * @Accessor("getName")
+     * @ReadOnly(false)
      */
     private $name;
 

+ 1 - 1
tests/JMS/Serializer/Tests/Metadata/Driver/xml/AuthorReadOnlyPerClass.xml

@@ -2,6 +2,6 @@
 <serializer>
     <class name="JMS\Serializer\Tests\Fixtures\AuthorReadOnlyPerClass" xml-root-name="author" read-only="true">
         <property name="id" read-only="true"/>
-        <property name="name" serialized-name="full_name" access-type="public_method" accessor-getter="getName" />
+        <property name="name" serialized-name="full_name" access-type="public_method" accessor-getter="getName" read-only="false" />
     </class>
 </serializer>

+ 1 - 1
tests/JMS/Serializer/Tests/Metadata/Driver/yml/AuthorReadOnlyPerClass.yml

@@ -8,4 +8,4 @@ JMS\Serializer\Tests\Fixtures\AuthorReadOnlyPerClass:
             serialized_name:  full_name
             access_type:      public_method
             accessor_getter:  getName
-            read_only:        true
+            read_only:        false

+ 1 - 1
tests/JMS/Serializer/Tests/Serializer/BaseSerializationTest.php

@@ -417,7 +417,7 @@ abstract class BaseSerializationTest extends \PHPUnit_Framework_TestCase
         if ($this->hasDeserializer()) {
             $deserialized = $this->deserialize($this->getContent('readonly'), get_class($author));
             $this->assertNull($this->getField($deserialized, 'id'));
-            $this->assertNull($this->getField($deserialized, 'name'));
+            $this->assertEquals('Ruud Kamphuis', $this->getField($deserialized, 'name'));
         }
     }