From 3b2d9da6c24462112053a0d0ea28fb1f5592ea24 Mon Sep 17 00:00:00 2001 From: Jerzy Lekowski Date: Fri, 6 Apr 2018 18:44:20 +0100 Subject: [PATCH] Fix #1283: Invalid swagger.json generation - definition for entities with Assert\NotBlank and Assert\Length --- .../SymfonyConstraintAnnotationReader.php | 2 +- .../SymfonyConstraintAnnotationReaderTest.php | 48 +++++++++++++++++++ 2 files changed, 49 insertions(+), 1 deletion(-) create mode 100644 Tests/ModelDescriber/Annotations/SymfonyConstraintAnnotationReaderTest.php diff --git a/ModelDescriber/Annotations/SymfonyConstraintAnnotationReader.php b/ModelDescriber/Annotations/SymfonyConstraintAnnotationReader.php index 3b93f8e2b..722844e62 100644 --- a/ModelDescriber/Annotations/SymfonyConstraintAnnotationReader.php +++ b/ModelDescriber/Annotations/SymfonyConstraintAnnotationReader.php @@ -97,7 +97,7 @@ private function updateSchemaDefinitionWithRequiredProperty(\ReflectionProperty $existingRequiredFields[] = $reflectionProperty->getName(); - $this->schema->setRequired(array_unique($existingRequiredFields)); + $this->schema->setRequired(array_values(array_unique($existingRequiredFields))); } /** diff --git a/Tests/ModelDescriber/Annotations/SymfonyConstraintAnnotationReaderTest.php b/Tests/ModelDescriber/Annotations/SymfonyConstraintAnnotationReaderTest.php new file mode 100644 index 000000000..dadbd6f17 --- /dev/null +++ b/Tests/ModelDescriber/Annotations/SymfonyConstraintAnnotationReaderTest.php @@ -0,0 +1,48 @@ +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']); + } +}