2018-04-06 18:44:20 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This file is part of the NelmioApiDocBundle package.
|
|
|
|
*
|
|
|
|
* (c) Nelmio
|
|
|
|
*
|
|
|
|
* For the full copyright and license information, please view the LICENSE
|
|
|
|
* file that was distributed with this source code.
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace Nelmio\ApiDocBundle\Tests\ModelDescriber\Annotations;
|
|
|
|
|
|
|
|
use Doctrine\Common\Annotations\AnnotationReader;
|
|
|
|
use Nelmio\ApiDocBundle\ModelDescriber\Annotations\SymfonyConstraintAnnotationReader;
|
2021-06-16 10:59:06 +03:00
|
|
|
use Nelmio\ApiDocBundle\Tests\Helper;
|
|
|
|
use Nelmio\ApiDocBundle\Tests\ModelDescriber\Annotations\Fixture as CustomAssert;
|
2020-05-28 13:19:11 +02:00
|
|
|
use OpenApi\Annotations as OA;
|
2018-04-06 18:44:20 +01:00
|
|
|
use PHPUnit\Framework\TestCase;
|
2021-11-06 07:35:50 -05:00
|
|
|
use Symfony\Component\Validator\Constraint;
|
2018-04-06 18:44:20 +01:00
|
|
|
use Symfony\Component\Validator\Constraints as Assert;
|
|
|
|
|
|
|
|
class SymfonyConstraintAnnotationReaderTest extends TestCase
|
|
|
|
{
|
|
|
|
public function testUpdatePropertyFix1283()
|
|
|
|
{
|
|
|
|
$entity = new class() {
|
|
|
|
/**
|
|
|
|
* @Assert\NotBlank()
|
|
|
|
* @Assert\Length(min = 1)
|
|
|
|
*/
|
|
|
|
private $property1;
|
|
|
|
/**
|
|
|
|
* @Assert\NotBlank()
|
|
|
|
*/
|
|
|
|
private $property2;
|
|
|
|
};
|
2018-09-11 13:57:22 +03:00
|
|
|
|
2020-05-28 13:19:11 +02:00
|
|
|
$schema = new OA\Schema([]);
|
|
|
|
$schema->merge([new OA\Property(['property' => 'property1'])]);
|
|
|
|
$schema->merge([new OA\Property(['property' => 'property2'])]);
|
2018-04-06 18:44:20 +01:00
|
|
|
|
|
|
|
$symfonyConstraintAnnotationReader = new SymfonyConstraintAnnotationReader(new AnnotationReader());
|
|
|
|
$symfonyConstraintAnnotationReader->setSchema($schema);
|
2018-09-11 13:57:22 +03:00
|
|
|
|
2020-05-28 13:19:11 +02:00
|
|
|
$symfonyConstraintAnnotationReader->updateProperty(new \ReflectionProperty($entity, 'property1'), $schema->properties[0]);
|
|
|
|
$symfonyConstraintAnnotationReader->updateProperty(new \ReflectionProperty($entity, 'property2'), $schema->properties[1]);
|
2018-04-06 18:44:20 +01:00
|
|
|
|
|
|
|
// expect required to be numeric array with sequential keys (not [0 => ..., 2 => ...])
|
2020-05-28 13:19:11 +02:00
|
|
|
$this->assertEquals($schema->required, ['property1', 'property2']);
|
2018-04-06 18:44:20 +01:00
|
|
|
}
|
2020-03-14 15:36:59 +03:00
|
|
|
|
2021-03-12 00:59:35 +01:00
|
|
|
/**
|
|
|
|
* @param object $entity
|
|
|
|
* @dataProvider provideOptionalProperty
|
|
|
|
*/
|
|
|
|
public function testOptionalProperty($entity)
|
2020-08-31 23:22:42 +03:00
|
|
|
{
|
2020-09-28 22:45:24 +03:00
|
|
|
if (!\property_exists(Assert\NotBlank::class, 'allowNull')) {
|
2020-09-20 20:38:26 +02:00
|
|
|
$this->markTestSkipped('NotBlank::allowNull was added in symfony/validator 4.3.');
|
|
|
|
}
|
|
|
|
|
2020-09-09 08:55:41 +02:00
|
|
|
$schema = new OA\Schema([]);
|
|
|
|
$schema->merge([new OA\Property(['property' => 'property1'])]);
|
|
|
|
$schema->merge([new OA\Property(['property' => 'property2'])]);
|
2020-08-31 23:22:42 +03:00
|
|
|
|
|
|
|
$symfonyConstraintAnnotationReader = new SymfonyConstraintAnnotationReader(new AnnotationReader());
|
|
|
|
$symfonyConstraintAnnotationReader->setSchema($schema);
|
|
|
|
|
2020-09-09 08:55:41 +02:00
|
|
|
$symfonyConstraintAnnotationReader->updateProperty(new \ReflectionProperty($entity, 'property1'), $schema->properties[0]);
|
|
|
|
$symfonyConstraintAnnotationReader->updateProperty(new \ReflectionProperty($entity, 'property2'), $schema->properties[1]);
|
2020-08-31 23:22:42 +03:00
|
|
|
|
|
|
|
// expect required to be numeric array with sequential keys (not [0 => ..., 2 => ...])
|
2020-09-09 08:55:41 +02:00
|
|
|
$this->assertEquals($schema->required, ['property2']);
|
2018-04-06 18:44:20 +01:00
|
|
|
}
|
2020-03-14 15:36:59 +03:00
|
|
|
|
2021-03-12 00:59:35 +01:00
|
|
|
public function provideOptionalProperty(): iterable
|
2020-03-14 15:36:59 +03:00
|
|
|
{
|
2021-03-12 00:59:35 +01:00
|
|
|
yield 'Annotations' => [new class() {
|
2020-03-14 15:36:59 +03:00
|
|
|
/**
|
2021-03-12 00:59:35 +01:00
|
|
|
* @Assert\NotBlank(allowNull = true)
|
2020-03-14 15:36:59 +03:00
|
|
|
* @Assert\Length(min = 1)
|
|
|
|
*/
|
|
|
|
private $property1;
|
2021-03-12 00:59:35 +01:00
|
|
|
/**
|
|
|
|
* @Assert\NotBlank()
|
|
|
|
*/
|
|
|
|
private $property2;
|
|
|
|
}];
|
|
|
|
|
|
|
|
if (\PHP_VERSION_ID >= 80000) {
|
|
|
|
yield 'Attributes' => [new class() {
|
|
|
|
#[Assert\NotBlank(allowNull: true)]
|
|
|
|
#[Assert\Length(min: 1)]
|
|
|
|
private $property1;
|
|
|
|
#[Assert\NotBlank]
|
|
|
|
private $property2;
|
|
|
|
}];
|
|
|
|
}
|
|
|
|
}
|
2020-03-14 15:36:59 +03:00
|
|
|
|
2021-03-12 00:59:35 +01:00
|
|
|
/**
|
|
|
|
* @param object $entity
|
|
|
|
* @dataProvider provideAssertChoiceResultsInNumericArray
|
|
|
|
*/
|
|
|
|
public function testAssertChoiceResultsInNumericArray($entity)
|
|
|
|
{
|
2020-05-28 13:19:11 +02:00
|
|
|
$schema = new OA\Schema([]);
|
|
|
|
$schema->merge([new OA\Property(['property' => 'property1'])]);
|
2020-03-14 15:36:59 +03:00
|
|
|
|
|
|
|
$symfonyConstraintAnnotationReader = new SymfonyConstraintAnnotationReader(new AnnotationReader());
|
|
|
|
$symfonyConstraintAnnotationReader->setSchema($schema);
|
|
|
|
|
2020-05-28 13:19:11 +02:00
|
|
|
$symfonyConstraintAnnotationReader->updateProperty(new \ReflectionProperty($entity, 'property1'), $schema->properties[0]);
|
2020-03-14 15:36:59 +03:00
|
|
|
|
|
|
|
// expect enum to be numeric array with sequential keys (not [1 => "active", 2 => "active"])
|
2020-05-28 13:19:11 +02:00
|
|
|
$this->assertEquals($schema->properties[0]->enum, ['active', 'blocked']);
|
2020-03-14 15:36:59 +03:00
|
|
|
}
|
2021-02-10 10:33:55 -06:00
|
|
|
|
2021-03-12 00:59:35 +01:00
|
|
|
public function provideAssertChoiceResultsInNumericArray(): iterable
|
Apply `enum` from Choice Constraints to Items When Choice is Multiple (#1784)
* Apply `enum` from Choice Constraints to Items When Choice is Multiple
Otherwise JSON schema like this is generated:
```
"property": {
"type": "array",
"enum": ["one", "two", "three"],
"items": {
"type": "string"
}
}
```
With this change, however, this schema is generated:
```
"property": {
"type": "array",
"items": {
"type": "string",
"enum": ["one", "two", "three"]
}
}
```
A possible downside here is that the symfony constraint stuff happens
before types are figured out from PHPDoc. So it's _possible_ to end up
with something that won't validated. Take something like this:
```
/**
* @Assert\Choice(multiple=true, choices={"..."})
* @var string
*/
```
This would generate:
```
"property": {
"type": "string",
"items": {
"enum": ["..."]
}
}
```
* Fix CS
* cs
* more cs
* fix tests
Co-authored-by: Guilhem Niot <guilhem@gniot.fr>
2021-02-19 02:41:32 -06:00
|
|
|
{
|
2021-03-12 00:59:35 +01:00
|
|
|
define('TEST_ASSERT_CHOICE_STATUSES', [
|
|
|
|
1 => 'active',
|
|
|
|
2 => 'blocked',
|
|
|
|
]);
|
|
|
|
|
|
|
|
yield 'Annotations' => [new class() {
|
Apply `enum` from Choice Constraints to Items When Choice is Multiple (#1784)
* Apply `enum` from Choice Constraints to Items When Choice is Multiple
Otherwise JSON schema like this is generated:
```
"property": {
"type": "array",
"enum": ["one", "two", "three"],
"items": {
"type": "string"
}
}
```
With this change, however, this schema is generated:
```
"property": {
"type": "array",
"items": {
"type": "string",
"enum": ["one", "two", "three"]
}
}
```
A possible downside here is that the symfony constraint stuff happens
before types are figured out from PHPDoc. So it's _possible_ to end up
with something that won't validated. Take something like this:
```
/**
* @Assert\Choice(multiple=true, choices={"..."})
* @var string
*/
```
This would generate:
```
"property": {
"type": "string",
"items": {
"enum": ["..."]
}
}
```
* Fix CS
* cs
* more cs
* fix tests
Co-authored-by: Guilhem Niot <guilhem@gniot.fr>
2021-02-19 02:41:32 -06:00
|
|
|
/**
|
2021-03-12 00:59:35 +01:00
|
|
|
* @Assert\Length(min = 1)
|
|
|
|
* @Assert\Choice(choices=TEST_ASSERT_CHOICE_STATUSES)
|
Apply `enum` from Choice Constraints to Items When Choice is Multiple (#1784)
* Apply `enum` from Choice Constraints to Items When Choice is Multiple
Otherwise JSON schema like this is generated:
```
"property": {
"type": "array",
"enum": ["one", "two", "three"],
"items": {
"type": "string"
}
}
```
With this change, however, this schema is generated:
```
"property": {
"type": "array",
"items": {
"type": "string",
"enum": ["one", "two", "three"]
}
}
```
A possible downside here is that the symfony constraint stuff happens
before types are figured out from PHPDoc. So it's _possible_ to end up
with something that won't validated. Take something like this:
```
/**
* @Assert\Choice(multiple=true, choices={"..."})
* @var string
*/
```
This would generate:
```
"property": {
"type": "string",
"items": {
"enum": ["..."]
}
}
```
* Fix CS
* cs
* more cs
* fix tests
Co-authored-by: Guilhem Niot <guilhem@gniot.fr>
2021-02-19 02:41:32 -06:00
|
|
|
*/
|
|
|
|
private $property1;
|
2021-03-12 00:59:35 +01:00
|
|
|
}];
|
|
|
|
|
|
|
|
if (\PHP_VERSION_ID >= 80000) {
|
|
|
|
yield 'Attributes' => [new class() {
|
|
|
|
#[Assert\Length(min: 1)]
|
|
|
|
#[Assert\Choice(choices: TEST_ASSERT_CHOICE_STATUSES)]
|
|
|
|
private $property1;
|
|
|
|
}];
|
|
|
|
}
|
|
|
|
}
|
Apply `enum` from Choice Constraints to Items When Choice is Multiple (#1784)
* Apply `enum` from Choice Constraints to Items When Choice is Multiple
Otherwise JSON schema like this is generated:
```
"property": {
"type": "array",
"enum": ["one", "two", "three"],
"items": {
"type": "string"
}
}
```
With this change, however, this schema is generated:
```
"property": {
"type": "array",
"items": {
"type": "string",
"enum": ["one", "two", "three"]
}
}
```
A possible downside here is that the symfony constraint stuff happens
before types are figured out from PHPDoc. So it's _possible_ to end up
with something that won't validated. Take something like this:
```
/**
* @Assert\Choice(multiple=true, choices={"..."})
* @var string
*/
```
This would generate:
```
"property": {
"type": "string",
"items": {
"enum": ["..."]
}
}
```
* Fix CS
* cs
* more cs
* fix tests
Co-authored-by: Guilhem Niot <guilhem@gniot.fr>
2021-02-19 02:41:32 -06:00
|
|
|
|
2021-03-12 00:59:35 +01:00
|
|
|
/**
|
|
|
|
* @param object $entity
|
|
|
|
* @dataProvider provideMultipleChoiceConstraintsApplyEnumToItems
|
|
|
|
*/
|
|
|
|
public function testMultipleChoiceConstraintsApplyEnumToItems($entity)
|
|
|
|
{
|
Apply `enum` from Choice Constraints to Items When Choice is Multiple (#1784)
* Apply `enum` from Choice Constraints to Items When Choice is Multiple
Otherwise JSON schema like this is generated:
```
"property": {
"type": "array",
"enum": ["one", "two", "three"],
"items": {
"type": "string"
}
}
```
With this change, however, this schema is generated:
```
"property": {
"type": "array",
"items": {
"type": "string",
"enum": ["one", "two", "three"]
}
}
```
A possible downside here is that the symfony constraint stuff happens
before types are figured out from PHPDoc. So it's _possible_ to end up
with something that won't validated. Take something like this:
```
/**
* @Assert\Choice(multiple=true, choices={"..."})
* @var string
*/
```
This would generate:
```
"property": {
"type": "string",
"items": {
"enum": ["..."]
}
}
```
* Fix CS
* cs
* more cs
* fix tests
Co-authored-by: Guilhem Niot <guilhem@gniot.fr>
2021-02-19 02:41:32 -06:00
|
|
|
$schema = new OA\Schema([]);
|
|
|
|
$schema->merge([new OA\Property(['property' => 'property1'])]);
|
|
|
|
|
|
|
|
$symfonyConstraintAnnotationReader = new SymfonyConstraintAnnotationReader(new AnnotationReader());
|
|
|
|
$symfonyConstraintAnnotationReader->setSchema($schema);
|
|
|
|
|
|
|
|
$symfonyConstraintAnnotationReader->updateProperty(new \ReflectionProperty($entity, 'property1'), $schema->properties[0]);
|
|
|
|
|
|
|
|
$this->assertInstanceOf(OA\Items::class, $schema->properties[0]->items);
|
|
|
|
$this->assertEquals($schema->properties[0]->items->enum, ['one', 'two']);
|
|
|
|
}
|
|
|
|
|
2021-03-12 00:59:35 +01:00
|
|
|
public function provideMultipleChoiceConstraintsApplyEnumToItems(): iterable
|
2021-02-10 10:33:55 -06:00
|
|
|
{
|
2021-03-12 00:59:35 +01:00
|
|
|
yield 'Annotations' => [new class() {
|
2021-02-10 10:33:55 -06:00
|
|
|
/**
|
2021-03-12 00:59:35 +01:00
|
|
|
* @Assert\Choice(choices={"one", "two"}, multiple=true)
|
2021-02-10 10:33:55 -06:00
|
|
|
*/
|
|
|
|
private $property1;
|
2021-03-12 00:59:35 +01:00
|
|
|
}];
|
2021-02-10 10:33:55 -06:00
|
|
|
|
2021-03-12 00:59:35 +01:00
|
|
|
if (\PHP_VERSION_ID >= 80000) {
|
|
|
|
yield 'Attributes' => [new class() {
|
|
|
|
#[Assert\Choice(choices: ['one', 'two'], multiple: true)]
|
|
|
|
private $property1;
|
|
|
|
}];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param object $entity
|
|
|
|
* @group https://github.com/nelmio/NelmioApiDocBundle/issues/1780
|
|
|
|
* @dataProvider provideLengthConstraintDoesNotSetMaxLengthIfMaxIsNotSet
|
|
|
|
*/
|
|
|
|
public function testLengthConstraintDoesNotSetMaxLengthIfMaxIsNotSet($entity)
|
|
|
|
{
|
2021-02-10 10:33:55 -06:00
|
|
|
$schema = new OA\Schema([]);
|
|
|
|
$schema->merge([new OA\Property(['property' => 'property1'])]);
|
|
|
|
|
|
|
|
$symfonyConstraintAnnotationReader = new SymfonyConstraintAnnotationReader(new AnnotationReader());
|
|
|
|
$symfonyConstraintAnnotationReader->setSchema($schema);
|
|
|
|
|
|
|
|
$symfonyConstraintAnnotationReader->updateProperty(new \ReflectionProperty($entity, 'property1'), $schema->properties[0]);
|
|
|
|
|
|
|
|
$this->assertSame(OA\UNDEFINED, $schema->properties[0]->maxLength);
|
|
|
|
$this->assertSame(1, $schema->properties[0]->minLength);
|
|
|
|
}
|
|
|
|
|
2021-03-12 00:59:35 +01:00
|
|
|
public function provideLengthConstraintDoesNotSetMaxLengthIfMaxIsNotSet(): iterable
|
2021-02-10 10:33:55 -06:00
|
|
|
{
|
2021-03-12 00:59:35 +01:00
|
|
|
yield 'Annotations' => [new class() {
|
2021-02-10 10:33:55 -06:00
|
|
|
/**
|
2021-03-12 00:59:35 +01:00
|
|
|
* @Assert\Length(min = 1)
|
2021-02-10 10:33:55 -06:00
|
|
|
*/
|
|
|
|
private $property1;
|
2021-03-12 00:59:35 +01:00
|
|
|
}];
|
2021-02-10 10:33:55 -06:00
|
|
|
|
2021-03-12 00:59:35 +01:00
|
|
|
if (\PHP_VERSION_ID >= 80000) {
|
|
|
|
yield 'Attributes' => [new class() {
|
|
|
|
#[Assert\Length(min: 1)]
|
|
|
|
private $property1;
|
|
|
|
}];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param object $entity
|
|
|
|
* @group https://github.com/nelmio/NelmioApiDocBundle/issues/1780
|
|
|
|
* @dataProvider provideLengthConstraintDoesNotSetMinLengthIfMinIsNotSet
|
|
|
|
*/
|
|
|
|
public function testLengthConstraintDoesNotSetMinLengthIfMinIsNotSet($entity)
|
|
|
|
{
|
2021-02-10 10:33:55 -06:00
|
|
|
$schema = new OA\Schema([]);
|
|
|
|
$schema->merge([new OA\Property(['property' => 'property1'])]);
|
|
|
|
|
|
|
|
$symfonyConstraintAnnotationReader = new SymfonyConstraintAnnotationReader(new AnnotationReader());
|
|
|
|
$symfonyConstraintAnnotationReader->setSchema($schema);
|
|
|
|
|
|
|
|
$symfonyConstraintAnnotationReader->updateProperty(new \ReflectionProperty($entity, 'property1'), $schema->properties[0]);
|
|
|
|
|
|
|
|
$this->assertSame(OA\UNDEFINED, $schema->properties[0]->minLength);
|
|
|
|
$this->assertSame(100, $schema->properties[0]->maxLength);
|
|
|
|
}
|
2021-03-12 00:59:35 +01:00
|
|
|
|
|
|
|
public function provideLengthConstraintDoesNotSetMinLengthIfMinIsNotSet(): iterable
|
|
|
|
{
|
|
|
|
yield 'Annotations' => [new class() {
|
|
|
|
/**
|
|
|
|
* @Assert\Length(max = 100)
|
|
|
|
*/
|
|
|
|
private $property1;
|
|
|
|
}];
|
|
|
|
|
|
|
|
if (\PHP_VERSION_ID >= 80000) {
|
|
|
|
yield 'Attributes' => [new class() {
|
|
|
|
#[Assert\Length(max: 100)]
|
|
|
|
private $property1;
|
|
|
|
}];
|
|
|
|
}
|
|
|
|
}
|
2021-05-25 06:26:27 -05:00
|
|
|
|
2021-06-16 10:59:06 +03:00
|
|
|
public function testCompoundValidationRules()
|
|
|
|
{
|
|
|
|
$entity = new class() {
|
|
|
|
/**
|
|
|
|
* @CustomAssert\CompoundValidationRule()
|
|
|
|
*/
|
|
|
|
private $property1;
|
|
|
|
};
|
|
|
|
$propertyName = 'property1';
|
|
|
|
|
|
|
|
$schema = new OA\Schema([]);
|
|
|
|
$schema->merge([new OA\Property(['property' => $propertyName])]);
|
|
|
|
|
|
|
|
$symfonyConstraintAnnotationReader = new SymfonyConstraintAnnotationReader(new AnnotationReader());
|
|
|
|
$symfonyConstraintAnnotationReader->setSchema($schema);
|
|
|
|
|
|
|
|
$symfonyConstraintAnnotationReader->updateProperty(new \ReflectionProperty($entity, $propertyName), $schema->properties[0]);
|
|
|
|
|
|
|
|
if (Helper::isCompoundValidatorConstraintSupported()) {
|
|
|
|
$this->assertSame([$propertyName], $schema->required);
|
|
|
|
$this->assertSame(0, $schema->properties[0]->minimum);
|
|
|
|
$this->assertTrue($schema->properties[0]->exclusiveMinimum);
|
|
|
|
$this->assertSame(5, $schema->properties[0]->maximum);
|
|
|
|
$this->assertTrue($schema->properties[0]->exclusiveMaximum);
|
|
|
|
} else {
|
|
|
|
$this->assertSame(OA\UNDEFINED, $schema->required);
|
|
|
|
$this->assertSame(OA\UNDEFINED, $schema->properties[0]->minimum);
|
|
|
|
$this->assertSame(OA\UNDEFINED, $schema->properties[0]->exclusiveMinimum);
|
|
|
|
$this->assertSame(OA\UNDEFINED, $schema->properties[0]->maximum);
|
|
|
|
$this->assertSame(OA\UNDEFINED, $schema->properties[0]->exclusiveMaximum);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-25 06:26:27 -05:00
|
|
|
/**
|
|
|
|
* @param object $entity
|
|
|
|
* @group https://github.com/nelmio/NelmioApiDocBundle/issues/1821
|
|
|
|
* @dataProvider provideCountConstraintDoesNotSetMinItemsIfMinIsNotSet
|
|
|
|
*/
|
|
|
|
public function testCountConstraintDoesNotSetMinItemsIfMinIsNotSet($entity)
|
|
|
|
{
|
|
|
|
$schema = new OA\Schema([]);
|
|
|
|
$schema->merge([new OA\Property(['property' => 'property1'])]);
|
|
|
|
|
|
|
|
$symfonyConstraintAnnotationReader = new SymfonyConstraintAnnotationReader(new AnnotationReader());
|
|
|
|
$symfonyConstraintAnnotationReader->setSchema($schema);
|
|
|
|
|
|
|
|
$symfonyConstraintAnnotationReader->updateProperty(new \ReflectionProperty($entity, 'property1'), $schema->properties[0]);
|
|
|
|
|
|
|
|
$this->assertSame(OA\UNDEFINED, $schema->properties[0]->minItems);
|
|
|
|
$this->assertSame(10, $schema->properties[0]->maxItems);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function provideCountConstraintDoesNotSetMinItemsIfMinIsNotSet(): iterable
|
|
|
|
{
|
|
|
|
yield 'Annotations' => [new class() {
|
|
|
|
/**
|
|
|
|
* @Assert\Count(max = 10)
|
|
|
|
*/
|
|
|
|
private $property1;
|
|
|
|
}];
|
|
|
|
|
|
|
|
if (\PHP_VERSION_ID >= 80000) {
|
|
|
|
yield 'Attributes' => [new class() {
|
|
|
|
#[Assert\Count(max: 10)]
|
|
|
|
private $property1;
|
|
|
|
}];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param object $entity
|
|
|
|
* @group https://github.com/nelmio/NelmioApiDocBundle/issues/1821
|
|
|
|
* @dataProvider provideCountConstraintDoesNotSetMaxItemsIfMaxIsNotSet
|
|
|
|
*/
|
|
|
|
public function testCountConstraintDoesNotSetMaxItemsIfMaxIsNotSet($entity)
|
|
|
|
{
|
|
|
|
$schema = new OA\Schema([]);
|
|
|
|
$schema->merge([new OA\Property(['property' => 'property1'])]);
|
|
|
|
|
|
|
|
$symfonyConstraintAnnotationReader = new SymfonyConstraintAnnotationReader(new AnnotationReader());
|
|
|
|
$symfonyConstraintAnnotationReader->setSchema($schema);
|
|
|
|
|
|
|
|
$symfonyConstraintAnnotationReader->updateProperty(new \ReflectionProperty($entity, 'property1'), $schema->properties[0]);
|
|
|
|
|
|
|
|
$this->assertSame(OA\UNDEFINED, $schema->properties[0]->maxItems);
|
|
|
|
$this->assertSame(10, $schema->properties[0]->minItems);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function provideCountConstraintDoesNotSetMaxItemsIfMaxIsNotSet(): iterable
|
|
|
|
{
|
|
|
|
yield 'Annotations' => [new class() {
|
|
|
|
/**
|
|
|
|
* @Assert\Count(min = 10)
|
|
|
|
*/
|
|
|
|
private $property1;
|
|
|
|
}];
|
|
|
|
|
|
|
|
if (\PHP_VERSION_ID >= 80000) {
|
|
|
|
yield 'Attributes' => [new class() {
|
|
|
|
#[Assert\Count(min: 10)]
|
|
|
|
private $property1;
|
|
|
|
}];
|
|
|
|
}
|
|
|
|
}
|
2021-05-25 06:39:21 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param object $entity
|
|
|
|
* @group https://github.com/nelmio/NelmioApiDocBundle/issues/1822
|
|
|
|
* @dataProvider provideRangeConstraintDoesNotSetMaximumIfMaxIsNotSet
|
|
|
|
*/
|
|
|
|
public function testRangeConstraintDoesNotSetMaximumIfMaxIsNotSet($entity)
|
|
|
|
{
|
|
|
|
$schema = new OA\Schema([]);
|
|
|
|
$schema->merge([new OA\Property(['property' => 'property1'])]);
|
|
|
|
|
|
|
|
$symfonyConstraintAnnotationReader = new SymfonyConstraintAnnotationReader(new AnnotationReader());
|
|
|
|
$symfonyConstraintAnnotationReader->setSchema($schema);
|
|
|
|
|
|
|
|
$symfonyConstraintAnnotationReader->updateProperty(new \ReflectionProperty($entity, 'property1'), $schema->properties[0]);
|
|
|
|
|
|
|
|
$this->assertSame(OA\UNDEFINED, $schema->properties[0]->maximum);
|
|
|
|
$this->assertSame(10, $schema->properties[0]->minimum);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function provideRangeConstraintDoesNotSetMaximumIfMaxIsNotSet(): iterable
|
|
|
|
{
|
|
|
|
yield 'Annotations' => [new class() {
|
|
|
|
/**
|
|
|
|
* @Assert\Range(min = 10)
|
|
|
|
*/
|
|
|
|
private $property1;
|
|
|
|
}];
|
|
|
|
|
|
|
|
if (\PHP_VERSION_ID >= 80000) {
|
|
|
|
yield 'Attributes' => [new class() {
|
|
|
|
#[Assert\Range(min: 10)]
|
|
|
|
private $property1;
|
|
|
|
}];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param object $entity
|
|
|
|
* @group https://github.com/nelmio/NelmioApiDocBundle/issues/1822
|
|
|
|
* @dataProvider provideRangeConstraintDoesNotSetMinimumIfMinIsNotSet
|
|
|
|
*/
|
|
|
|
public function testRangeConstraintDoesNotSetMinimumIfMinIsNotSet($entity)
|
|
|
|
{
|
|
|
|
$schema = new OA\Schema([]);
|
|
|
|
$schema->merge([new OA\Property(['property' => 'property1'])]);
|
|
|
|
|
|
|
|
$symfonyConstraintAnnotationReader = new SymfonyConstraintAnnotationReader(new AnnotationReader());
|
|
|
|
$symfonyConstraintAnnotationReader->setSchema($schema);
|
|
|
|
|
|
|
|
$symfonyConstraintAnnotationReader->updateProperty(new \ReflectionProperty($entity, 'property1'), $schema->properties[0]);
|
|
|
|
|
|
|
|
$this->assertSame(OA\UNDEFINED, $schema->properties[0]->minimum);
|
|
|
|
$this->assertSame(10, $schema->properties[0]->maximum);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function provideRangeConstraintDoesNotSetMinimumIfMinIsNotSet(): iterable
|
|
|
|
{
|
|
|
|
yield 'Annotations' => [new class() {
|
|
|
|
/**
|
|
|
|
* @Assert\Range(max = 10)
|
|
|
|
*/
|
|
|
|
private $property1;
|
|
|
|
}];
|
|
|
|
|
|
|
|
if (\PHP_VERSION_ID >= 80000) {
|
|
|
|
yield 'Attributes' => [new class() {
|
|
|
|
#[Assert\Range(max: 10)]
|
|
|
|
private $property1;
|
|
|
|
}];
|
|
|
|
}
|
|
|
|
}
|
2021-11-06 06:16:15 -05:00
|
|
|
|
|
|
|
/**
|
|
|
|
* re-using another provider here, since all constraints land in the default
|
|
|
|
* group when `group={"someGroup"}` is not set.
|
|
|
|
*
|
|
|
|
* @group https://github.com/nelmio/NelmioApiDocBundle/issues/1857
|
|
|
|
* @dataProvider provideRangeConstraintDoesNotSetMinimumIfMinIsNotSet
|
|
|
|
*/
|
|
|
|
public function testReaderWithValidationGroupsEnabledChecksForDefaultGroupWhenNoSerializationGroupsArePassed($entity)
|
|
|
|
{
|
|
|
|
$schema = new OA\Schema([]);
|
|
|
|
$schema->merge([new OA\Property(['property' => 'property1'])]);
|
|
|
|
$reader = $this->createConstraintReaderWithValidationGroupsEnabled();
|
|
|
|
$reader->setSchema($schema);
|
|
|
|
|
|
|
|
// no serialization groups passed here
|
|
|
|
$reader->updateProperty(
|
|
|
|
new \ReflectionProperty($entity, 'property1'),
|
|
|
|
$schema->properties[0]
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->assertSame(10, $schema->properties[0]->maximum, 'should have read constraints in the default group');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @group https://github.com/nelmio/NelmioApiDocBundle/issues/1857
|
|
|
|
* @dataProvider provideConstraintsWithGroups
|
|
|
|
*/
|
|
|
|
public function testReaderWithValidationGroupsEnabledDoesNotReadAnnotationsWithoutDefaultGroupIfNoGroupsArePassed($entity)
|
|
|
|
{
|
|
|
|
$schema = new OA\Schema([]);
|
|
|
|
$schema->merge([
|
|
|
|
new OA\Property(['property' => 'property1']),
|
|
|
|
]);
|
|
|
|
$reader = $this->createConstraintReaderWithValidationGroupsEnabled();
|
|
|
|
$reader->setSchema($schema);
|
|
|
|
|
|
|
|
// no serialization groups passed here
|
|
|
|
$reader->updateProperty(
|
|
|
|
new \ReflectionProperty($entity, 'property1'),
|
|
|
|
$schema->properties[0]
|
|
|
|
);
|
|
|
|
|
|
|
|
$this->assertSame(['property1'], $schema->required, 'should have read constraint in default group');
|
|
|
|
$this->assertSame(OA\UNDEFINED, $schema->properties[0]->minimum, 'should not have read constraint in other group');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @group https://github.com/nelmio/NelmioApiDocBundle/issues/1857
|
|
|
|
* @dataProvider provideConstraintsWithGroups
|
|
|
|
*/
|
|
|
|
public function testReaderWithValidationGroupsEnabledReadsOnlyConstraintsWithGroupsProvided($entity)
|
|
|
|
{
|
|
|
|
$schema = new OA\Schema([]);
|
|
|
|
$schema->merge([
|
|
|
|
new OA\Property(['property' => 'property1']),
|
|
|
|
]);
|
|
|
|
$reader = $this->createConstraintReaderWithValidationGroupsEnabled();
|
|
|
|
$reader->setSchema($schema);
|
|
|
|
|
|
|
|
// no serialization groups passed here
|
|
|
|
$reader->updateProperty(
|
|
|
|
new \ReflectionProperty($entity, 'property1'),
|
|
|
|
$schema->properties[0],
|
2021-11-06 07:35:50 -05:00
|
|
|
['other'],
|
2021-11-06 06:16:15 -05:00
|
|
|
);
|
|
|
|
|
|
|
|
$this->assertSame(OA\UNDEFINED, $schema->required, 'should not have read constraint in default group');
|
|
|
|
$this->assertSame(1, $schema->properties[0]->minimum, 'should have read constraint in other group');
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @group https://github.com/nelmio/NelmioApiDocBundle/issues/1857
|
|
|
|
* @dataProvider provideConstraintsWithGroups
|
|
|
|
*/
|
|
|
|
public function testReaderWithValidationGroupsEnabledCanReadFromMultipleValidationGroups($entity)
|
|
|
|
{
|
|
|
|
$schema = new OA\Schema([]);
|
|
|
|
$schema->merge([
|
|
|
|
new OA\Property(['property' => 'property1']),
|
|
|
|
]);
|
|
|
|
$reader = $this->createConstraintReaderWithValidationGroupsEnabled();
|
|
|
|
$reader->setSchema($schema);
|
|
|
|
|
|
|
|
// no serialization groups passed here
|
|
|
|
$reader->updateProperty(
|
|
|
|
new \ReflectionProperty($entity, 'property1'),
|
|
|
|
$schema->properties[0],
|
2021-11-06 07:35:50 -05:00
|
|
|
['other', Constraint::DEFAULT_GROUP],
|
2021-11-06 06:16:15 -05:00
|
|
|
);
|
|
|
|
|
|
|
|
$this->assertSame(['property1'], $schema->required, 'should have read constraint in default group');
|
|
|
|
$this->assertSame(1, $schema->properties[0]->minimum, 'should have read constraint in other group');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function provideConstraintsWithGroups(): iterable
|
|
|
|
{
|
|
|
|
yield 'Annotations' => [new class() {
|
|
|
|
/**
|
|
|
|
* @Assert\NotBlank()
|
|
|
|
* @Assert\Range(min=1, groups={"other"})
|
|
|
|
*/
|
|
|
|
private $property1;
|
|
|
|
}];
|
|
|
|
|
|
|
|
if (\PHP_VERSION_ID >= 80000) {
|
|
|
|
yield 'Attributes' => [new class() {
|
|
|
|
#[Assert\NotBlank()]
|
2021-11-06 07:35:50 -05:00
|
|
|
#[Assert\Range(min: 1, group: ['other'])]
|
2021-11-06 06:16:15 -05:00
|
|
|
private $property1;
|
|
|
|
}];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-06 07:35:50 -05:00
|
|
|
private function createConstraintReaderWithValidationGroupsEnabled(): SymfonyConstraintAnnotationReader
|
2021-11-06 06:16:15 -05:00
|
|
|
{
|
|
|
|
return new SymfonyConstraintAnnotationReader(
|
|
|
|
new AnnotationReader(),
|
|
|
|
true
|
|
|
|
);
|
|
|
|
}
|
2018-04-06 18:44:20 +01:00
|
|
|
}
|