8000 [Validator] Deprecate `ValidatorBuilder::enableAnnotationMapping()` and `ValidatorBuilder::disableAnnotationMapping()` by alexandre-daubois · Pull Request #51426 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Validator] Deprecate ValidatorBuilder::enableAnnotationMapping() and ValidatorBuilder::disableAnnotationMapping() #51426

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.

8000

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
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 UPGRADE-6.4.md
Original file line number Diff line number Diff line change
Expand Up @@ -157,3 +157,5 @@ Validator
* Deprecate passing an annotation reader to the constructor signature of `AnnotationLoader`
* Deprecate `ValidatorBuilder::setDoctrineAnnotationReader()`
* Deprecate `ValidatorBuilder::addDefaultDoctrineAnnotationReader()`
* Deprecate `ValidatorBuilder::enableAnnotationMapping()`, use `ValidatorBuilder::enableAttributeMapping()` instead
* Deprecate `ValidatorBuilder::disableAnnotationMapping()`, use `ValidatorBuilder::disableAttributeMapping()` instead
2 changes: 2 additions & 0 deletions src/Symfony/Component/Validator/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ CHANGELOG
* Deprecate `ValidatorBuilder::addDefaultDoctrineAnnotationReader()`
* Add `number`, `finite-number` and `finite-float` types to `Type` constraint
* Add the `withSeconds` option to the `Time` constraint that allows to pass time without seconds
* Deprecate `ValidatorBuilder::enableAnnotationMapping()`, use `ValidatorBuilder::enableAttributeMapping()` instead
* Deprecate `ValidatorBuilder::disableAnnotationMapping()`, use `ValidatorBuilder::disableAttributeMapping()` instead

