AssertLength doesn't make the field required (#1389)

This commit is contained in:
Guilhem N 2018-08-30 00:32:11 +02:00 committed by GitHub
parent f0b1eb7849
commit fcb36d8e8d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 18 additions and 62 deletions

View File

@ -44,36 +44,28 @@ class SymfonyConstraintAnnotationReader
foreach ($annotations as $annotation) { foreach ($annotations as $annotation) {
if ($annotation instanceof Assert\NotBlank || $annotation instanceof Assert\NotNull) { if ($annotation instanceof Assert\NotBlank || $annotation instanceof Assert\NotNull) {
$this->updateSchemaDefinitionWithRequiredProperty($reflectionProperty); // The field is required
if (null === $this->schema) {
continue;
} }
if ($annotation instanceof Assert\Length) { $existingRequiredFields = $this->schema->getRequired() ?? [];
if ($annotation->min > 0) { $existingRequiredFields[] = $reflectionProperty->getName();
$this->updateSchemaDefinitionWithRequiredProperty($reflectionProperty);
}
$this->schema->setRequired(array_values(array_unique($existingRequiredFields)));
} elseif ($annotation instanceof Assert\Length) {
$property->setMinLength($annotation->min); $property->setMinLength($annotation->min);
$property->setMaxLength($annotation->max); $property->setMaxLength($annotation->max);
} } elseif ($annotation instanceof Assert\Regex) {
if ($annotation instanceof Assert\Regex) {
$this->appendPattern($property, $annotation->getHtmlPattern()); $this->appendPattern($property, $annotation->getHtmlPattern());
} } elseif ($annotation instanceof Assert\DateTime) {
if ($annotation instanceof Assert\DateTime) {
$this->appendPattern($property, $annotation->format); $this->appendPattern($property, $annotation->format);
} } elseif ($annotation instanceof Assert\Count) {
if ($annotation instanceof Assert\Count) {
$property->setMinItems($annotation->min); $property->setMinItems($annotation->min);
$property->setMaxItems($annotation->max); $property->setMaxItems($annotation->max);
} } elseif ($annotation instanceof Assert\Choice) {
if ($annotation instanceof Assert\Choice) {
$property->setEnum($annotation->callback ? call_user_func($annotation->callback) : $annotation->choices); $property->setEnum($annotation->callback ? call_user_func($annotation->callback) : $annotation->choices);
} } elseif ($annotation instanceof Assert\Expression) {
if ($annotation instanceof Assert\Expression) {
$this->appendPattern($property, $annotation->message); $this->appendPattern($property, $annotation->message);
} }
} }
@ -84,22 +76,6 @@ class SymfonyConstraintAnnotationReader
$this->schema = $schema; $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. * Append the pattern from the constraint to the existing pattern.
*/ */

View File

@ -29,19 +29,12 @@ class SymfonyConstraints
*/ */
private $propertyNotNull; private $propertyNotNull;
/**
* @var int
*
* @Assert\Length(min="1")
*/
private $propertyAssertLengthRequired;
/** /**
* @var int * @var int
* *
* @Assert\Length(min="0", max="50") * @Assert\Length(min="0", max="50")
*/ */
private $propertyAssertLengthMinAndMax; private $propertyAssertLength;
/** /**
* @var int * @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; $this->propertyAssertLength = $propertyAssertLength;
}
/**
* @param int $propertyAssertLengthMinAndMax
*/
public function setPropertyAssertLengthMinAndMax(int $propertyAssertLengthMinAndMax): void
{
$this->propertyAssertLengthMinAndMax = $propertyAssertLengthMinAndMax;
} }
/** /**

View File

@ -339,7 +339,6 @@ class FunctionalTest extends WebTestCase
'required' => [ 'required' => [
'propertyNotBlank', 'propertyNotBlank',
'propertyNotNull', 'propertyNotNull',
'propertyAssertLengthRequired',
], ],
'properties' => [ 'properties' => [
'propertyNotBlank' => [ 'propertyNotBlank' => [
@ -348,11 +347,7 @@ class FunctionalTest extends WebTestCase
'propertyNotNull' => [ 'propertyNotNull' => [
'type' => 'integer', 'type' => 'integer',
], ],
'propertyAssertLengthRequired' => [ 'propertyAssertLength' => [
'type' => 'integer',
'minLength' => '1',
],
'propertyAssertLengthMinAndMax' => [
'type' => 'integer', 'type' => 'integer',
'maxLength' => '50', 'maxLength' => '50',
'minLength' => '0', 'minLength' => '0',