From 995ade1c8d08264c6bdf91f6749c2ab00e5965db Mon Sep 17 00:00:00 2001 From: Pavel Batanov Date: Tue, 11 Sep 2018 13:42:50 +0300 Subject: [PATCH 1/2] Respect property name for constraints --- .../SymfonyConstraintAnnotationReader.php | 25 ++++++++++++++- Tests/Functional/Controller/JMSController.php | 13 ++++++++ .../Entity/JMSNamingStrategyConstraints.php | 31 +++++++++++++++++++ Tests/Functional/JMSFunctionalTest.php | 15 +++++++++ 4 files changed, 83 insertions(+), 1 deletion(-) create mode 100644 Tests/Functional/Entity/JMSNamingStrategyConstraints.php diff --git a/ModelDescriber/Annotations/SymfonyConstraintAnnotationReader.php b/ModelDescriber/Annotations/SymfonyConstraintAnnotationReader.php index 01661a5..15b70ce 100644 --- a/ModelDescriber/Annotations/SymfonyConstraintAnnotationReader.php +++ b/ModelDescriber/Annotations/SymfonyConstraintAnnotationReader.php @@ -49,8 +49,13 @@ class SymfonyConstraintAnnotationReader continue; } + $propertyName = $this->getSchemaPropertyName($property); + if (null === $propertyName) { + continue; + } + $existingRequiredFields = $this->schema->getRequired() ?? []; - $existingRequiredFields[] = $reflectionProperty->getName(); + $existingRequiredFields[] = $propertyName; $this->schema->setRequired(array_values(array_unique($existingRequiredFields))); } elseif ($annotation instanceof Assert\Length) { @@ -76,6 +81,24 @@ class SymfonyConstraintAnnotationReader $this->schema = $schema; } + /** + * Get assigned property name for property schema. + */ + private function getSchemaPropertyName(Schema $property) + { + if (null === $this->schema) { + return null; + } + + foreach ($this->schema->getProperties() as $name => $schemaProperty) { + if ($schemaProperty === $property) { + return $name; + } + } + + return null; + } + /** * Append the pattern from the constraint to the existing pattern. */ diff --git a/Tests/Functional/Controller/JMSController.php b/Tests/Functional/Controller/JMSController.php index fea410a..e80388f 100644 --- a/Tests/Functional/Controller/JMSController.php +++ b/Tests/Functional/Controller/JMSController.php @@ -14,6 +14,7 @@ namespace Nelmio\ApiDocBundle\Tests\Functional\Controller; use Nelmio\ApiDocBundle\Annotation\Model; use Nelmio\ApiDocBundle\Tests\Functional\Entity\JMSComplex; use Nelmio\ApiDocBundle\Tests\Functional\Entity\JMSDualComplex; +use Nelmio\ApiDocBundle\Tests\Functional\Entity\JMSNamingStrategyConstraints; use Nelmio\ApiDocBundle\Tests\Functional\Entity\JMSUser; use Nelmio\ApiDocBundle\Tests\Functional\Entity\NestedGroup\JMSChat; use Nelmio\ApiDocBundle\Tests\Functional\Entity\NestedGroup\JMSChatUser; @@ -75,6 +76,18 @@ class JMSController { } + /** + * @Route("/api/jms_naming_strategy", methods={"GET"}) + * @SWG\Response( + * response=200, + * description="Success", + * @Model(type=JMSNamingStrategyConstraints::class, groups={"Default"}) + * ) + */ + public function namingStrategyConstraintsAction() + { + } + /** * @Route("/api/jms_chat", methods={"GET"}) * @SWG\Response( diff --git a/Tests/Functional/Entity/JMSNamingStrategyConstraints.php b/Tests/Functional/Entity/JMSNamingStrategyConstraints.php new file mode 100644 index 0000000..106ac35 --- /dev/null +++ b/Tests/Functional/Entity/JMSNamingStrategyConstraints.php @@ -0,0 +1,31 @@ +some_weird_named_property; + } + + public function setSomeWeirdNamedProperty(string $some_weird_named_property) + { + $this->some_weird_named_property = $some_weird_named_property; + } +} diff --git a/Tests/Functional/JMSFunctionalTest.php b/Tests/Functional/JMSFunctionalTest.php index 3f55371..957b442 100644 --- a/Tests/Functional/JMSFunctionalTest.php +++ b/Tests/Functional/JMSFunctionalTest.php @@ -237,6 +237,21 @@ class JMSFunctionalTest extends WebTestCase ], $this->getModel('VirtualProperty')->toArray()); } + public function testNamingStrategyWithConstraints() + { + $this->assertEquals([ + 'type' => 'object', + 'properties' => [ + 'beautifulName' => [ + 'type' => 'string', + 'maxLength' => '10', + 'minLength' => '3', + ], + ], + 'required' => ['beautifulName'], + ], $this->getModel('JMSNamingStrategyConstraints')->toArray()); + } + protected static function createKernel(array $options = []) { return new TestKernel(true); From 4902627badccd2de4553c7406b7140d4eec2167e Mon Sep 17 00:00:00 2001 From: Pavel Batanov Date: Tue, 11 Sep 2018 13:57:22 +0300 Subject: [PATCH 2/2] Make SymfonyConstraintAnnotationReaderTest use proper calls to reader --- .../SymfonyConstraintAnnotationReaderTest.php | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/Tests/ModelDescriber/Annotations/SymfonyConstraintAnnotationReaderTest.php b/Tests/ModelDescriber/Annotations/SymfonyConstraintAnnotationReaderTest.php index dadbd6f..de60e24 100644 --- a/Tests/ModelDescriber/Annotations/SymfonyConstraintAnnotationReaderTest.php +++ b/Tests/ModelDescriber/Annotations/SymfonyConstraintAnnotationReaderTest.php @@ -32,15 +32,16 @@ class SymfonyConstraintAnnotationReaderTest extends TestCase */ private $property2; }; - $reflectionProperties = (new \ReflectionClass($entity))->getProperties(); - $property = new Schema(); + $schema = new Schema(); + $schema->getProperties()->set('property1', new Schema()); + $schema->getProperties()->set('property2', new Schema()); $symfonyConstraintAnnotationReader = new SymfonyConstraintAnnotationReader(new AnnotationReader()); $symfonyConstraintAnnotationReader->setSchema($schema); - foreach ($reflectionProperties as $reflectionProperty) { - $symfonyConstraintAnnotationReader->updateProperty($reflectionProperty, $property); - } + + $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(), ['property1', 'property2']);