fix(specs): swagger add nullable property
This commit is contained in:
parent
c927b1a93b
commit
eb7c9dd020
|
@ -20,34 +20,6 @@ class OpenAPI3 extends Format
|
|||
return 'Open API 3';
|
||||
}
|
||||
|
||||
/**
|
||||
* Get Used Models
|
||||
*
|
||||
* Recursively get all used models
|
||||
*
|
||||
* @param object $model
|
||||
* @param array $models
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
protected function getUsedModels($model, array &$usedModels)
|
||||
{
|
||||
if (is_string($model) && !in_array($model, ['string', 'integer', 'boolean', 'json', 'float', 'double'])) {
|
||||
$usedModels[] = $model;
|
||||
return;
|
||||
}
|
||||
if (!is_object($model)) return;
|
||||
foreach ($model->getRules() as $rule) {
|
||||
if(\is_array($rule['type'])) {
|
||||
foreach ($rule['type'] as $type) {
|
||||
$this->getUsedModels($type, $usedModels);
|
||||
}
|
||||
} else {
|
||||
$this->getUsedModels($rule['type'], $usedModels);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Parse
|
||||
*
|
||||
|
@ -422,7 +394,11 @@ class OpenAPI3 extends Format
|
|||
$output['paths'][$url][\strtolower($route->getMethod())] = $temp;
|
||||
}
|
||||
foreach ($this->models as $model) {
|
||||
$this->getUsedModels($model, $usedModels);
|
||||
foreach ($model->getRules() as $rule) {
|
||||
if (!in_array($rule['type'], ['string', 'integer', 'boolean', 'json', 'float'])) {
|
||||
$usedModels[] = $rule['type'];
|
||||
}
|
||||
}
|
||||
}
|
||||
foreach ($this->models as $model) {
|
||||
if (!in_array($model->getType(), $usedModels) && $model->getType() !== 'error') {
|
||||
|
@ -526,7 +502,6 @@ class OpenAPI3 extends Format
|
|||
$output['components']['schemas'][$model->getType()]['properties'][$name] = [
|
||||
'type' => $type,
|
||||
'description' => $rule['description'] ?? '',
|
||||
//'default' => $rule['default'] ?? null,
|
||||
'x-example' => $rule['example'] ?? null,
|
||||
];
|
||||
|
||||
|
@ -538,6 +513,9 @@ class OpenAPI3 extends Format
|
|||
$output['components']['schemas'][$model->getType()]['properties'][$name]['items'] = $items;
|
||||
}
|
||||
}
|
||||
if (!in_array($name, $required)) {
|
||||
$output['components']['schemas'][$model->getType()]['properties'][$name]['nullable'] = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -20,34 +20,6 @@ class Swagger2 extends Format
|
|||
return 'Swagger 2';
|
||||
}
|
||||
|
||||
/**
|
||||
* Get Used Models
|
||||
*
|
||||
* Recursively get all used models
|
||||
*
|
||||
* @param object $model
|
||||
* @param array $models
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
protected function getUsedModels($model, array &$usedModels)
|
||||
{
|
||||
if (is_string($model) && !in_array($model, ['string', 'integer', 'boolean', 'json', 'float', 'double'])) {
|
||||
$usedModels[] = $model;
|
||||
return;
|
||||
}
|
||||
if (!is_object($model)) return;
|
||||
foreach ($model->getRules() as $rule) {
|
||||
if(\is_array($rule['type'])) {
|
||||
foreach ($rule['type'] as $type) {
|
||||
$this->getUsedModels($type, $usedModels);
|
||||
}
|
||||
} else {
|
||||
$this->getUsedModels($rule['type'], $usedModels);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Parse
|
||||
*
|
||||
|
@ -308,7 +280,7 @@ class Swagger2 extends Format
|
|||
case 'Utopia\Validator\Mock':
|
||||
case 'Utopia\Validator\Assoc':
|
||||
$node['type'] = 'object';
|
||||
$param['default'] = (empty($param['default'])) ? new \stdClass() : $param['default'];
|
||||
$node['default'] = (empty($param['default'])) ? new \stdClass() : $param['default'];
|
||||
$node['x-example'] = '{}';
|
||||
//$node['format'] = 'json';
|
||||
break;
|
||||
|
@ -379,14 +351,14 @@ class Swagger2 extends Format
|
|||
$temp['parameters'][] = $node;
|
||||
} else { // Param is in payload
|
||||
|
||||
if(\in_array('multipart/form-data', $consumes)) {
|
||||
if (\in_array('multipart/form-data', $consumes)) {
|
||||
$node['in'] = 'formData';
|
||||
$temp['parameters'][] = $node;
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
if(!$param['optional']) {
|
||||
if (!$param['optional']) {
|
||||
$bodyRequired[] = $name;
|
||||
}
|
||||
|
||||
|
@ -417,8 +389,16 @@ class Swagger2 extends Format
|
|||
|
||||
$output['paths'][$url][\strtolower($route->getMethod())] = $temp;
|
||||
}
|
||||
|
||||
foreach ($this->models as $model) {
|
||||
$this->getUsedModels($model, $usedModels);
|
||||
foreach ($model->getRules() as $rule) {
|
||||
if (
|
||||
in_array($model->getType(), $usedModels)
|
||||
&& !in_array($rule['type'], ['string', 'integer', 'boolean', 'json', 'float'])
|
||||
) {
|
||||
$usedModels[] = $rule['type'];
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
foreach ($this->models as $model) {
|
||||
|
@ -536,6 +516,9 @@ class Swagger2 extends Format
|
|||
$output['definitions'][$model->getType()]['properties'][$name]['items'] = $items;
|
||||
}
|
||||
}
|
||||
if (!in_array($name, $required)) {
|
||||
$output['definitions'][$model->getType()]['properties'][$name]['nullable'] = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -92,7 +92,7 @@ abstract class Model
|
|||
$list = [];
|
||||
|
||||
foreach ($this->rules as $key => $rule) {
|
||||
if (isset($rule['require']) || $rule['require']) {
|
||||
if ($rule['require'] ?? false) {
|
||||
$list[] = $key;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue