mirror of
https://github.com/retailcrm/NelmioApiDocBundle.git
synced 2025-02-09 02:59:27 +03:00
Check if min
and max
Are Set on Count Constraints
It's possible to set a count constraint with a min but no max, which would generate an OpenAPI Schema like... "property": { "type": "array", "minItems": 1, "maxItems": 0 } With this change the schema will only set `minItems` in that example.
This commit is contained in:
parent
1899a9e7ba
commit
7fd8c0ecfe
@ -76,8 +76,12 @@ class SymfonyConstraintAnnotationReader
|
||||
} 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) {
|
||||
|
@ -244,4 +244,76 @@ class SymfonyConstraintAnnotationReaderTest extends TestCase
|
||||
}];
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @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;
|
||||
}];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user