Skip to content

Commit

Permalink
Merge pull request #1526 from boesing/bugfix/missing-attributes
Browse files Browse the repository at this point in the history
Allow missing attributes in serialized objects
  • Loading branch information
scyzoryck authored Dec 9, 2023
2 parents 83ede54 + b69fe07 commit c586bf6
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 6 deletions.
3 changes: 3 additions & 0 deletions phpstan/ignore-by-php-version.neon.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
if (PHP_VERSION_ID < 80100) {
$includes[] = __DIR__ . '/no-enum.neon';
}
if (PHP_VERSION_ID >= 80000) {
$includes[] = __DIR__ . '/ignore-missing-attribute.neon';
}
if (PHP_VERSION_ID >= 80100 && PHP_VERSION_ID < 80200) {
$includes[] = __DIR__ . '/php-81.neon';
}
Expand Down
3 changes: 3 additions & 0 deletions phpstan/ignore-missing-attribute.neon
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
parameters:
ignoreErrors:
- '#^Attribute class JMS\\Serializer\\Tests\\Fixtures\\MissingAttribute does not exist\.$#'
23 changes: 20 additions & 3 deletions src/Metadata/Driver/AnnotationOrAttributeDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@
use Metadata\Driver\DriverInterface;
use Metadata\MethodMetadata;

use function array_filter;

class AnnotationOrAttributeDriver implements DriverInterface
{
use ExpressionMetadataTrait;
Expand Down Expand Up @@ -309,7 +311,12 @@ protected function getClassAnnotations(\ReflectionClass $class): array
static function (\ReflectionAttribute $attribute): object {
return $attribute->newInstance();
},
$class->getAttributes()
array_filter(
$class->getAttributes(),
static function (\ReflectionAttribute $attribute): bool {
return class_exists($attribute->getName());
}
)
);
}

Expand All @@ -332,7 +339,12 @@ protected function getMethodAnnotations(\ReflectionMethod $method): array
static function (\ReflectionAttribute $attribute): object {
return $attribute->newInstance();
},
$method->getAttributes()
array_filter(
$method->getAttributes(),
static function (\ReflectionAttribute $attribute): bool {
return class_exists($attribute->getName());
}
)
);
}

Expand All @@ -355,7 +367,12 @@ protected function getPropertyAnnotations(\ReflectionProperty $property): array
static function (\ReflectionAttribute $attribute): object {
return $attribute->newInstance();
},
$property->getAttributes()
array_filter(
$property->getAttributes(),
static function (\ReflectionAttribute $attribute): bool {
return class_exists($attribute->getName());
}
)
);
}

Expand Down
23 changes: 20 additions & 3 deletions src/Metadata/Driver/AttributeDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

namespace JMS\Serializer\Metadata\Driver;

use function array_filter;

class AttributeDriver extends AnnotationOrAttributeDriver
{
/**
Expand All @@ -15,7 +17,12 @@ protected function getClassAnnotations(\ReflectionClass $class): array
static function (\ReflectionAttribute $attribute): object {
return $attribute->newInstance();
},
$class->getAttributes()
array_filter(
$class->getAttributes(),
static function (\ReflectionAttribute $attribute): bool {
return class_exists($attribute->getName());
}
)
);
}

Expand All @@ -28,7 +35,12 @@ protected function getMethodAnnotations(\ReflectionMethod $method): array
static function (\ReflectionAttribute $attribute): object {
return $attribute->newInstance();
},
$method->getAttributes()
array_filter(
$method->getAttributes(),
static function (\ReflectionAttribute $attribute): bool {
return class_exists($attribute->getName());
}
)
);
}

Expand All @@ -41,7 +53,12 @@ protected function getPropertyAnnotations(\ReflectionProperty $property): array
static function (\ReflectionAttribute $attribute): object {
return $attribute->newInstance();
},
$property->getAttributes()
array_filter(
$property->getAttributes(),
static function (\ReflectionAttribute $attribute): bool {
return class_exists($attribute->getName());
}
)
);
}
}
24 changes: 24 additions & 0 deletions tests/Fixtures/MissingAttributeObject.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

declare(strict_types=1);

namespace JMS\Serializer\Tests\Fixtures;

use JMS\Serializer\Annotation\VirtualProperty;

#[MissingAttribute]
final class MissingAttributeObject
{
#[MissingAttribute]
public $property;

/**
* @VirtualProperty(name="propertyFromMethod")
*/
#[MissingAttribute]
#[VirtualProperty(name: 'propertyFromMethod')]
public function propertyMethod()
{
return '';
}
}
14 changes: 14 additions & 0 deletions tests/Metadata/Driver/BaseAnnotationOrAttributeDriverTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
namespace JMS\Serializer\Tests\Metadata\Driver;

use JMS\Serializer\Tests\Fixtures\AllExcludedObject;
use JMS\Serializer\Tests\Fixtures\MissingAttributeObject;
use ReflectionClass;

use const PHP_VERSION_ID;

abstract class BaseAnnotationOrAttributeDriverTestCase extends BaseDriverTestCase
{
Expand All @@ -26,4 +30,14 @@ public function testShortExposeSyntax(): void
{
$this->markTestSkipped('Short expose syntax not supported on annotations or attribute');
}

public function testCanHandleMissingAttributes(): void
{
$metadata = $this->getDriver()->loadMetadataForClass(new ReflectionClass(MissingAttributeObject::class));
self::assertArrayHasKey('property', $metadata->propertyMetadata);

if (PHP_VERSION_ID >= 80000) {
self::assertArrayHasKey('propertyFromMethod', $metadata->propertyMetadata);
}
}
}

0 comments on commit c586bf6

Please sign in to comment.