Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow missing attributes in serialized objects #1526

Merged
merged 5 commits into from
Dec 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -289,7 +291,7 @@
}
}

if (!$configured) {

Check failure on line 294 in src/Metadata/Driver/AnnotationOrAttributeDriver.php

View workflow job for this annotation

GitHub Actions / Coding Standards (7.2)

Empty IF statement detected
// return null;
// uncomment the above line afetr a couple of months
}
Expand All @@ -309,7 +311,12 @@
static function (\ReflectionAttribute $attribute): object {
return $attribute->newInstance();
},
$class->getAttributes()
array_filter(
$class->getAttributes(),
static function (\ReflectionAttribute $attribute): bool {
return class_exists($attribute->getName());
}
)
boesing marked this conversation as resolved.
Show resolved Hide resolved
);
}

Expand All @@ -332,7 +339,12 @@
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 @@
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);
}
}
}
Loading