Skip to content

Commit

Permalink
Merge pull request #1823 from chrisguitarguy/fix_constraints
Browse files Browse the repository at this point in the history
Fix Count and Range Constraints
  • Loading branch information
GuilhemN authored May 27, 2021
2 parents 1899a9e + 5f1645d commit 7f3926c
Show file tree
Hide file tree
Showing 2 changed files with 156 additions and 4 deletions.
16 changes: 12 additions & 4 deletions ModelDescriber/Annotations/SymfonyConstraintAnnotationReader.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,13 +76,21 @@ public function updateProperty($reflection, OA\Property $property): void
} elseif ($annotation instanceof Assert\Regex) {
$this->appendPattern($property, $annotation->getHtmlPattern());
} elseif ($annotation instanceof Assert\Count) {
$property->minItems = (int) $annotation->min;
$property->maxItems = (int) $annotation->max;
if (isset($annotation->min)) {
$property->minItems = (int) $annotation->min;
}
if (isset($annotation->max)) {
$property->maxItems = (int) $annotation->max;
}
} elseif ($annotation instanceof Assert\Choice) {
$this->applyEnumFromChoiceConstraint($property, $annotation, $reflection);
} elseif ($annotation instanceof Assert\Range) {
$property->minimum = (int) $annotation->min;
$property->maximum = (int) $annotation->max;
if (isset($annotation->min)) {
$property->minimum = (int) $annotation->min;
}
if (isset($annotation->max)) {
$property->maximum = (int) $annotation->max;
}
} elseif ($annotation instanceof Assert\LessThan) {
$property->exclusiveMaximum = true;
$property->maximum = (int) $annotation->value;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -244,4 +244,148 @@ public function provideLengthConstraintDoesNotSetMinLengthIfMinIsNotSet(): itera
}];
}
}

/**
* @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;
}];
}
}

/**
* @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;
}];
}
}
}

0 comments on commit 7f3926c

Please sign in to comment.