8000 [FrameworkBundle][Routing][Serializer][Validator] Deprecate Doctrine annotations by derrabus · Pull Request #50986 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[FrameworkBundle][Routing][Serializer][Validator] Deprecate Doctrine annotations #50986

New issue

Have a question about this project? Sign up for a free GitH 8000 ub 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

Closed
wants to merge 5 commits into from
Closed
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
16 changes: 16 additions & 0 deletions UPGRADE-6.4.md
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,26 @@ Routing
-------

* Add native return type to `AnnotationClassLoader::setResolver()`
* Deprecate Doctrine annotations support in favor of native attributes
* Change the constructor signature of `AnnotationClassLoader` to `__construct(?string $env = null)`, passing an annotation reader as first argument is deprecated

Security
--------

* `UserValueResolver` no longer implements `ArgumentValueResolverInterface`
* Make `PersistentToken` immutable
* Deprecate accepting only `DateTime` for `TokenProviderInterface::updateToken()`, use `DateTimeInterface` instead

Serializer
----------

* Deprecate Doctrine annotations support in favor of native attributes
* Deprecate passing an annotation reader to the constructor of `AnnotationLoader`

Validator
---------

* Deprecate Doctrine annotations support in favor of native attributes
* Deprecate passing an annotation reader to the constructor signature of `AnnotationLoader`
* Deprecate `ValidatorBuilder::setDoctrineAnnotationReader()`
* Deprecate `ValidatorBuilder::addDefaultDoctrineAnnotationReader()`
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
use Symfony\Component\Validator\Mapping\ClassMetadata;
use Symfony\Component\Validator\Mapping\PropertyMetadata;
use Symfony\Component\Validator\Mapping\TraversalStrategy;
use Symfony\Component\Validator\Tests\Fixtures\Entity;
use Symfony\Component\Validator\Tests\Fixtures\NestedAttribute\Entity;
use Symfony\Component\Validator\Validation;

