Skip to content

Commit

Permalink
Don't return a metadata object unless there is explicit annotation or…
Browse files Browse the repository at this point in the history
… attribute configuration, return null from decorating drivers if the driver they decorate returns null
  • Loading branch information
mbabker committed Jun 12, 2023
1 parent 01e67df commit 874250b
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 3 deletions.
5 changes: 5 additions & 0 deletions src/Metadata/Driver/AbstractDoctrineTypeDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,11 @@ public function __construct(DriverInterface $delegate, ManagerRegistry $registry
public function loadMetadataForClass(\ReflectionClass $class): ?BaseClassMetadata
{
$classMetadata = $this->delegate->loadMetadataForClass($class);

if (null === $classMetadata) {
return null;
}

\assert($classMetadata instanceof ClassMetadata);

// Abort if the given class is not a mapped entity
Expand Down
13 changes: 13 additions & 0 deletions src/Metadata/Driver/AnnotationOrAttributeDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,9 @@ public function __construct(PropertyNamingStrategyInterface $namingStrategy, ?Pa

public function loadMetadataForClass(\ReflectionClass $class): ?BaseClassMetadata
{
// For metadata to merge correctly, we must treat the object as configured if it has a parent class
$configured = $class->getParentClass() !== false;

$classMetadata = new ClassMetadata($name = $class->name);
$fileResource = $class->getFilename();

Expand All @@ -86,6 +89,8 @@ public function loadMetadataForClass(\ReflectionClass $class): ?BaseClassMetadat
$readOnlyClass = false;

foreach ($this->getClassAnnotations($class) as $annot) {
$configured = true;

if ($annot instanceof ExclusionPolicy) {
$exclusionPolicy = $annot->policy;
} elseif ($annot instanceof XmlRoot) {
Expand Down Expand Up @@ -135,6 +140,8 @@ public function loadMetadataForClass(\ReflectionClass $class): ?BaseClassMetadat
$methodAnnotations = $this->getMethodAnnotations($method);

foreach ($methodAnnotations as $annot) {
$configured = true;

if ($annot instanceof PreSerialize) {
$classMetadata->addPreSerializeMethod(new MethodMetadata($name, $method->name));
continue 2;
Expand Down Expand Up @@ -174,6 +181,8 @@ public function loadMetadataForClass(\ReflectionClass $class): ?BaseClassMetadat
$propertyAnnotations = $propertiesAnnotations[$propertyKey];

foreach ($propertyAnnotations as $annot) {
$configured = true;

if ($annot instanceof Since) {
$propertyMetadata->sinceVersion = $annot->version;
} elseif ($annot instanceof Until) {
Expand Down Expand Up @@ -274,6 +283,10 @@ public function loadMetadataForClass(\ReflectionClass $class): ?BaseClassMetadat
}
}

if (!$configured) {
return null;
}

return $classMetadata;
}

Expand Down
3 changes: 2 additions & 1 deletion src/Metadata/Driver/DefaultValuePropertyDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,13 @@ public function __construct(DriverInterface $delegate)
public function loadMetadataForClass(ReflectionClass $class): ?ClassMetadata
{
$classMetadata = $this->delegate->loadMetadataForClass($class);
\assert($classMetadata instanceof SerializerClassMetadata);

if (null === $classMetadata) {
return null;
}

\assert($classMetadata instanceof SerializerClassMetadata);

foreach ($classMetadata->propertyMetadata as $key => $propertyMetadata) {
\assert($propertyMetadata instanceof PropertyMetadata);
if (null !== $propertyMetadata->hasDefault) {
Expand Down
2 changes: 1 addition & 1 deletion src/Metadata/Driver/DocBlockDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,12 @@ public function __construct(DriverInterface $delegate, ?ParserInterface $typePar
public function loadMetadataForClass(ReflectionClass $class): ?ClassMetadata
{
$classMetadata = $this->delegate->loadMetadataForClass($class);
\assert($classMetadata instanceof SerializerClassMetadata);

if (null === $classMetadata) {
return null;
}

\assert($classMetadata instanceof SerializerClassMetadata);
// We base our scan on the internal driver's property list so that we
// respect any internal allow/blocklist like in the AnnotationDriver
foreach ($classMetadata->propertyMetadata as $key => $propertyMetadata) {
Expand Down
3 changes: 2 additions & 1 deletion src/Metadata/Driver/EnumPropertiesDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,13 @@ public function __construct(DriverInterface $delegate)
public function loadMetadataForClass(ReflectionClass $class): ?ClassMetadata
{
$classMetadata = $this->delegate->loadMetadataForClass($class);
\assert($classMetadata instanceof SerializerClassMetadata);

if (null === $classMetadata) {
return null;
}

\assert($classMetadata instanceof SerializerClassMetadata);

// We base our scan on the internal driver's property list so that we
// respect any internal allow/blocklist like in the AnnotationDriver
foreach ($classMetadata->propertyMetadata as $propertyMetadata) {
Expand Down
5 changes: 5 additions & 0 deletions src/Metadata/Driver/TypedPropertiesDriver.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,11 @@ private function getDefaultWhiteList(): array
public function loadMetadataForClass(ReflectionClass $class): ?ClassMetadata
{
$classMetadata = $this->delegate->loadMetadataForClass($class);

if (null === $classMetadata) {
return null;
}

\assert($classMetadata instanceof SerializerClassMetadata);

if (PHP_VERSION_ID <= 70400) {
Expand Down

0 comments on commit 874250b

Please sign in to comment.