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
2 changes: 2 additions & 0 deletions src/Symfony/Bundle/ApiPlatformBundle.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
use ApiPlatform\Symfony\Bundle\DependencyInjection\Compiler\JsonStreamerTransformerPass;
use ApiPlatform\Symfony\Bundle\DependencyInjection\Compiler\MetadataAwareNameConverterPass;
use ApiPlatform\Symfony\Bundle\DependencyInjection\Compiler\MutatorPass;
use ApiPlatform\Symfony\Bundle\DependencyInjection\Compiler\PropertyInfoTagPass;
use ApiPlatform\Symfony\Bundle\DependencyInjection\Compiler\SerializerMappingLoaderPass;
use ApiPlatform\Symfony\Bundle\DependencyInjection\Compiler\TestClientPass;
use ApiPlatform\Symfony\Bundle\DependencyInjection\Compiler\TestMercureHubPass;
Expand Down Expand Up @@ -60,6 +61,7 @@ public function build(ContainerBuilder $container): void
$container->addCompilerPass(new AuthenticatorManagerPass());
$container->addCompilerPass(new SerializerMappingLoaderPass());
$container->addCompilerPass(new MutatorPass());
$container->addCompilerPass(new PropertyInfoTagPass(), PassConfig::TYPE_BEFORE_OPTIMIZATION, -100);
// Must run after Symfony's TransformerPass so we can rely on the value_object_transformer tag being processed.
$container->addCompilerPass(new JsonStreamerTransformerPass(), PassConfig::TYPE_BEFORE_OPTIMIZATION, -10);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php

/*
* This file is part of the API Platform project.
*
* (c) Kévin Dunglas <dunglas@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace ApiPlatform\Symfony\Bundle\DependencyInjection\Compiler;

use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;

/**
* Bridges Symfony's public `property_info.*` tags to API Platform's private
* `api_platform.property_info.*` tags so that `api_platform.property_info`
* inherits framework- and third-party-registered extractors (e.g. Doctrine's
* `DoctrineExtractor`) without API Platform's own extractors leaking back into
* Symfony's `property_info` service.
*
* @internal
*
* @see https://github.com/api-platform/core/issues/8201
*/
final class PropertyInfoTagPass implements CompilerPassInterface
{
private const TAG_SUFFIXES = [
'list_extractor',
'type_extractor',
'description_extractor',
'access_extractor',
'initializable_extractor',
];

public function process(ContainerBuilder $container): void
{
foreach (self::TAG_SUFFIXES as $suffix) {
$publicTag = 'property_info.'.$suffix;
$privateTag = 'api_platform.property_info.'.$suffix;

foreach ($container->findTaggedServiceIds($publicTag) as $serviceId => $tags) {
$definition = $container->getDefinition($serviceId);
if ($definition->hasTag($privateTag)) {
continue;
}
foreach ($tags as $attributes) {
$definition->addTag($privateTag, $attributes);
}
}
}
}
}
24 changes: 12 additions & 12 deletions src/Symfony/Bundle/Resources/config/api.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,29 +77,29 @@
$services->alias('api_platform.property_accessor', 'property_accessor');

$services->set('api_platform.property_info.reflection_extractor', ReflectionExtractor::class)
->tag('property_info.list_extractor', ['priority' => -1000])
->tag('property_info.type_extractor', ['priority' => -1002])
->tag('property_info.access_extractor', ['priority' => -1000])
->tag('property_info.initializable_extractor', ['priority' => -1000]);
->tag('api_platform.property_info.list_extractor', ['priority' => -1000])
->tag('api_platform.property_info.type_extractor', ['priority' => -1002])
->tag('api_platform.property_info.access_extractor', ['priority' => -1000])
->tag('api_platform.property_info.initializable_extractor', ['priority' => -1000]);

if (class_exists(DocBlockFactory::class)) {
$services->set('api_platform.property_info.php_doc_extractor', PhpDocExtractor::class)
->tag('property_info.description_extractor', ['priority' => -1000])
->tag('property_info.type_extractor', ['priority' => -1001]);
->tag('api_platform.property_info.description_extractor', ['priority' => -1000])
->tag('api_platform.property_info.type_extractor', ['priority' => -1001]);
}

if (class_exists(PhpDocParser::class) && class_exists(ContextFactory::class)) {
$services->set('api_platform.property_info.phpstan_extractor', PhpStanExtractor::class)
->tag('property_info.type_extractor', ['priority' => -1000]);
->tag('api_platform.property_info.type_extractor', ['priority' => -1000]);
}

$services->set('api_platform.property_info', PropertyInfoExtractor::class)
->args([
tagged_iterator('property_info.list_extractor'),
tagged_iterator('property_info.type_extractor'),
tagged_iterator('property_info.description_extractor'),
tagged_iterator('property_info.access_extractor'),
tagged_iterator('property_info.initializable_extractor'),
tagged_iterator('api_platform.property_info.list_extractor'),
tagged_iterator('api_platform.property_info.type_extractor'),
tagged_iterator('api_platform.property_info.description_extractor'),
tagged_iterator('api_platform.property_info.access_extractor'),
tagged_iterator('api_platform.property_info.initializable_extractor'),
]);

