Skip to content
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 @@ -18,6 +18,7 @@
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\Serializer\Mapping\Loader\AnnotationLoader;
use Symfony\Component\Serializer\Mapping\Loader\AttributeLoader;

/**
Expand All @@ -43,15 +44,32 @@ public function process(ContainerBuilder $container): void
return;
}

// Symfony 6.4 ships an AttributeLoader with a different constructor signature
// (`?Doctrine\Common\Annotations\Reader`), incompatible with the `allowAnyClass`/`mappedClasses`
// arguments below. The `AnnotationLoader` class only exists on the 6.4 branch
// (removed in 7.0), so its presence is a reliable marker for that signature. See #8244.
if (class_exists(AnnotationLoader::class)) {
return;
}

$chainLoader = $container->getDefinition('serializer.mapping.chain_loader');
$loaders = $chainLoader->getArgument(0);

// Skip when Symfony already wired an AttributeLoader (i.e. `enable_attributes: true`).
// Adding another one would duplicate work and re-process every class twice.
foreach ($loaders as $loader) {
if ($loader instanceof Definition && is_a($loader->getClass(), AttributeLoader::class, true)) {
return;
}
}

$mappedClasses = [
Error::class => [Error::class],
ValidationException::class => [ValidationException::class],
];

$loaderDefinition = new Definition(AttributeLoader::class, [true, $mappedClasses]);

$chainLoader = $container->getDefinition('serializer.mapping.chain_loader');
$loaders = $chainLoader->getArgument(0);
$loaders[] = $loaderDefinition;
$chainLoader->replaceArgument(0, $loaders);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,41 @@ public function testDoesNothingWhenChainLoaderIsAbsent(): void
$this->assertFalse($container->hasDefinition('serializer.mapping.chain_loader'));
}

public function testDoesNothingWhenChainAlreadyContainsAnAttributeLoader(): void
{
$container = new ContainerBuilder();
$existing = new Definition(AttributeLoader::class);
$container->setDefinition('serializer.mapping.chain_loader', new Definition(LoaderChain::class, [[$existing]]));
$container->setDefinition('serializer.mapping.cache_warmer', new Definition(\stdClass::class, [[$existing]]));

(new ErrorResourceAttributeLoaderPass())->process($container);

$loaders = $container->getDefinition('serializer.mapping.chain_loader')->getArgument(0);
$this->assertCount(1, $loaders, 'pass must not add another AttributeLoader when one is already wired (enable_attributes: true).');

$warmerLoaders = $container->getDefinition('serializer.mapping.cache_warmer')->getArgument(0);
$this->assertCount(1, $warmerLoaders);
}

/**
* @see https://github.com/api-platform/core/issues/8244
*/
public function testSkipsOnSymfony64SerializerSignature(): void
{
if (!class_exists(\Symfony\Component\Serializer\Mapping\Loader\AnnotationLoader::class)) {
$this->markTestSkipped('Only relevant when running against symfony/serializer 6.4 (AnnotationLoader still present).');
}

$container = new ContainerBuilder();
$container->setDefinition('serializer.mapping.chain_loader', new Definition(LoaderChain::class, [[]]));
$container->setDefinition('serializer.mapping.cache_warmer', new Definition(\stdClass::class, [[]]));

(new ErrorResourceAttributeLoaderPass())->process($container);

$this->assertSame([], $container->getDefinition('serializer.mapping.chain_loader')->getArgument(0));
$this->assertSame([], $container->getDefinition('serializer.mapping.cache_warmer')->getArgument(0));
}

/**
* Mirrors the runtime behavior with `framework.serializer.enable_attributes: false`:
* Symfony builds the `AttributeLoader` with `allowAnyClass = false` and no mapped classes,
Expand Down
Loading