Kaynağa Gözat

Merge branch 'readonlyOrderFix' of git://github.com/mvrhov/JMSSerializerBundle

Johannes Schmitt 12 yıl önce
ebeveyn
işleme
ef9a003b56

+ 3 - 2
Metadata/Driver/AnnotationDriver.php

@@ -159,14 +159,15 @@ class AnnotationDriver implements DriverInterface
                         $propertyMetadata->xmlValue = true;
                     } else if ($annot instanceof AccessType) {
                         $AccessType = $annot->type;
+                    //we need ReadOnly before setter and getter set, because that method depends on flag being set
+                    } else if ($annot instanceof ReadOnly) {
+                       $propertyMetadata->readOnly = true;
                     } else if ($annot instanceof Accessor) {
                         $accessor = array($annot->getter, $annot->setter);
                     } else if ($annot instanceof Groups) {
                         $propertyMetadata->groups = $annot->groups;
                     } else if ($annot instanceof Inline) {
                         $propertyMetadata->inline = true;
-                    } else if ($annot instanceof ReadOnly) {
-                        $propertyMetadata->readOnly = true;
                     }
                 }
 

+ 5 - 3
Metadata/Driver/XmlDriver.php

@@ -166,6 +166,11 @@ class XmlDriver extends AbstractFileDriver
                         $pMetadata->xmlKeyValuePairs = 'true' === (string) $pElem->attributes()->{'xml-key-value-pairs'};
                     }
 
+                    //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 = 'true' === strtolower($readOnly);
+                    }
+
                     $getter = $pElem->attributes()->{'accessor-getter'};
                     $setter = $pElem->attributes()->{'accessor-setter'};
                     $pMetadata->setAccessor(
@@ -178,9 +183,6 @@ class XmlDriver extends AbstractFileDriver
                         $pMetadata->inline = 'true' === strtolower($inline);
                     }
 
-                    if (null !== $readOnly = $pElem->attributes()->{'read-only'}) {
-                        $pMetadata->readOnly = 'true' === strtolower($readOnly);
-                    }
                 }
 
                 if ((ExclusionPolicy::NONE === (string)$exclusionPolicy && !$isExclude)

+ 5 - 3
Metadata/Driver/YamlDriver.php

@@ -156,6 +156,11 @@ class YamlDriver extends AbstractFileDriver
                         $pMetadata->xmlKeyValuePairs = (Boolean) $pConfig['xml_key_value_pairs'];
                     }
 
+                    //we need read_only before setter and getter set, because that method depends on flag being set
+                    if (isset($pConfig['read_only'])) {
+                          $pMetadata->readOnly = (Boolean) $pConfig['read_only'];
+                    }
+
                     $pMetadata->setAccessor(
                         isset($pConfig['access_type']) ? $pConfig['access_type'] : $classAccessType,
                         isset($pConfig['accessor']) ? $pConfig['accessor'] : null
@@ -165,9 +170,6 @@ class YamlDriver extends AbstractFileDriver
                         $pMetadata->inline = (Boolean) $pConfig['inline'];
                     }
 
-                    if (isset($pConfig['read_only'])) {
-                        $pMetadata->readOnly = (Boolean) $pConfig['read_only'];
-                    }
                 }
                 if ((ExclusionPolicy::NONE === $exclusionPolicy && !$isExclude)
                 || (ExclusionPolicy::ALL === $exclusionPolicy && $isExpose)) {

+ 2 - 0
Tests/Fixtures/AuthorReadOnly.php

@@ -22,6 +22,7 @@ use JMS\SerializerBundle\Annotation\SerializedName;
 use JMS\SerializerBundle\Annotation\Type;
 use JMS\SerializerBundle\Annotation\XmlRoot;
 use JMS\SerializerBundle\Annotation\ReadOnly;
+use JMS\SerializerBundle\Annotation\Accessor;
 
 /** @XmlRoot("author") */
 class AuthorReadOnly
@@ -35,6 +36,7 @@ class AuthorReadOnly
     /**
      * @Type("string")
      * @SerializedName("full_name")
+     * @Accessor("getName")
      */
     private $name;
 

+ 7 - 0
Tests/Metadata/Driver/BaseDriverTest.php

@@ -107,5 +107,12 @@ abstract class BaseDriverTest extends \PHPUnit_Framework_TestCase
         $this->assertEquals($p, $m->propertyMetadata['virtualValue']);
     }
 
+    public function testReadOnlyDefinedBeforeGetterAndSetter()
+    {
+        $m = $this->getDriver()->loadMetadataForClass(new \ReflectionClass('JMS\SerializerBundle\Tests\Fixtures\AuthorReadOnly'));
+
+        $this->assertNotNull($m);
+    }
+
     abstract protected function getDriver();
 }

+ 8 - 0
Tests/Metadata/Driver/php/AuthorReadOnly.php

@@ -0,0 +1,8 @@
+<?php
+
+use JMS\SerializerBundle\Metadata\ClassMetadata;
+use JMS\SerializerBundle\Metadata\PropertyMetadata;
+
+$metadata = new ClassMetadata('JMS\SerializerBundle\Tests\Fixtures\AuthorReadOnly');
+
+return $metadata;

+ 7 - 0
Tests/Metadata/Driver/xml/AuthorReadOnly.xml

@@ -0,0 +1,7 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<serializer>
+    <class name="JMS\SerializerBundle\Tests\Fixtures\AuthorReadOnly" xml-root-name="author">
+        <property name="id" read-only="true"/>
+        <property name="name" serialized-name="full_name" access-type="public_method" accessor-getter="getName" read-only="true"/>
+    </class>
+</serializer>

+ 10 - 0
Tests/Metadata/Driver/yml/AuthorReadOnly.yml

@@ -0,0 +1,10 @@
+JMS\SerializerBundle\Tests\Fixtures\AuthorReadOnly:
+    xml_root_name: author
+    properties:
+        id:
+            read_only: true
+        name:
+            serialized_name:  full_name
+            access_type:      public_method
+            accessor_getter:  getName
+            read_only:        true