Fix #1283: Invalid swagger.json generation - definition for entities with Assert\NotBlank and Assert\Length

This commit is contained in:
Jerzy Lekowski 2018-04-06 18:44:20 +01:00
parent ff590b013c
commit 3b2d9da6c2
2 changed files with 49 additions and 1 deletions

View File

@ -97,7 +97,7 @@ class SymfonyConstraintAnnotationReader
$existingRequiredFields[] = $reflectionProperty->getName();
$this->schema->setRequired(array_unique($existingRequiredFields));
$this->schema->setRequired(array_values(array_unique($existingRequiredFields)));
}
/**

View File

@ -0,0 +1,48 @@
<?php
/*
* This file is part of the NelmioApiDocBundle package.
*
* (c) Nelmio
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Nelmio\ApiDocBundle\Tests\ModelDescriber\Annotations;
use Doctrine\Common\Annotations\AnnotationReader;
use EXSyst\Component\Swagger\Schema;
use Nelmio\ApiDocBundle\ModelDescriber\Annotations\SymfonyConstraintAnnotationReader;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Validator\Constraints as Assert;
class SymfonyConstraintAnnotationReaderTest extends TestCase
{
public function testUpdatePropertyFix1283()
{
$entity = new class() {
/**
* @Assert\NotBlank()
* @Assert\Length(min = 1)
*/
private $property1;
/**
* @Assert\NotBlank()
*/
private $property2;
};
$reflectionProperties = (new \ReflectionClass($entity))->getProperties();
$property = new Schema();
$schema = new Schema();
$symfonyConstraintAnnotationReader = new SymfonyConstraintAnnotationReader(new AnnotationReader());
$symfonyConstraintAnnotationReader->setSchema($schema);
foreach ($reflectionProperties as $reflectionProperty) {
$symfonyConstraintAnnotationReader->updateProperty($reflectionProperty, $property);
}
// expect required to be numeric array with sequential keys (not [0 => ..., 2 => ...])
$this->assertEquals($schema->getRequired(), ['property1', 'property2']);
}
}