Skip to content

[Serializer] Give more hints when an attribute is not correctly used #44790

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

Merged
merged 1 commit into from
Jan 1, 2022
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
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,21 @@ public function loadAnnotations(object $reflector): iterable
{
foreach ($reflector->getAttributes() as $attribute) {
if ($this->isKnownAttribute($attribute->getName())) {
yield $attribute->newInstance();
try {
yield $attribute->newInstance();
} catch (\Error $e) {
if ($e::class !== \Error::class) {
throw $e;
}
$on = match (true) {
$reflector instanceof \ReflectionClass => ' on class '.$reflector->name,
$reflector instanceof \ReflectionMethod => sprintf(' on "%s::%s()"', $reflector->getDeclaringClass()->name, $reflector->name),
$reflector instanceof \ReflectionProperty => sprintf(' on "%s::$%s"', $reflector->getDeclaringClass()->name, $reflector->name),
default => '',
};

throw new MappingException(sprintf('Could not instantiate attribute "%s"%s.', $attribute->getName(), $on), 0, $e);
}
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Serializer\Tests\Fixtures\Attributes;

use Symfony\Component\Serializer\Annotation\Groups;

class BadAttributeDummy extends ContextDummyParent
{
#[Groups(['bar'])]
#[Groups(['foo'])]
public function myMethod()
{
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,7 @@ abstract class AnnotationLoaderTest extends TestCase
{
use ContextMappingTestTrait;

/**
* @var AnnotationLoader
*/
private $loader;
protected AnnotationLoader $loader;

protected function setUp(): void
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

namespace Symfony\Component\Serializer\Tests\Mapping\Loader;

use Symfony\Component\Serializer\Exception\MappingException;
use Symfony\Component\Serializer\Mapping\ClassMetadata;
use Symfony\Component\Serializer\Mapping\Loader\AnnotationLoader;

class AnnotationLoaderWithAttributesTest extends AnnotationLoaderTest
Expand All @@ -24,4 +26,14 @@ protected function getNamespace(): string
{
return 'Symfony\Component\Serializer\Tests\Fixtures\Attributes';
}

public function testLoadWithInvalidAttribute()
{
$this->expectException(MappingException::class);
$this->expectExceptionMessage('Could not instantiate attribute "Symfony\Component\Serializer\Annotation\Groups" on "Symfony\Component\Serializer\Tests\Fixtures\Attributes\BadAttributeDummy::myMethod()".');

$classMetadata = new ClassMetadata($this->getNamespace().'\BadAttributeDummy');

$this->loader->loadClassMetadata($classMetadata);
}
}