diff --git a/Tests/Functional/Controller/ApiController.php b/Tests/Functional/Controller/ApiController.php index 68a3bcf..7889404 100644 --- a/Tests/Functional/Controller/ApiController.php +++ b/Tests/Functional/Controller/ApiController.php @@ -17,6 +17,7 @@ use Nelmio\ApiDocBundle\Annotation\Model; use Nelmio\ApiDocBundle\Annotation\Operation; use Nelmio\ApiDocBundle\Annotation\Security; use Nelmio\ApiDocBundle\Tests\Functional\Entity\Article; +use Nelmio\ApiDocBundle\Tests\Functional\Entity\SymfonyConstraints; use Nelmio\ApiDocBundle\Tests\Functional\Entity\User; use Nelmio\ApiDocBundle\Tests\Functional\Form\DummyType; use Nelmio\ApiDocBundle\Tests\Functional\Form\UserType; @@ -168,4 +169,16 @@ class ApiController public function securityAction() { } + + /** + * @Route("/swagger/symfonyConstraints", methods={"GET"}) + * @SWG\Response( + * response="201", + * description="Used for symfony constraints test", + * @Model(type=SymfonyConstraints::class) + * ) + */ + public function symfonyConstraintsAction() + { + } } diff --git a/Tests/Functional/Entity/JMSUser.php b/Tests/Functional/Entity/JMSUser.php index 7eb5cab..7a7dc7e 100644 --- a/Tests/Functional/Entity/JMSUser.php +++ b/Tests/Functional/Entity/JMSUser.php @@ -13,7 +13,6 @@ namespace Nelmio\ApiDocBundle\Tests\Functional\Entity; use JMS\Serializer\Annotation as Serializer; use Swagger\Annotations as SWG; -use Symfony\Component\Validator\Constraints as Assert; /** * User. @@ -28,8 +27,6 @@ class JMSUser * @Serializer\Groups({"list"}) * * @SWG\Property(description = "User id", readOnly = true, title = "userid", example=1, default = null) - * - * @Assert\NotBlank() */ private $id; diff --git a/Tests/Functional/Entity/SymfonyConstraints.php b/Tests/Functional/Entity/SymfonyConstraints.php new file mode 100644 index 0000000..13d2e08 --- /dev/null +++ b/Tests/Functional/Entity/SymfonyConstraints.php @@ -0,0 +1,140 @@ +propertyNotBlank = $propertyNotBlank; + } + + /** + * @param int $propertyNotNull + */ + public function setPropertyNotNull(int $propertyNotNull): void + { + $this->propertyNotNull = $propertyNotNull; + } + + /** + * @param int $propertyAssertLengthRequired + */ + public function setPropertyAssertLengthRequired(int $propertyAssertLengthRequired): void + { + $this->propertyAssertLengthRequired = $propertyAssertLengthRequired; + } + + /** + * @param int $propertyAssertLengthMinAndMax + */ + public function setPropertyAssertLengthMinAndMax(int $propertyAssertLengthMinAndMax): void + { + $this->propertyAssertLengthMinAndMax = $propertyAssertLengthMinAndMax; + } + + /** + * @param int $propertyRegex + */ + public function setPropertyRegex(int $propertyRegex): void + { + $this->propertyRegex = $propertyRegex; + } + + /** + * @param int $propertyCount + */ + public function setPropertyCount(int $propertyCount): void + { + $this->propertyCount = $propertyCount; + } + + /** + * @param int $propertyChoice + */ + public function setPropertyChoice(int $propertyChoice): void + { + $this->propertyChoice = $propertyChoice; + } + + /** + * @param int $propertyExpression + */ + public function setPropertyExpression(int $propertyExpression): void + { + $this->propertyExpression = $propertyExpression; + } +} diff --git a/Tests/Functional/FunctionalTest.php b/Tests/Functional/FunctionalTest.php index 90c531e..1965719 100644 --- a/Tests/Functional/FunctionalTest.php +++ b/Tests/Functional/FunctionalTest.php @@ -11,7 +11,6 @@ namespace Nelmio\ApiDocBundle\Tests\Functional; -use EXSyst\Component\Swagger\Operation; use EXSyst\Component\Swagger\Tag; use Nelmio\ApiDocBundle\Tests\Functional\Form\DummyType; @@ -256,4 +255,50 @@ class FunctionalTest extends WebTestCase ]; $this->assertEquals($expected, $operation->getSecurity()); } + + public function testSymfonyConstraintDocumentation() + { + $this->assertEquals([ + 'required' => [ + 'propertyNotBlank', + 'propertyNotNull', + 'propertyAssertLengthRequired', + ], + 'properties' => [ + 'propertyNotBlank' => [ + 'type' => 'integer', + ], + 'propertyNotNull' => [ + 'type' => 'integer', + ], + 'propertyAssertLengthRequired' => [ + 'type' => 'integer', + 'minLength' => '1', + ], + 'propertyAssertLengthMinAndMax' => [ + 'type' => 'integer', + 'maxLength' => '50', + 'minLength' => '0', + ], + 'propertyRegex' => [ + 'type' => 'integer', + 'pattern' => '.*[a-z]{2}.*', + ], + 'propertyCount' => [ + 'type' => 'integer', + 'maxItems' => '10', + 'minItems' => '0', + ], + 'propertyChoice' => [ + 'type' => 'integer', + 'enum' => ['choice1', 'choice2'], + ], + 'propertyExpression' => [ + 'type' => 'integer', + 'pattern' => 'If this is a tech post, the category should be either php or symfony!', + ], + ], + 'type' => 'object', + ], $this->getModel('SymfonyConstraints')->toArray()); + } } diff --git a/Tests/Functional/JMSFunctionalTest.php b/Tests/Functional/JMSFunctionalTest.php index 73b3035..c8863a7 100644 --- a/Tests/Functional/JMSFunctionalTest.php +++ b/Tests/Functional/JMSFunctionalTest.php @@ -67,9 +67,6 @@ class JMSFunctionalTest extends WebTestCase 'type' => 'date', ], ], - 'required' => [ - 'id', - ], ], $this->getModel('JMSUser')->toArray()); } @@ -78,7 +75,7 @@ class JMSFunctionalTest extends WebTestCase $this->assertEquals([ 'type' => 'object', 'properties' => [ - 'id' => ['type' => 'integer'], + 'id' => ['type' => 'integer'], 'user' => ['$ref' => '#/definitions/JMSUser2'], 'name' => ['type' => 'string'], ], @@ -99,9 +96,6 @@ class JMSFunctionalTest extends WebTestCase 'example' => '1', ], ], - 'required' => [ - 'id', - ], ], $this->getModel('JMSUser2')->toArray()); }