diff --git a/ModelDescriber/Annotations/SymfonyConstraintAnnotationReader.php b/ModelDescriber/Annotations/SymfonyConstraintAnnotationReader.php index c4df7cd..01661a5 100644 --- a/ModelDescriber/Annotations/SymfonyConstraintAnnotationReader.php +++ b/ModelDescriber/Annotations/SymfonyConstraintAnnotationReader.php @@ -44,36 +44,28 @@ class SymfonyConstraintAnnotationReader foreach ($annotations as $annotation) { if ($annotation instanceof Assert\NotBlank || $annotation instanceof Assert\NotNull) { - $this->updateSchemaDefinitionWithRequiredProperty($reflectionProperty); - } - - if ($annotation instanceof Assert\Length) { - if ($annotation->min > 0) { - $this->updateSchemaDefinitionWithRequiredProperty($reflectionProperty); + // The field is required + if (null === $this->schema) { + continue; } + $existingRequiredFields = $this->schema->getRequired() ?? []; + $existingRequiredFields[] = $reflectionProperty->getName(); + + $this->schema->setRequired(array_values(array_unique($existingRequiredFields))); + } elseif ($annotation instanceof Assert\Length) { $property->setMinLength($annotation->min); $property->setMaxLength($annotation->max); - } - - if ($annotation instanceof Assert\Regex) { + } elseif ($annotation instanceof Assert\Regex) { $this->appendPattern($property, $annotation->getHtmlPattern()); - } - - if ($annotation instanceof Assert\DateTime) { + } elseif ($annotation instanceof Assert\DateTime) { $this->appendPattern($property, $annotation->format); - } - - if ($annotation instanceof Assert\Count) { + } elseif ($annotation instanceof Assert\Count) { $property->setMinItems($annotation->min); $property->setMaxItems($annotation->max); - } - - if ($annotation instanceof Assert\Choice) { + } elseif ($annotation instanceof Assert\Choice) { $property->setEnum($annotation->callback ? call_user_func($annotation->callback) : $annotation->choices); - } - - if ($annotation instanceof Assert\Expression) { + } elseif ($annotation instanceof Assert\Expression) { $this->appendPattern($property, $annotation->message); } } @@ -84,22 +76,6 @@ class SymfonyConstraintAnnotationReader $this->schema = $schema; } - /** - * Set the required properties on the scheme. - */ - private function updateSchemaDefinitionWithRequiredProperty(\ReflectionProperty $reflectionProperty) - { - if (null === $this->schema) { - return; - } - - $existingRequiredFields = $this->schema->getRequired() ?? []; - - $existingRequiredFields[] = $reflectionProperty->getName(); - - $this->schema->setRequired(array_values(array_unique($existingRequiredFields))); - } - /** * Append the pattern from the constraint to the existing pattern. */ diff --git a/Tests/Functional/Entity/SymfonyConstraints.php b/Tests/Functional/Entity/SymfonyConstraints.php index 6bea33e..23610f4 100644 --- a/Tests/Functional/Entity/SymfonyConstraints.php +++ b/Tests/Functional/Entity/SymfonyConstraints.php @@ -29,19 +29,12 @@ class SymfonyConstraints */ private $propertyNotNull; - /** - * @var int - * - * @Assert\Length(min="1") - */ - private $propertyAssertLengthRequired; - /** * @var int * * @Assert\Length(min="0", max="50") */ - private $propertyAssertLengthMinAndMax; + private $propertyAssertLength; /** * @var int @@ -98,19 +91,11 @@ class SymfonyConstraints } /** - * @param int $propertyAssertLengthRequired + * @param int $propertyAssertLength */ - public function setPropertyAssertLengthRequired(int $propertyAssertLengthRequired): void + public function setPropertyAssertLength(int $propertyAssertLength): void { - $this->propertyAssertLengthRequired = $propertyAssertLengthRequired; - } - - /** - * @param int $propertyAssertLengthMinAndMax - */ - public function setPropertyAssertLengthMinAndMax(int $propertyAssertLengthMinAndMax): void - { - $this->propertyAssertLengthMinAndMax = $propertyAssertLengthMinAndMax; + $this->propertyAssertLength = $propertyAssertLength; } /** diff --git a/Tests/Functional/FunctionalTest.php b/Tests/Functional/FunctionalTest.php index 36e8add..95d8071 100644 --- a/Tests/Functional/FunctionalTest.php +++ b/Tests/Functional/FunctionalTest.php @@ -339,7 +339,6 @@ class FunctionalTest extends WebTestCase 'required' => [ 'propertyNotBlank', 'propertyNotNull', - 'propertyAssertLengthRequired', ], 'properties' => [ 'propertyNotBlank' => [ @@ -348,11 +347,7 @@ class FunctionalTest extends WebTestCase 'propertyNotNull' => [ 'type' => 'integer', ], - 'propertyAssertLengthRequired' => [ - 'type' => 'integer', - 'minLength' => '1', - ], - 'propertyAssertLengthMinAndMax' => [ + 'propertyAssertLength' => [ 'type' => 'integer', 'maxLength' => '50', 'minLength' => '0',