Skip to content

Commit

Permalink
Fix displaying array of objects in api docs (#11)
Browse files Browse the repository at this point in the history
* Fix displaying array of objects in api docs

* Add comments

---------

Co-authored-by: Bartosz Urbaniak <[email protected]>
  • Loading branch information
barturb and Bartosz Urbaniak authored Aug 31, 2023
1 parent 0a4e19e commit 705bc05
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
17 changes: 14 additions & 3 deletions src/Service/Nelmio/DtoOADescriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -230,9 +230,17 @@ private function resolveProperties(
]);

if (is_array($model)) {
$property->properties = $this->resolveProperties($model);
$property->type = 'object';
$property->required = $this->resolveRequiredProperties($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([]);
$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;

Expand Down Expand Up @@ -338,6 +346,9 @@ private function getClassBags(
}

if ($item instanceof DtoTypeModel) {
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));

continue;
Expand Down
1 change: 1 addition & 0 deletions src/Service/Validation/TypeValidationHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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))) {
Expand Down

0 comments on commit 705bc05

Please sign in to comment.