From 82aa08e5ffa45fad42edc9edf365c7d1d7f06101 Mon Sep 17 00:00:00 2001 From: katinsv Date: Fri, 13 Mar 2020 16:59:56 +0300 Subject: [PATCH 1/2] Fix Assert\Choice when choices are object not array --- .../Annotations/SymfonyConstraintAnnotationReader.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/ModelDescriber/Annotations/SymfonyConstraintAnnotationReader.php b/ModelDescriber/Annotations/SymfonyConstraintAnnotationReader.php index db25d0d..73f8449 100644 --- a/ModelDescriber/Annotations/SymfonyConstraintAnnotationReader.php +++ b/ModelDescriber/Annotations/SymfonyConstraintAnnotationReader.php @@ -67,7 +67,8 @@ class SymfonyConstraintAnnotationReader $property->setMinItems($annotation->min); $property->setMaxItems($annotation->max); } elseif ($annotation instanceof Assert\Choice) { - $property->setEnum($annotation->callback ? call_user_func(is_array($annotation->callback) ? $annotation->callback : [$reflectionProperty->class, $annotation->callback]) : $annotation->choices); + $values = $annotation->callback ? call_user_func(is_array($annotation->callback) ? $annotation->callback : [$reflectionProperty->class, $annotation->callback]) : $annotation->choices; + $property->setEnum(array_values($values)); } elseif ($annotation instanceof Assert\Expression) { $this->appendPattern($property, $annotation->message); } elseif ($annotation instanceof Assert\Range) { From 97530a6eeec6b815f455e142060f86814d386792 Mon Sep 17 00:00:00 2001 From: katin-dev Date: Sat, 14 Mar 2020 15:36:59 +0300 Subject: [PATCH 2/2] #1601 test for Assert\Choice results in numeric array --- .../SymfonyConstraintAnnotationReaderTest.php | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/Tests/ModelDescriber/Annotations/SymfonyConstraintAnnotationReaderTest.php b/Tests/ModelDescriber/Annotations/SymfonyConstraintAnnotationReaderTest.php index de60e24..55ea9ec 100644 --- a/Tests/ModelDescriber/Annotations/SymfonyConstraintAnnotationReaderTest.php +++ b/Tests/ModelDescriber/Annotations/SymfonyConstraintAnnotationReaderTest.php @@ -46,4 +46,31 @@ class SymfonyConstraintAnnotationReaderTest extends TestCase // expect required to be numeric array with sequential keys (not [0 => ..., 2 => ...]) $this->assertEquals($schema->getRequired(), ['property1', 'property2']); } + + public function testAssertChoiceResultsInNumericArray() + { + define('TEST_ASSERT_CHOICE_STATUSES', [ + 1 => 'active', + 2 => 'blocked', + ]); + + $entity = new class() { + /** + * @Assert\Length(min = 1) + * @Assert\Choice(choices=TEST_ASSERT_CHOICE_STATUSES) + */ + private $property1; + }; + + $schema = new Schema(); + $schema->getProperties()->set('property1', new Schema()); + + $symfonyConstraintAnnotationReader = new SymfonyConstraintAnnotationReader(new AnnotationReader()); + $symfonyConstraintAnnotationReader->setSchema($schema); + + $symfonyConstraintAnnotationReader->updateProperty(new \ReflectionProperty($entity, 'property1'), $schema->getProperties()->get('property1')); + + // expect enum to be numeric array with sequential keys (not [1 => "active", 2 => "active"]) + $this->assertEquals($schema->getProperties()->get('property1')->getEnum(), ['active', 'blocked']); + } }