Skip to content

Commit d91dff7

Browse files
committed
Fix an Exception when an excluded property has no method accessors
Exception is thrown, if the class accessor is set to public_method, and there is an excluded property with no accessor
1 parent 65a86bb commit d91dff7

7 files changed

Lines changed: 90 additions & 0 deletions

File tree

src/JMS/Serializer/Metadata/Driver/XmlDriver.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,10 @@ protected function loadMetadataFromFile(\ReflectionClass $class, $path)
160160
$isExclude = 'true' === strtolower($exclude);
161161
}
162162

163+
if ($isExclude) {
164+
continue;
165+
}
166+
163167
if (null !== $expose = $pElem->attributes()->expose) {
164168
$isExpose = 'true' === strtolower($expose);
165169
}

src/JMS/Serializer/Metadata/Driver/YamlDriver.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,10 @@ protected function loadMetadataFromFile(\ReflectionClass $class, $file)
9191
$isExclude = (Boolean)$pConfig['exclude'];
9292
}
9393

94+
if ($isExclude) {
95+
continue;
96+
}
97+
9498
if (isset($pConfig['expose'])) {
9599
$isExpose = (Boolean)$pConfig['expose'];
96100
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php
2+
3+
/*
4+
* Copyright 2016 Johannes M. Schmitt <schmittjoh@gmail.com>
5+
*
6+
* Licensed under the Apache License, Version 2.0 (the "License");
7+
* you may not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
19+
namespace JMS\Serializer\Tests\Fixtures;
20+
21+
use JMS\Serializer\Annotation\AccessType;
22+
use JMS\Serializer\Annotation\Exclude;
23+
use JMS\Serializer\Annotation\ReadOnly;
24+
25+
/**
26+
*/
27+
28+
/**
29+
* @AccessType("public_method")
30+
* @ReadOnly
31+
*/
32+
class ExcludePublicAccessor
33+
{
34+
/**
35+
* @Exclude
36+
*
37+
* @var mixed
38+
*/
39+
private $iShallNotBeAccessed;
40+
41+
/**
42+
* @var int
43+
*/
44+
private $id = 1;
45+
46+
public function getId()
47+
{
48+
return $this->id;
49+
}
50+
}

tests/Metadata/Driver/BaseDriverTest.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -477,6 +477,17 @@ public function testExclusionIf()
477477
$this->assertEquals($p, $m->propertyMetadata['age']);
478478
}
479479

480+
public function testExcludePropertyNoPublicAccessorException()
481+
{
482+
$first = $this->getDriver()->loadMetadataForClass(new \ReflectionClass('JMS\Serializer\Tests\Fixtures\ExcludePublicAccessor'));
483+
484+
if ($this instanceof PhpDriverTest) {
485+
return;
486+
}
487+
$this->assertArrayHasKey('id', $first->propertyMetadata);
488+
$this->assertArrayNotHasKey('iShallNotBeAccessed', $first->propertyMetadata);
489+
}
490+
480491

481492
/**
482493
* @return DriverInterface

tests/Metadata/Driver/DoctrineDriverTest.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,15 @@ public function testNonDoctrineEntityClassIsNotModified()
100100
$this->assertEquals($plainMetadata, $doctrineMetadata);
101101
}
102102

103+
public function testExcludePropertyNoPublicAccessorException()
104+
{
105+
$first = $this->getAnnotationDriver()
106+
->loadMetadataForClass(new \ReflectionClass('JMS\Serializer\Tests\Fixtures\ExcludePublicAccessor'));
107+
108+
$this->assertArrayHasKey('id', $first->propertyMetadata);
109+
$this->assertArrayNotHasKey('iShallNotBeAccessed', $first->propertyMetadata);
110+
}
111+
103112
public function testVirtualPropertiesAreNotModified()
104113
{
105114
$doctrineMetadata = $this->getMetadata();
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<serializer>
3+
<class name="JMS\Serializer\Tests\Fixtures\ExcludePublicAccessor" access-type="public_method" read-only="true">
4+
<property name="iShallNotBeAccessed" exclude="true" />
5+
</class>
6+
</serializer>
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
JMS\Serializer\Tests\Fixtures\ExcludePublicAccessor:
2+
access_type: public_method
3+
read_only: false
4+
properties:
5+
iShallNotBeAccessed:
6+
exclude: true

0 commit comments

Comments
 (0)