/**
Expand All @@ -40,7 +40,6 @@ public function testLoadClassMetadata()
{
$validator = Validation::createValidatorBuilder()
->enableAnnotationMapping(true)
->addDefaultDoctrineAnnotationReader()
->addLoader(new DoctrineLoader(DoctrineTestHelper::createTestEntityManager(), '{^Symfony\\\\Bridge\\\\Doctrine\\\\Tests\\\\Fixtures\\\\DoctrineLoader}'))
->getValidator()
;
Expand Down Expand Up @@ -144,7 +143,6 @@ public function testExtractEnum()
$validator = Validation::createValidatorBuilder()
->addMethodMapping('loadValidatorMetadata')
->enableAnnotationMapping(true)
->addDefaultDoctrineAnnotationReader()
->addLoader(new DoctrineLoader(DoctrineTestHelper::createTestEntityManager(), '{^Symfony\\\\Bridge\\\\Doctrine\\\\Tests\\\\Fixtures\\\\DoctrineLoader}'))
->getValidator()
;
Expand All @@ -162,7 +160,6 @@ public function testFieldMappingsConfiguration()
{
$validator = Validation::createValidatorBuilder()
->enableAnnotationMapping(true)
->addDefaultDoctrineAnnotationReader()
->addXmlMappings([__DIR__.'/../Resources/validator/BaseUser.xml'])
->addLoader(
new DoctrineLoader(
Expand Down Expand Up @@ -204,7 +201,6 @@ public function testClassNoAutoMapping()
{
$validator = Validation::createValidatorBuilder()
->enableAnnotationMapping(true)
->addDefaultDoctrineAnnotationReader()
->addLoader(new DoctrineLoader(DoctrineTestHelper::createTestEntityManager(), '{.*}'))
->getValidator();

Expand Down
2 changes: 0 additions & 2 deletions src/Symfony/Bridge/Doctrine/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,15 +43,13 @@
"symfony/uid": "^5.4|^6.0|^7.0",
"symfony/validator": "^5.4.25|~6.2.12|^6.3.1|^7.0",
"symfony/var-dumper": "^5.4|^6.0|^7.0",
"doctrine/annotations": "^1.13.1|^2",
"doctrine/collections": "^1.0|^2.0",
"doctrine/data-fixtures": "^1.1",
"doctrine/dbal": "^2.13.1|^3.0",
"doctrine/orm": "^2.15",
"psr/log": "^1|^2|^3"
},
"conflict": {
"doctrine/annotations": "<1.13.1",
"doctrine/dbal": "<2.13.1",
"doctrine/lexer": "<1.1",
"doctrine/orm": "<2.15",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,7 @@
*/
class SerializerModelFixture
{
/**
* @Groups({"read"})
*/
#[Groups(['read'])]
public $name = 'howdy';

public $title = 'fixture';
Expand Down
3 changes: 1 addition & 2 deletions src/Symfony/Bridge/Twig/Tests/Extension/SerializerExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@

namespace Symfony\Bridge\Twig\Tests\Extension;

use Doctrine\Common\Annotations\AnnotationReader;
use PHPUnit\Framework\TestCase;
use Symfony\Bridge\Twig\Extension\SerializerExtension;
use Symfony\Bridge\Twig\Extension\SerializerRuntime;
Expand Down Expand Up @@ -50,7 +49,7 @@ public static function serializerDataProvider(): \Generator

private function getTwig(string $template): Environment
{
$meta = new ClassMetadataFactory(new AnnotationLoader(new AnnotationReader()));
$meta = new ClassMetadataFactory(new AnnotationLoader());
$runtime = new SerializerRuntime(new Serializer([new ObjectNormalizer($meta)], [new JsonEncoder(), new YamlEncoder()]));

$mockRuntimeLoader = $this->createMock(RuntimeLoaderInterface::class);
Expand Down
2 changes: 1 addition & 1 deletion src/Symfony/Bridge/Twig/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@
"twig/twig": "^2.13|^3.0.4"
},
"require-dev": {
"doctrine/annotations": "^1.12|^2",
"egulias/email-validator": "^2.1.10|^3|^4",
"league/html-to-markdown": "^5.0",
"phpdocumentor/reflection-docblock": "^3.0|^4.0|^5.0",
Expand Down Expand Up @@ -62,6 +61,7 @@
"symfony/http-foundation": "<5.4",
"symfony/http-kernel": "<6.2",
"symfony/mime": "<6.2",
"symfony/serializer": "<6.2",
"symfony/translation": "<5.4",
"symfony/workflow": "<5.4"
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@
use Symfony\Component\RateLimiter\Storage\CacheStorage;
use Symfony\Component\RemoteEvent\Attribute\AsRemoteEventConsumer;
use Symfony\Component\RemoteEvent\RemoteEvent;
use Symfony\Component\Routing\Loader\AnnotationClassLoader;
use Symfony\Component\Routing\Loader\Psr4DirectoryLoader;
use Symfony\Component\Scheduler\Attribute\AsSchedule;
use Symfony\Component\Scheduler\Messenger\SchedulerTransportFactory;
Expand Down Expand Up @@ -181,6 +182,7 @@
use Symfony\Component\Validator\Mapping\Loader\PropertyInfoLoader;
use Symfony\Component\Validator\ObjectInitializerInterface;
use Symfony\Component\Validator\Validation;
use Symfony\Component\Validator\ValidatorBuilder;
use Symfony\Component\Webhook\Controller\WebhookController;
use Symfony\Component\WebLink\HttpHeaderSerializer;
use Symfony\Component\Workflow;
Expand Down Expand Up @@ -1179,6 +1181,13 @@ private function registerRouterConfiguration(array $config, ContainerBuilder $co
if (!class_exists(Psr4DirectoryLoader::class)) {
$container->removeDefinition('routing.loader.psr4');
}

if ($this->isInitializedConfigEnabled('annotations') && (new \ReflectionClass(AnnotationClassLoader::class))->hasProperty('reader')) {
$container->getDefinition('routing.loader.annotation')->setArguments([
new Reference('annotation_reader'),
'%kernel.environment%',
]);
}
}

private function registerSessionConfiguration(array $config, ContainerBuilder $container, PhpFileLoader $loader): void
Expand Down Expand Up @@ -1609,7 +1618,7 @@ private function registerValidationConfiguration(array $config, ContainerBuilder

if (\array_key_exists('enable_annotations', $config) && $config['enable_annotations']) {
$validatorBuilder->addMethodCall('enableAnnotationMapping', [true]);
if ($this->isInitializedConfigEnabled('annotations')) {
if ($this->isInitializedConfigEnabled('annotations') && \method_exists(ValidatorBuilder::class, 'setDoctrineAnnotationReader')) {
$validatorBuilder->addMethodCall('setDoctrineAnnotationReader', [new Reference('annotation_reader')]);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,6 @@

->set('routing.loader.annotation', AnnotatedRouteControllerLoader::class)
->args([
service('annotation_reader')->nullOnInvalid(),
'%kernel.environment%',
])
->tag('routing.loader', ['priority' => -10])
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public function testWarmUp()
$validatorBuilder->addXmlMapping(__DIR__.'/../Fixtures/Validation/Resources/person.xml');
$validatorBuilder->addYamlMapping(__DIR__.'/../Fixtures/Validation/Resources/author.yml');
$validatorBuilder->addMethodMapping('loadValidatorMetadata');
$validatorBuilder->enableAnnotationMapping(true)->addDefaultDoctrineAnnotationReader();
$validatorBuilder->enableAnnotationMapping();

$file = sys_get_temp_dir().'/cache-validator.php';
@unlink($file);
Expand All @@ -46,7 +46,7 @@ public function testWarmUpWithAnnotations()
{
$validatorBuilder = new ValidatorBuilder();
$validatorBuilder->addYamlMapping(__DIR__.'/../Fixtures/Validation/Resources/categories.yml');
$validatorBuilder->enableAnnotationMapping(true)->addDefaultDoctrineAnnotationReader();
$validatorBuilder->enableAnnotationMapping();

$file = sys_get_temp_dir().'/cache-validator-with-annotations.php';
@unlink($file);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@
use Symfony\Component\Validator\DependencyInjection\AddConstraintValidatorsPass;
use Symfony\Component\Validator\Validation;
use Symfony\Component\Validator\Validator\ValidatorInterface;
use Symfony\Component\Validator\ValidatorBuilder;
use Symfony\Component\Webhook\Client\RequestParser;
use Symfony\Component\Webhook\Controller\WebhookController;
use Symfony\Component\Workflow;
Expand Down Expand Up @@ -1308,12 +1309,17 @@ public function testValidationLegacyAnnotations()

$this->assertCount(8, $calls);
$this->assertSame('enableAnnotationMapping', $calls[4][0]);
$this->assertSame('setDoctrineAnnotationReader', $calls[5][0]);
$this->assertEquals([new Reference('annotation_reader')], $calls[5][1]);
$this->assertSame('addMethodMapping', $calls[6][0]);
$this->assertSame(['loadValidatorMetadata'], $calls[6][1]);
$this->assertSame('setMappingCache', $calls[7][0]);
$this->assertEquals([new Reference('validator.mapping.cache.adapter')], $calls[7][1]);
if (\method_exists(ValidatorBuilder::class, 'setDoctrineAnnotationReader')) {
$this->assertSame('setDoctrineAnnotationReader', $calls[5][0]);
$this->assertEquals([new Reference('annotation_reader')], $calls[5][1]);
$i = 6;
} else {
$i = 5;
}
$this->assertSame('addMethodMapping', $calls[$i][0]);
$this->assertSame(['loadValidatorMetadata'], $calls[$i][1]);
$this->assertSame('setMappingCache', $calls[++$i][0]);
$this->assertEquals([new Reference('validator.mapping.cache.adapter')], $calls[$i][1]);
// no cache this time
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ class Category

public $id;

/**
* @Assert\Type("string")
*/
#[Assert\Type('string')]
public $name;
}
2 changes: 1 addition & 1 deletion src/Symfony/Bundle/FrameworkBundle/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
"symfony/polyfill-mbstring": "~1.0",
"symfony/filesystem": "^5.4|^6.0|^7.0",
"symfony/finder": "^5.4|^6.0|^7.0",
"symfony/routing": "^6.1|^7.0"
"symfony/routing": "^6.4|^7.0"
},
"require-dev": {
"doctrine/annotations": "^1.13.1|^2",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@

namespace Symfony\Component\PropertyInfo\Tests\Extractor;

use Doctrine\Common\Annotations\AnnotationReader;
use PHPUnit\Framework\TestCase;
use Symfony\Component\PropertyInfo\Extractor\SerializerExtractor;
use Symfony\Component\PropertyInfo\Tests\Fixtures\AdderRemoverDummy;
use Symfony\Component\PropertyInfo\Tests\Fixtures\Dummy;
use Symfony\Component\PropertyInfo\Tests\Fixtures\IgnorePropertyDummy;
use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactory;
use Symfony\Component\Serializer\Mapping\Loader\AnnotationLoader;
Expand All @@ -24,22 +24,19 @@
*/
class SerializerExtractorTest extends TestCase
{
/**
* @var SerializerExtractor
*/
private $extractor;
private SerializerExtractor $extractor;

protected function setUp(): void
{
$classMetadataFactory = new ClassMetadataFactory(new AnnotationLoader(new AnnotationReader()));
$classMetadataFactory = new ClassMetadataFactory(new AnnotationLoader());
$this->extractor = new SerializerExtractor($classMetadataFactory);
}

public function testGetProperties()
{
$this->assertEquals(
['collection'],
$this->extractor->getProperties('Symfony\Component\PropertyInfo\Tests\Fixtures\Dummy', ['serializer_groups' => ['a']])
$this->extractor->getProperties(Dummy::class, ['serializer_groups' => ['a']])
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ class Dummy extends ParentDummy

/**
* @var \DateTimeImmutable[]
* @Groups({"a", "b"})
*/
#[Groups(['a', 'b'])]
public $collection;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,9 @@
*/
class IgnorePropertyDummy
{
/**
* @Groups({"a"})
*/
#[Groups(['a'])]
public $visibleProperty;

/**
* @Groups({"a"})
* @Ignore
*/
#[Groups(['a']), Ignore]
private $ignoredProperty;
}
6 changes: 3 additions & 3 deletions src/Symfony/Component/PropertyInfo/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,13 @@
"symfony/cache": "^5.4|^6.0|^7.0",
"symfony/dependency-injection": "^5.4|^6.0|^7.0",
"phpdocumentor/reflection-docblock": "^5.2",
"phpstan/phpdoc-parser": "^1.0",
"doctrine/annotations": "^1.10.4|^2"
"phpstan/phpdoc-parser": "^1.0"
},
"conflict": {
"phpdocumentor/reflection-docblock": "<5.2",
"phpdocumentor/type-resolver": "<1.5.1",
"symfony/dependency-injection": "<5.4"
"symfony/dependency-injection": "<5.4",
"symfony/serializer": "<5.4"
},
"autoload": {
"psr-4": { "Symfony\\Component\\PropertyInfo\\": "" },
Expand Down
2 changes: 2 additions & 0 deletions src/Symfony/Component/Routing/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ CHANGELOG

* Add FQCN and FQCN::method aliases for routes loaded from attributes/annotations when applicable
* Add native return type to `AnnotationClassLoader::setResolver()`
* Deprecate Doctrine annotations support in favor of native attributes
* Change the constructor signature of `AnnotationClassLoader` to `__construct(?string $env = null)`, passing an annotation reader as first argument is deprecated

6.2
---
Expand Down
Loading
0