diff --git a/app/controllers/mock.php b/app/controllers/mock.php index d9cd03f28..2c5245184 100644 --- a/app/controllers/mock.php +++ b/app/controllers/mock.php @@ -6,9 +6,9 @@ use Appwrite\Database\Document; use Appwrite\Network\Validator\Host; use Appwrite\Utopia\Response; use Utopia\App; -use Utopia\Validator\Numeric; -use Utopia\Validator\Text; use Utopia\Validator\ArrayList; +use Utopia\Validator\Integer; +use Utopia\Validator\Text; use Utopia\Storage\Validator\File; App::get('/v1/mock/tests/foo') @@ -24,7 +24,7 @@ App::get('/v1/mock/tests/foo') ->label('sdk.response.model', Response::MODEL_MOCK) ->label('sdk.mock', true) ->param('x', '', new Text(100), 'Sample string param') - ->param('y', '', new Numeric(), 'Sample numeric param') + ->param('y', '', new Integer(), 'Sample numeric param') ->param('z', null, new ArrayList(new Text(256)), 'Sample array param') ->action(function ($x, $y, $z) { }); @@ -42,7 +42,7 @@ App::post('/v1/mock/tests/foo') ->label('sdk.response.model', Response::MODEL_MOCK) ->label('sdk.mock', true) ->param('x', '', new Text(100), 'Sample string param') - ->param('y', '', new Numeric(), 'Sample numeric param') + ->param('y', '', new Integer(), 'Sample numeric param') ->param('z', null, new ArrayList(new Text(256)), 'Sample array param') ->action(function ($x, $y, $z) { }); @@ -60,7 +60,7 @@ App::patch('/v1/mock/tests/foo') ->label('sdk.response.model', Response::MODEL_MOCK) ->label('sdk.mock', true) ->param('x', '', new Text(100), 'Sample string param') - ->param('y', '', new Numeric(), 'Sample numeric param') + ->param('y', '', new Integer(), 'Sample numeric param') ->param('z', null, new ArrayList(new Text(256)), 'Sample array param') ->action(function ($x, $y, $z) { }); @@ -78,7 +78,7 @@ App::put('/v1/mock/tests/foo') ->label('sdk.response.model', Response::MODEL_MOCK) ->label('sdk.mock', true) ->param('x', '', new Text(100), 'Sample string param') - ->param('y', '', new Numeric(), 'Sample numeric param') + ->param('y', '', new Integer(), 'Sample numeric param') ->param('z', null, new ArrayList(new Text(256)), 'Sample array param') ->action(function ($x, $y, $z) { }); @@ -96,7 +96,7 @@ App::delete('/v1/mock/tests/foo') ->label('sdk.response.model', Response::MODEL_MOCK) ->label('sdk.mock', true) ->param('x', '', new Text(100), 'Sample string param') - ->param('y', '', new Numeric(), 'Sample numeric param') + ->param('y', '', new Integer(), 'Sample numeric param') ->param('z', null, new ArrayList(new Text(256)), 'Sample array param') ->action(function ($x, $y, $z) { }); @@ -114,7 +114,7 @@ App::get('/v1/mock/tests/bar') ->label('sdk.response.model', Response::MODEL_MOCK) ->label('sdk.mock', true) ->param('x', '', new Text(100), 'Sample string param') - ->param('y', '', new Numeric(), 'Sample numeric param') + ->param('y', '', new Integer(), 'Sample numeric param') ->param('z', null, new ArrayList(new Text(256)), 'Sample array param') ->action(function ($x, $y, $z) { }); @@ -132,7 +132,7 @@ App::post('/v1/mock/tests/bar') ->label('sdk.response.model', Response::MODEL_MOCK) ->label('sdk.mock', true) ->param('x', '', new Text(100), 'Sample string param') - ->param('y', '', new Numeric(), 'Sample numeric param') + ->param('y', '', new Integer(), 'Sample numeric param') ->param('z', null, new ArrayList(new Text(256)), 'Sample array param') ->action(function ($x, $y, $z) { }); @@ -150,7 +150,7 @@ App::patch('/v1/mock/tests/bar') ->label('sdk.response.model', Response::MODEL_MOCK) ->label('sdk.mock', true) ->param('x', '', new Text(100), 'Sample string param') - ->param('y', '', new Numeric(), 'Sample numeric param') + ->param('y', '', new Integer(), 'Sample numeric param') ->param('z', null, new ArrayList(new Text(256)), 'Sample array param') ->action(function ($x, $y, $z) { }); @@ -168,7 +168,7 @@ App::put('/v1/mock/tests/bar') ->label('sdk.response.model', Response::MODEL_MOCK) ->label('sdk.mock', true) ->param('x', '', new Text(100), 'Sample string param') - ->param('y', '', new Numeric(), 'Sample numeric param') + ->param('y', '', new Integer(), 'Sample numeric param') ->param('z', null, new ArrayList(new Text(256)), 'Sample array param') ->action(function ($x, $y, $z) { }); @@ -186,7 +186,7 @@ App::delete('/v1/mock/tests/bar') ->label('sdk.response.model', Response::MODEL_MOCK) ->label('sdk.mock', true) ->param('x', '', new Text(100), 'Sample string param') - ->param('y', '', new Numeric(), 'Sample numeric param') + ->param('y', '', new Integer(), 'Sample numeric param') ->param('z', null, new ArrayList(new Text(256)), 'Sample array param') ->action(function ($x, $y, $z) { }); @@ -205,7 +205,7 @@ App::post('/v1/mock/tests/general/upload') ->label('sdk.response.model', Response::MODEL_MOCK) ->label('sdk.mock', true) ->param('x', '', new Text(100), 'Sample string param') - ->param('y', '', new Numeric(), 'Sample numeric param') + ->param('y', '', new Integer(), 'Sample numeric param') ->param('z', null, new ArrayList(new Text(256)), 'Sample array param') ->param('file', [], new File(), 'Sample file param', false) ->inject('request') @@ -246,6 +246,7 @@ App::get('/v1/mock/tests/general/redirect') ->label('sdk.description', 'Mock a redirect request.') ->label('sdk.response.code', Response::STATUS_CODE_MOVED_PERMANENTLY) ->label('sdk.response.type', Response::CONTENT_TYPE_HTML) + ->label('sdk.response.model', Response::MODEL_MOCK) ->label('sdk.mock', true) ->inject('response') ->action(function ($response) { @@ -369,6 +370,7 @@ App::get('/v1/mock/tests/general/502-error') ->label('sdk.description', 'Mock a 502 bad gateway.') ->label('sdk.response.code', Response::STATUS_CODE_BAD_GATEWAY) ->label('sdk.response.type', Response::CONTENT_TYPE_TEXT) + ->label('sdk.response.model', Response::MODEL_ANY) ->label('sdk.mock', true) ->inject('response') ->action(function ($response) { diff --git a/app/controllers/web/home.php b/app/controllers/web/home.php index d76d8da05..6e4544fd7 100644 --- a/app/controllers/web/home.php +++ b/app/controllers/web/home.php @@ -8,6 +8,7 @@ use Utopia\App; use Utopia\View; use Utopia\Config\Config; use Utopia\Exception; +use Utopia\Validator\Boolean; use Utopia\Validator\Range; use Utopia\Validator\WhiteList; @@ -241,10 +242,11 @@ App::get('/specs/:format') ->param('format', 'swagger2', new WhiteList(['swagger2', 'open-api3'], true), 'Spec format.', true) ->param('platform', APP_PLATFORM_CLIENT, new WhiteList([APP_PLATFORM_CLIENT, APP_PLATFORM_SERVER, APP_PLATFORM_CONSOLE], true), 'Choose target platform.', true) ->param('tests', 0, function () {return new Range(0, 1);}, 'Include only test services.', true) + ->param('allModels', false, new Boolean(true), 'Also include unused models.', true) ->inject('utopia') ->inject('request') ->inject('response') - ->action(function ($format, $platform, $tests, $utopia, $request, $response) { + ->action(function ($format, $platform, $tests, $allModels, $utopia, $request, $response) { /** @var Utopia\App $utopia */ /** @var Utopia\Swoole\Request $request */ /** @var Appwrite\Utopia\Response $response */ @@ -425,11 +427,11 @@ App::get('/specs/:format') switch ($format) { case 'swagger2': - $format = new Swagger2($utopia, $services, $routes, $models, $keys[$platform], $authCounts[$platform] ?? 0); + $format = new Swagger2($utopia, $services, $routes, $models, $keys[$platform], $authCounts[$platform] ?? 0, $allModels); break; case 'open-api3': - $format = new OpenAPI3($utopia, $services, $routes, $models, $keys[$platform], $authCounts[$platform] ?? 0); + $format = new OpenAPI3($utopia, $services, $routes, $models, $keys[$platform], $authCounts[$platform] ?? 0, $allModels); break; default: diff --git a/src/Appwrite/Specification/Format.php b/src/Appwrite/Specification/Format.php index 62447444f..b61eb7956 100644 --- a/src/Appwrite/Specification/Format.php +++ b/src/Appwrite/Specification/Format.php @@ -27,6 +27,11 @@ abstract class Format * @var Model[] */ protected $models; + + /** + * @var bool + */ + protected $includeUnusedModels; /** * @var array @@ -62,8 +67,10 @@ abstract class Format * @param Route[] $routes * @param Model[] $models * @param array $keys + * @param int $authCount + * @param bool $includeUnusedModels */ - public function __construct(App $app, array $services, array $routes, array $models, array $keys, int $authCount) + public function __construct(App $app, array $services, array $routes, array $models, array $keys, int $authCount, bool $includeUnusedModels = false) { $this->app = $app; $this->services = $services; @@ -71,6 +78,7 @@ abstract class Format $this->models = $models; $this->keys = $keys; $this->authCount = $authCount; + $this->includeUnusedModels = $includeUnusedModels; } /** diff --git a/src/Appwrite/Specification/Format/Swagger2.php b/src/Appwrite/Specification/Format/Swagger2.php index c63be0a70..442df921b 100644 --- a/src/Appwrite/Specification/Format/Swagger2.php +++ b/src/Appwrite/Specification/Format/Swagger2.php @@ -4,7 +4,6 @@ namespace Appwrite\Specification\Format; use Appwrite\Specification\Format; use Appwrite\Template\Template; -use Appwrite\Utopia\Response\Model; use stdClass; class Swagger2 extends Format @@ -187,7 +186,7 @@ class Swagger2 extends Format ]; } - if($route->getLabel('sdk.response.code', 500) === 204) { + if(in_array($route->getLabel('sdk.response.code', 500), [204, 301, 302, 308], true)) { $temp['responses'][(string)$route->getLabel('sdk.response.code', '500')]['description'] = 'No content'; unset($temp['responses'][(string)$route->getLabel('sdk.response.code', '500')]['schema']); } @@ -217,7 +216,7 @@ class Swagger2 extends Format $bodyRequired = []; foreach ($route->getParams() as $name => $param) { // Set params - $validator = (\is_callable($param['validator'])) ? call_user_func_array($param['validator'], $this->app->getResources($param['injections'])) : $param['validator']; /* @var $validator \Utopia\Validator */ + $validator = (\is_callable($param['validator'])) ? call_user_func_array($param['validator'], $this->app->getResources($param['injections'])) : $param['validator']; /** @var \Utopia\Validator $validator */ $node = [ 'name' => $name, @@ -278,6 +277,7 @@ class Swagger2 extends Format $node['x-example'] = $validator->getMin(); break; case 'Utopia\Validator\Numeric': + case 'Utopia\Validator\Integer': $node['type'] = 'integer'; $node['format'] = 'int32'; break; @@ -354,13 +354,16 @@ class Swagger2 extends Format } foreach ($this->models as $model) { foreach ($model->getRules() as $rule) { - if (!in_array($rule['type'], ['string', 'integer', 'boolean', 'json', 'float'])) { + if ( + in_array($model->getType(), $usedModels) + && !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') { + if (!$this->includeUnusedModels && !in_array($model->getType(), $usedModels)) { continue; }