From 925cc0b188499be2a06adb66c1ae3461a52bee8c Mon Sep 17 00:00:00 2001 From: Bartosz Urbaniak Date: Tue, 29 Aug 2023 11:28:38 +0200 Subject: [PATCH 1/2] Fix displaying array of objects in api docs --- src/Service/Nelmio/DtoOADescriber.php | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/src/Service/Nelmio/DtoOADescriber.php b/src/Service/Nelmio/DtoOADescriber.php index 77aa973..b70fbc4 100644 --- a/src/Service/Nelmio/DtoOADescriber.php +++ b/src/Service/Nelmio/DtoOADescriber.php @@ -230,9 +230,16 @@ private function resolveProperties( ]); if (is_array($model)) { - $property->properties = $this->resolveProperties($model); - $property->type = 'object'; - $property->required = $this->resolveRequiredProperties($model); + if (is_array($model[''] ?? null)) { + $property->type = "array"; + $property->items = new Items([]); + $property->items->properties = $this->resolveProperties($model['']); + $property->items->required = $this->resolveRequiredProperties($model['']); + } else { + $property->type = 'object'; + $property->properties = $this->resolveProperties($model); + $property->required = $this->resolveRequiredProperties($model); + } } else { $assignTo = $property; @@ -338,6 +345,9 @@ private function getClassBags( } if ($item instanceof DtoTypeModel) { + if ($item->isCollection()) { + $propertyPath .= '.'; + } $bags = DtoUtil::mergeRecursively($bags, $this->getClassBags($item, $propertyPath)); continue; From 88a6d86e15e8560b4ae145fd110313f506295cf2 Mon Sep 17 00:00:00 2001 From: Bartosz Urbaniak Date: Thu, 31 Aug 2023 10:55:46 +0200 Subject: [PATCH 2/2] Add comments --- src/Service/Nelmio/DtoOADescriber.php | 3 ++- src/Service/Validation/TypeValidationHelper.php | 1 + 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/src/Service/Nelmio/DtoOADescriber.php b/src/Service/Nelmio/DtoOADescriber.php index b70fbc4..cfac802 100644 --- a/src/Service/Nelmio/DtoOADescriber.php +++ b/src/Service/Nelmio/DtoOADescriber.php @@ -230,6 +230,7 @@ private function resolveProperties( ]); if (is_array($model)) { + // checking whether there is an empty index by which you can distinguish whether the model is a collection of objects or a flat object if (is_array($model[''] ?? null)) { $property->type = "array"; $property->items = new Items([]); @@ -345,7 +346,7 @@ private function getClassBags( } if ($item instanceof DtoTypeModel) { - if ($item->isCollection()) { + if ($item->isCollection()) { // forces an empty index to make it easier to check whether an element is an array $propertyPath .= '.'; } $bags = DtoUtil::mergeRecursively($bags, $this->getClassBags($item, $propertyPath)); diff --git a/src/Service/Validation/TypeValidationHelper.php b/src/Service/Validation/TypeValidationHelper.php index a28714f..6d8af3b 100644 --- a/src/Service/Validation/TypeValidationHelper.php +++ b/src/Service/Validation/TypeValidationHelper.php @@ -42,6 +42,7 @@ public function validateType( } $list = new ConstraintViolationList(); + // coerce and copy sub-values foreach ($subValues as $key => $value) { if (null === ($result = $this->coercionService->coerce($key, $subProperties[$key], $value))) {