6.3
---
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class ValidValidatorTest extends TestCase
public function testPropertyPathsArePassedToNestedContexts()
{
$validatorBuilder = new ValidatorBuilder();
$validator = $validatorBuilder->enableAnnotationMapping()->getValidator();
$validator = $validatorBuilder->enableAttributeMapping()->getValidator();

$violations = $validator->validate(new Foo(), null, ['nested']);

Expand All @@ -31,7 +31,7 @@ public function testPropertyPathsArePassedToNestedContexts()
public function testNullValues()
{
$validatorBuilder = new ValidatorBuilder();
$validator = $validatorBuilder->enableAnnotationMapping()->getValidator();
$validator = $validatorBuilder->enableAttributeMapping()->getValidator();

$foo = new Foo();
$foo->fooBar = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ public function testLoadClassMetadata()
$propertyInfoLoader = new PropertyInfoLoader($propertyInfoStub, $propertyInfoStub, $propertyInfoStub, '{.*}');

$validator = Validation::createValidatorBuilder()
->enableAnnotationMapping()
->enableAttributeMapping()
->addLoader($propertyInfoLoader)
->getValidator()
;
Expand Down Expand Up @@ -230,7 +230,7 @@ public function testClassNoAutoMapping()

$propertyInfoLoader = new PropertyInfoLoader($propertyInfoStub, $propertyInfoStub, $propertyInfoStub, '{.*}');
$validator = Validation::createValidatorBuilder()
->enableAnnotationMapping()
->enableAttributeMapping()
->addLoader($propertyInfoLoader)
->getValidator()
;
Expand Down
26 changes: 22 additions & 4 deletions src/Symfony/Component/Validator/Tests/ValidatorBuilderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ public function testAddMethodMappings()
*/
public function testEnableAnnotationMappingWithDefaultDoctrineAnnotationReader()
{
$this->assertSame($this->builder, $this->builder->enableAnnotationMapping());
$this->assertSame($this->builder, $this->builder->enableAttributeMapping());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this test is really about annotation mapping. So I would rather expect both deprecations in it than changing method


$this->expectDeprecation('Since symfony/validator 6.4: Method "Symfony\Component\Validator\ValidatorBuilder::addDefaultDoctrineAnnotationReader()" is deprecated without replacement.');
$this->assertSame($this->builder, $this->builder->addDefaultDoctrineAnnotationReader());
Expand All @@ -102,7 +102,7 @@ public function testEnableAnnotationMappingWithCustomDoctrineAnnotationReader()
{
$reader = $this->createMock(Reader::class);

$this->assertSame($this->builder, $this->builder->enableAnnotationMapping());
$this->assertSame($this->builder, $this->builder->enableAttributeMapping());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same here


$this->expectDeprecation('Since symfony/validator 6.4: Method "Symfony\Component\Validator\ValidatorBuilder::setDoctrineAnnotationReader()" is deprecated without replacement.');
$this->assertSame($this->builder, $this->builder->setDoctrineAnnotationReader($reader));
Expand All @@ -116,9 +116,27 @@ public function testEnableAnnotationMappingWithCustomDoctrineAnnotationReader()
$this->assertSame($reader, $r->getValue($loaders[0]));
}

public function testDisableAnnotationMapping()
/**
* @group legacy
*/
public function testExpectDeprecationWhenEnablingAnnotationMapping()
{
$this->expectDeprecation('Since symfony/validator 6.4: Method "Symfony\Component\Validator\ValidatorBuilder::enableAnnotationMapping()" is deprecated, use "enableAttributeMapping()" instead.');
$this->builder->enableAnnotationMapping();
}

/**
* @group legacy
*/
public function testExpectDeprecationWhenDisablingAnnotationMapping()
{
$this->expectDeprecation('Since symfony/validator 6.4: Method "Symfony\Component\Validator\ValidatorBuilder::disableAnnotationMapping()" is deprecated, use "disableAttributeMapping()" instead.');
$this->builder->disableAnnotationMapping();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we should not loose the assertion about the fluent API that we had before

}

public function testDisableAttributeMapping()
{
$this->assertSame($this->builder, $this->builder->disableAnnotationMapping());
$this->assertSame($this->builder, $this->builder->disableAttributeMapping());
}

public function testSetMappingCache()
Expand Down
39 changes: 32 additions & 7 deletions src/Symfony/Component/Validator/ValidatorBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ class ValidatorBuilder
private array $yamlMappings = [];
private array $methodMappings = [];
private ?Reader $annotationReader = null;
private bool $enableAnnotationMapping = false;
private bool $enableAttributeMapping = false;
private ?MetadataFactoryInterface $metadataFactory = null;
private ConstraintValidatorFactoryInterface $validatorFactory;
private ?CacheItemPoolInterface $mappingCache = null;
Expand Down Expand Up @@ -187,31 +187,56 @@ public function addMethodMappings(array $methodNames): static
}

/**
* Enables annotation and attribute based constraint mapping.
* @deprecated since Symfony 6.4, use "enableAttributeMapping()" instead.
*
* @return $this
*/
public function enableAnnotationMapping(): static
{
trigger_deprecation('symfony/validator', '6.4', 'Method "%s()" is deprecated, use "enableAttributeMapping()" instead.', __METHOD__);

return $this->enableAttributeMapping();
}

/**
* Enables attribute based constraint mapping.
*
* @return $this
*/
public function enableAttributeMapping(): static
{
if (null !== $this->metadataFactory) {
throw new ValidatorException('You cannot enable annotation mapping after setting a custom metadata factory. Configure your metadata factory instead.');
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this exception message should be updated

}

$this->enableAnnotationMapping = true;
$this->enableAttributeMapping = true;

return $this;
}

/**
* Disables annotation and attribute based constraint mapping.
* @deprecated since Symfony 6.4, use "disableAttributeMapping()" instead
*
* @return $this
*/
public function disableAnnotationMapping(): static
{
$this->enableAnnotationMapping = false;
trigger_deprecation('symfony/validator', '6.4', 'Method "%s()" is deprecated, use "disableAttributeMapping()" instead.', __METHOD__);

$this->annotationReader = null;

return $this->disableAttributeMapping();
}

/**
* Disables attribute based constraint mapping.
*
* @return $this
*/
public function disableAttributeMapping(): static
{
$this->enableAttributeMapping = false;

return $this;
}

Expand Down Expand Up @@ -250,7 +275,7 @@ public function addDefaultDoctrineAnnotationReader(): static
*/
public function setMetadataFactory(MetadataFactoryInterface $metadataFactory): static
{
if (\count($this->xmlMappings) > 0 || \count($this->yamlMappings) > 0 || \count($this->methodMappings) > 0 || $this->enableAnnotationMapping) {
if (\count($this->xmlMappings) > 0 || \count($this->yamlMappings) > 0 || \count($this->methodMappings) > 0 || $this->enableAttributeMapping) {
throw new ValidatorException('You cannot set a custom metadata factory after adding custom mappings. You should do either of both.');
}

Expand Down Expand Up @@ -344,7 +369,7 @@ public function getLoaders(): array
$loaders[] = new StaticMethodLoader($methodName);
}

if ($this->enableAnnotationMapping) {
if ($this->enableAttributeMapping) {
$loaders[] = new AnnotationLoader($this->annotationReader);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if you don't rename the loader class as well, this work is incomplete

}

Expand Down
0