Skip to content

Commit

Permalink
Merge branch '3.x'
Browse files Browse the repository at this point in the history
  • Loading branch information
GuilhemN committed Sep 9, 2020
2 parents 5b6983e + ab8c44f commit 865527b
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,11 @@ public function updateProperty($reflection, OA\Property $property): void

foreach ($annotations as $annotation) {
if ($annotation instanceof Assert\NotBlank || $annotation instanceof Assert\NotNull) {
if ($annotation instanceof Assert\NotBlank && $annotation->allowNull) {
// The field is optional
continue;
}

// The field is required
if (null === $this->schema) {
continue;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,34 @@ public function testUpdatePropertyFix1283()
$this->assertEquals($schema->required, ['property1', 'property2']);
}

public function testOptionalProperty()
{
$entity = new class() {
/**
* @Assert\NotBlank(allowNull = true)
* @Assert\Length(min = 1)
*/
private $property1;
/**
* @Assert\NotBlank()
*/
private $property2;
};

$schema = new Schema();
$schema->getProperties()->set('property1', new Schema());
$schema->getProperties()->set('property2', new Schema());

$symfonyConstraintAnnotationReader = new SymfonyConstraintAnnotationReader(new AnnotationReader());
$symfonyConstraintAnnotationReader->setSchema($schema);

$symfonyConstraintAnnotationReader->updateProperty(new \ReflectionProperty($entity, 'property1'), $schema->getProperties()->get('property1'));
$symfonyConstraintAnnotationReader->updateProperty(new \ReflectionProperty($entity, 'property2'), $schema->getProperties()->get('property2'));

// expect required to be numeric array with sequential keys (not [0 => ..., 2 => ...])
$this->assertEquals($schema->getRequired(), ['property2']);
}

public function testAssertChoiceResultsInNumericArray()
{
define('TEST_ASSERT_CHOICE_STATUSES', [
Expand Down

0 comments on commit 865527b

Please sign in to comment.