فهرست منبع

Merge branch 'patch-2' of https://github.com/kertz/JMSSerializerBundle

Johannes M. Schmitt 13 سال پیش
والد
کامیت
a7984ee760

+ 2 - 2
Metadata/PropertyMetadata.php

@@ -56,7 +56,7 @@ class PropertyMetadata extends BasePropertyMetadata
                 }
             }
 
-            if (empty($setter)) {
+            if (empty($setter) && !$this->readOnly) {
                 if ($class->hasMethod('set'.$this->name) && $class->getMethod('set'.$this->name)->isPublic()) {
                     $setter = 'set'.$this->name;
                 } else {
@@ -114,4 +114,4 @@ class PropertyMetadata extends BasePropertyMetadata
 
         parent::unserialize($parentStr);
     }
-}
+}

+ 12 - 1
Tests/Fixtures/GetSetObject.php

@@ -4,6 +4,7 @@ namespace JMS\SerializerBundle\Tests\Fixtures;
 
 use JMS\SerializerBundle\Annotation\AccessType;
 use JMS\SerializerBundle\Annotation\Type;
+use JMS\SerializerBundle\Annotation\ReadOnly;
 
 /** @AccessType("public_method") */
 class GetSetObject
@@ -14,6 +15,11 @@ class GetSetObject
     /** @Type("string") */
     private $name = 'Foo';
 
+    /**
+     * @ReadOnly
+     */
+    private $readOnlyProperty = 42;
+
     public function getId()
     {
         throw new \RuntimeException('This should not be called.');
@@ -28,4 +34,9 @@ class GetSetObject
     {
         $this->name = $name;
     }
-}
+
+    public function getReadOnlyProperty()
+    {
+        return $this->readOnlyProperty;
+    }
+}

+ 2 - 1
Tests/Serializer/BaseSerializationTest.php

@@ -343,7 +343,7 @@ abstract class BaseSerializationTest extends \PHPUnit_Framework_TestCase
 
     public function testNestedFormErrors()
     {
-        $dispatcher = $this->getMock('Symfony\Component\EventDispatcher\EventDispatcherInterface');
+        $dispatcher = $this->getMock('Symfony\Component\EventDispatcher\EventDispatcherInterface');
         $formConfig = $this->getMock('Symfony\Component\Form\FormConfigInterface');
         $formConfig->expects($this->any())
             ->method('getEventDispatcher')
@@ -426,6 +426,7 @@ abstract class BaseSerializationTest extends \PHPUnit_Framework_TestCase
             $object = $this->deserialize($this->getContent('mixed_access_types'), 'JMS\SerializerBundle\Tests\Fixtures\GetSetObject');
             $this->assertAttributeEquals(1, 'id', $object);
             $this->assertAttributeEquals('Johannes', 'name', $object);
+            $this->assertAttributeEquals(42, 'readOnlyProperty', $object);
         }
     }
 

+ 1 - 1
Tests/Serializer/JsonSerializationTest.php

@@ -57,7 +57,7 @@ class JsonSerializationTest extends BaseSerializationTest
             $outputs['article'] = '{"custom":"serialized"}';
             $outputs['orm_proxy'] = '{"foo":"foo","moo":"bar","camel_case":"proxy-boo"}';
             $outputs['custom_accessor'] = '{"comments":{"Foo":{"comments":[{"author":{"full_name":"Foo"},"text":"foo"},{"author":{"full_name":"Foo"},"text":"bar"}],"count":2}}}';
-            $outputs['mixed_access_types'] = '{"id":1,"name":"Johannes"}';
+            $outputs['mixed_access_types'] = '{"id":1,"name":"Johannes","read_only_property":42}';
             $outputs['accessor_order_child'] = '{"c":"c","d":"d","a":"a","b":"b"}';
             $outputs['accessor_order_parent'] = '{"a":"a","b":"b"}';
             $outputs['inline'] = '{"c":"c","a":"a","b":"b","d":"d"}';

+ 1 - 0
Tests/Serializer/xml/mixed_access_types.xml

@@ -2,4 +2,5 @@
 <result>
   <id>1</id>
   <name><![CDATA[Johannes]]></name>
+  <read_only_property>42</read_only_property>
 </result>

+ 1 - 0
Tests/Serializer/yml/mixed_access_types.yml

@@ -1,2 +1,3 @@
 id: 1
 name: Johannes
+read_only_property: 42