From 3623bdb1eabaaf4145e1525440a42f25a0af600e Mon Sep 17 00:00:00 2001 From: Guilhem Niot Date: Wed, 17 Jun 2020 14:12:44 +0200 Subject: [PATCH] Fix the Util class manipulation of array of objects (#1653) * Fix the Util class manipulation of array of objects * fix cs --- OpenApiPhp/Util.php | 26 +++++++++++++++++++++----- Tests/Functional/TestKernel.php | 11 +++++++++++ 2 files changed, 32 insertions(+), 5 deletions(-) diff --git a/OpenApiPhp/Util.php b/OpenApiPhp/Util.php index 1473431..0065173 100644 --- a/OpenApiPhp/Util.php +++ b/OpenApiPhp/Util.php @@ -455,11 +455,27 @@ final class Util private static function mergeTyped(OA\AbstractAnnotation $annotation, $propertyName, $type, array $properties, array $defaults, bool $overwrite) { if (\is_string($type) && 0 === strpos($type, '[')) { - /* type is declared as array in @see OA\AbstractAnnotation::$_types */ - $annotation->{$propertyName} = array_unique(array_merge( - $annotation->{$propertyName} && UNDEFINED !== $annotation->{$propertyName} ? $annotation->{$propertyName} : [], - $properties[$propertyName] - )); + $innerType = substr($type, 1, -1); + + if (!$annotation->{$propertyName} || UNDEFINED === $annotation->{$propertyName}) { + $annotation->{$propertyName} = []; + } + + if (!class_exists($innerType)) { + /* type is declared as array in @see OA\AbstractAnnotation::$_types */ + $annotation->{$propertyName} = array_unique(array_merge( + $annotation->{$propertyName}, + $properties[$propertyName] + )); + + return; + } + + // $type == [Schema] for instance + foreach ($properties[$propertyName] as $child) { + $annotation->{$propertyName}[] = $annot = self::createChild($annotation, $innerType, []); + self::merge($annot, $child, $overwrite); + } } else { self::mergeProperty($annotation, $propertyName, $properties[$propertyName], $defaults[$propertyName], $overwrite); } diff --git a/Tests/Functional/TestKernel.php b/Tests/Functional/TestKernel.php index 8f5c7a7..6b9dbf6 100644 --- a/Tests/Functional/TestKernel.php +++ b/Tests/Functional/TestKernel.php @@ -176,6 +176,17 @@ class TestKernel extends Kernel 'Test' => [ 'type' => 'string', ], + + // Ensures https://github.com/nelmio/NelmioApiDocBundle/issues/1650 is working + 'Pet' => [ + 'type' => 'object', + ], + 'Cat' => [ + 'allOf' => [ + ['$ref' => '#/components/schemas/Pet'], + ['type' => 'object'], + ], + ], ], 'parameters' => [ 'test' => [