$services->set('api_platform.property_info.cache', PropertyInfoCacheExtractor::class)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -412,4 +412,36 @@ public function testPaginationMaximumItemsPerPageWhenDefaultsKeyIsMissing(): voi
$this->assertTrue($this->container->hasParameter('api_platform.collection.pagination.maximum_items_per_page'));
$this->assertSame(30, $this->container->getParameter('api_platform.collection.pagination.maximum_items_per_page'));
}

/**
* @see https://github.com/api-platform/core/issues/8201
*/
public function testPropertyInfoExtractorsDoNotLeakIntoFrameworkPropertyInfo(): void
{
$config = self::DEFAULT_CONFIG;
(new ApiPlatformExtension())->load($config, $this->container);

$services = ['api_platform.property_info.reflection_extractor'];
if (class_exists(\phpDocumentor\Reflection\DocBlockFactory::class)) {
$services[] = 'api_platform.property_info.php_doc_extractor';
}
if (class_exists(\PHPStan\PhpDocParser\Parser\PhpDocParser::class) && class_exists(\phpDocumentor\Reflection\Types\ContextFactory::class)) {
$services[] = 'api_platform.property_info.phpstan_extractor';
}

foreach ($services as $service) {
$this->assertContainerHasService($service);
$tags = $this->container->getDefinition($service)->getTags();
foreach ($tags as $name => $_) {
$this->assertStringStartsNotWith('property_info.', $name, \sprintf('Service "%s" must not use the global "property_info.*" tag namespace (leaks into Symfony\'s property_info and breaks the validator chain — issue #8201). Found tag "%s".', $service, $name));
}
}

$apiPlatformPropertyInfo = $this->container->getDefinition('api_platform.property_info');
foreach ($apiPlatformPropertyInfo->getArguments() as $arg) {
if ($arg instanceof \Symfony\Component\DependencyInjection\Argument\TaggedIteratorArgument) {
$this->assertStringStartsWith('api_platform.property_info.', $arg->getTag(), \sprintf('api_platform.property_info must consume only "api_platform.property_info.*" private tags; found "%s".', $arg->getTag()));
}
}
}
}
2 changes: 2 additions & 0 deletions tests/Symfony/Bundle/ApiPlatformBundleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
use ApiPlatform\Symfony\Bundle\DependencyInjection\Compiler\JsonStreamerTransformerPass;
use ApiPlatform\Symfony\Bundle\DependencyInjection\Compiler\MetadataAwareNameConverterPass;
use ApiPlatform\Symfony\Bundle\DependencyInjection\Compiler\MutatorPass;
use ApiPlatform\Symfony\Bundle\DependencyInjection\Compiler\PropertyInfoTagPass;
use ApiPlatform\Symfony\Bundle\DependencyInjection\Compiler\SerializerMappingLoaderPass;
use ApiPlatform\Symfony\Bundle\DependencyInjection\Compiler\TestClientPass;
use ApiPlatform\Symfony\Bundle\DependencyInjection\Compiler\TestMercureHubPass;
Expand Down Expand Up @@ -59,6 +60,7 @@ public function testBuild(): void
$this->assertContains(AuthenticatorManagerPass::class, $passClasses);
$this->assertContains(SerializerMappingLoaderPass::class, $passClasses);
$this->assertContains(MutatorPass::class, $passClasses);
$this->assertContains(PropertyInfoTagPass::class, $passClasses);
$this->assertContains(JsonStreamerTransformerPass::class, $passClasses);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php

/*
* This file is part of the API Platform project.
*
* (c) Kévin Dunglas <dunglas@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace ApiPlatform\Tests\Symfony\Bundle\DependencyInjection\Compiler;

use ApiPlatform\Symfony\Bundle\DependencyInjection\Compiler\PropertyInfoTagPass;
use PHPUnit\Framework\TestCase;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Definition;

/**
* @see https://github.com/api-platform/core/issues/8201
*/
final class PropertyInfoTagPassTest extends TestCase
{
public function testBridgesPublicTagsToPrivateNamespace(): void
{
$container = new ContainerBuilder();
$container->setDefinition('framework.reflection_extractor', (new Definition(\stdClass::class))
->addTag('property_info.type_extractor', ['priority' => -100])
->addTag('property_info.list_extractor'));

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

$definition = $container->getDefinition('framework.reflection_extractor');
$this->assertSame([['priority' => -100]], $definition->getTag('api_platform.property_info.type_extractor'));
$this->assertSame([[]], $definition->getTag('api_platform.property_info.list_extractor'));
}

public function testSkipsServicesAlreadyCarryingThePrivateTag(): void
{
$container = new ContainerBuilder();
$container->setDefinition('api_platform.property_info.reflection_extractor', (new Definition(\stdClass::class))
->addTag('property_info.type_extractor', ['priority' => -1002])
->addTag('api_platform.property_info.type_extractor', ['priority' => -1002]));

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

$tags = $container->getDefinition('api_platform.property_info.reflection_extractor')->getTag('api_platform.property_info.type_extractor');
$this->assertCount(1, $tags, 'Pass must not re-tag services that already carry the private tag.');
}

public function testDoesNotTagServicesWithoutPublicTags(): void
{
$container = new ContainerBuilder();
$container->setDefinition('unrelated_service', new Definition(\stdClass::class));

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

$this->assertSame([], $container->getDefinition('unrelated_service')->getTags());
}
}
Loading