Skip to content

Commit

Permalink
Add array checks for nested paths of variable
Browse files Browse the repository at this point in the history
  • Loading branch information
digitv committed Dec 15, 2021
1 parent 2189e8c commit 47e0936
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 7 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"description": "Swagger yaml generator",
"keywords": ["api", "swagger", "open auth"],
"license": "MIT",
"version": "1.0.17",
"version": "1.0.18",
"authors": [
{
"name": "Digit",
Expand Down
26 changes: 22 additions & 4 deletions oa/BaseValueDescribed.php
Original file line number Diff line number Diff line change
Expand Up @@ -109,12 +109,15 @@ public function getNestedPaths()
}
$namePartsParent = $nameParts;
array_pop($namePartsParent);
$path = $this->makePathFromKeysArray($nameParts);
$pathParent = $this->makePathFromKeysArray($namePartsParent);
$lastName = last($nameParts);

return [
implode('.properties.', $nameParts), // Full path
implode('.properties.', $namePartsParent), // Path to the parent
last($nameParts), // Deepest variable name
implode('.', $namePartsParent) // Parent name
$path, // Full path
$pathParent, // Path to the parent
$lastName !== '*' ? $lastName : null, // Deepest variable name
implode('.', $namePartsParent), // Parent name
];
}

Expand Down Expand Up @@ -260,6 +263,21 @@ protected function hasEnum()
return is_array($this->enum) && !empty($this->enum);
}

/**
* Make a path from keys array.
*
* @param array $keys
* @return string
*/
protected function makePathFromKeysArray(array $keys)
{
$first = array_shift($keys);
$keys = array_map(fn ($k) => $k === '*' ? '.items' : '.properties.' . $k, $keys);
array_unshift($keys, $first);

return implode('', $keys);
}

/**
* Get example with check by enum
*
Expand Down
9 changes: 7 additions & 2 deletions src/Yaml/Variable.php
Original file line number Diff line number Diff line change
Expand Up @@ -405,9 +405,14 @@ protected function getDescriptionByPropertyAnnotations($className, $only = [], $
$annotation->isNested()
&& ([$nestedPath, $nestedParentPath, $nestedName] = $annotation->getNestedPaths())
&& $nestedParentPath !== null
&& Arr::get($result, $nestedParentPath . '.type') === static::SW_TYPE_OBJECT
&& ($nestedParentType = Arr::get($result, $nestedParentPath . '.type')) !== null
&& in_array($nestedParentType, [static::SW_TYPE_ARRAY, static::SW_TYPE_OBJECT], true)
) {
$rowData['name'] = $nestedName;
if ($nestedParentType === static::SW_TYPE_ARRAY && in_array($nestedName, ['*', null], true)) {
unset($rowData['name']);
} elseif ($nestedParentType === static::SW_TYPE_OBJECT) {
$rowData['name'] = $nestedName;
}
Arr::set($result, $nestedPath, $rowData);
continue;
}
Expand Down

0 comments on commit 47e0936

Please sign in to comment.