Added open-api
This commit is contained in:
parent
8bbf394d4a
commit
40ca93c4d0
|
@ -57,7 +57,7 @@ class OpenAPI3 extends Format
|
||||||
'paths' => [],
|
'paths' => [],
|
||||||
'components' => [
|
'components' => [
|
||||||
'schemas' => [],
|
'schemas' => [],
|
||||||
'securitySchemes' => $this->security,
|
'securitySchemes' => $this->keys,
|
||||||
],
|
],
|
||||||
'externalDocs' => [
|
'externalDocs' => [
|
||||||
'description' => $this->getParam('docs.description'),
|
'description' => $this->getParam('docs.description'),
|
||||||
|
@ -85,47 +85,31 @@ class OpenAPI3 extends Format
|
||||||
$url = \str_replace('/v1', '', $route->getURL());
|
$url = \str_replace('/v1', '', $route->getURL());
|
||||||
$scope = $route->getLabel('scope', '');
|
$scope = $route->getLabel('scope', '');
|
||||||
$hide = $route->getLabel('sdk.hide', false);
|
$hide = $route->getLabel('sdk.hide', false);
|
||||||
|
$consumes = [$route->getLabel('sdk.request.type', 'application/json')];
|
||||||
|
|
||||||
if ($hide) {
|
if ($hide) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
$desc = (!empty($route->getLabel('sdk.description', ''))) ? \realpath(__DIR__.'/../../../'.$route->getLabel('sdk.description', '')) : null;
|
$id = $route->getLabel('sdk.method', \uniqid());
|
||||||
|
$desc = (!empty($route->getLabel('sdk.description', ''))) ? \realpath(__DIR__.'/../../../../'.$route->getLabel('sdk.description', '')) : null;
|
||||||
$model = $route->getLabel('sdk.response.model', 'none');
|
$produces = $route->getLabel('sdk.response.type', 'application/json');
|
||||||
|
$model = $route->getLabel('sdk.response.model', 'none');
|
||||||
|
|
||||||
$temp = [
|
$temp = [
|
||||||
|
|
||||||
'summary' => $route->getDesc(),
|
'summary' => $route->getDesc(),
|
||||||
'operationId' => $route->getLabel('sdk.method', \uniqid()),
|
'operationId' => $route->getLabel('sdk.namespace', 'default').ucfirst($id),
|
||||||
|
// 'consumes' => [],
|
||||||
|
// 'produces' => [$produces],
|
||||||
'tags' => [$route->getLabel('sdk.namespace', 'default')],
|
'tags' => [$route->getLabel('sdk.namespace', 'default')],
|
||||||
'description' => ($desc) ? \file_get_contents($desc) : '',
|
'description' => ($desc) ? \file_get_contents($desc) : '',
|
||||||
'responses' => [
|
'responses' => [],
|
||||||
(string)$route->getLabel('sdk.response.code', '500') => [
|
|
||||||
'description' => '',
|
|
||||||
'content' => [
|
|
||||||
$route->getLabel('sdk.response.type', '') => [
|
|
||||||
'schema' => [
|
|
||||||
'$ref' => '#/components/schemas/'.$model,
|
|
||||||
],
|
|
||||||
]
|
|
||||||
],
|
|
||||||
],
|
|
||||||
],
|
|
||||||
'requestBody' => [
|
|
||||||
'content' => [
|
|
||||||
'multipart/form-data' => [
|
|
||||||
'schema' => [
|
|
||||||
'type' => 'object',
|
|
||||||
],
|
|
||||||
],
|
|
||||||
]
|
|
||||||
],
|
|
||||||
'x-appwrite' => [ // Appwrite related metadata
|
'x-appwrite' => [ // Appwrite related metadata
|
||||||
|
'method' => $route->getLabel('sdk.method', \uniqid()),
|
||||||
'weight' => $route->getOrder(),
|
'weight' => $route->getOrder(),
|
||||||
'cookies' => $route->getLabel('sdk.cookies', false),
|
'cookies' => $route->getLabel('sdk.cookies', false),
|
||||||
'type' => $route->getLabel('sdk.methodType', ''),
|
'type' => $route->getLabel('sdk.methodType', ''),
|
||||||
'demo' => 'docs/examples/'. Template::fromCamelCaseToDash($route->getLabel('sdk.namespace', 'default')).'/'.Template::fromCamelCaseToDash($temp['operationId']).'.md',
|
'demo' => 'docs/examples/'. Template::fromCamelCaseToDash($route->getLabel('sdk.namespace', 'default')).'/'.Template::fromCamelCaseToDash($id).'.md',
|
||||||
'edit' => 'https://github.com/appwrite/appwrite/edit/master' . $route->getLabel('sdk.description', ''),
|
'edit' => 'https://github.com/appwrite/appwrite/edit/master' . $route->getLabel('sdk.description', ''),
|
||||||
'rate-limit' => $route->getLabel('abuse-limit', 0),
|
'rate-limit' => $route->getLabel('abuse-limit', 0),
|
||||||
'rate-time' => $route->getLabel('abuse-time', 3600),
|
'rate-time' => $route->getLabel('abuse-time', 3600),
|
||||||
|
@ -135,11 +119,65 @@ class OpenAPI3 extends Format
|
||||||
],
|
],
|
||||||
];
|
];
|
||||||
|
|
||||||
|
foreach ($this->models as $key => $value) {
|
||||||
|
if($value->getType() === $model) {
|
||||||
|
$model = $value;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if($model->isNone()) {
|
||||||
|
$temp['responses'][(string)$route->getLabel('sdk.response.code', '500')] = [
|
||||||
|
'description' => (in_array($produces, [
|
||||||
|
'image/*',
|
||||||
|
'image/jpeg',
|
||||||
|
'image/gif',
|
||||||
|
'image/png',
|
||||||
|
'image/webp',
|
||||||
|
'image/svg-x',
|
||||||
|
'image/x-icon',
|
||||||
|
'image/bmp',
|
||||||
|
])) ? 'Image' : 'File',
|
||||||
|
// 'schema' => [
|
||||||
|
// 'type' => 'file'
|
||||||
|
// ],
|
||||||
|
];
|
||||||
|
} else {
|
||||||
|
$temp['responses'][(string)$route->getLabel('sdk.response.code', '500')] = [
|
||||||
|
'description' => $model->getName(),
|
||||||
|
'content' => [
|
||||||
|
$produces => [
|
||||||
|
'schema' => [
|
||||||
|
'$ref' => '#/components/schemas/'.$model->getType(),
|
||||||
|
],
|
||||||
|
],
|
||||||
|
],
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
if($route->getLabel('sdk.response.code', 500) === 204) {
|
||||||
|
$temp['responses'][(string)$route->getLabel('sdk.response.code', '500')]['description'] = 'No content';
|
||||||
|
unset($temp['responses'][(string)$route->getLabel('sdk.response.code', '500')]['schema']);
|
||||||
|
}
|
||||||
|
|
||||||
if ((!empty($scope))) { // && 'public' != $scope
|
if ((!empty($scope))) { // && 'public' != $scope
|
||||||
$temp['security'][] = $route->getLabel('sdk.security', $this->security);
|
$temp['security'][] = $route->getLabel('sdk.security', $this->security);
|
||||||
}
|
}
|
||||||
|
|
||||||
foreach ($route->getParams() as $name => $param) {
|
$body = [
|
||||||
|
'content' => [
|
||||||
|
$consumes[0] => [
|
||||||
|
'schema' => [
|
||||||
|
'type' => 'object',
|
||||||
|
'properties' => [],
|
||||||
|
],
|
||||||
|
],
|
||||||
|
],
|
||||||
|
];
|
||||||
|
|
||||||
|
$bodyRequired = [];
|
||||||
|
|
||||||
|
foreach ($route->getParams() as $name => $param) { // Set params
|
||||||
$validator = (\is_callable($param['validator'])) ? call_user_func_array($param['validator'], $this->app->getResources($param['resources'])) : $param['validator']; /* @var $validator \Utopia\Validator */
|
$validator = (\is_callable($param['validator'])) ? call_user_func_array($param['validator'], $this->app->getResources($param['resources'])) : $param['validator']; /* @var $validator \Utopia\Validator */
|
||||||
|
|
||||||
$node = [
|
$node = [
|
||||||
|
@ -150,79 +188,79 @@ class OpenAPI3 extends Format
|
||||||
|
|
||||||
switch ((!empty($validator)) ? \get_class($validator) : '') {
|
switch ((!empty($validator)) ? \get_class($validator) : '') {
|
||||||
case 'Utopia\Validator\Text':
|
case 'Utopia\Validator\Text':
|
||||||
$node['type'] = 'string';
|
$node['schema']['type'] = 'string';
|
||||||
$node['x-example'] = '['.\strtoupper(Template::fromCamelCaseToSnake($node['name'])).']';
|
$node['schema']['x-example'] = '['.\strtoupper(Template::fromCamelCaseToSnake($node['name'])).']';
|
||||||
break;
|
break;
|
||||||
case 'Utopia\Validator\Boolean':
|
case 'Utopia\Validator\Boolean':
|
||||||
$node['type'] = 'boolean';
|
$node['schema']['type'] = 'boolean';
|
||||||
$node['x-example'] = false;
|
$node['schema']['x-example'] = false;
|
||||||
break;
|
break;
|
||||||
case 'Appwrite\Database\Validator\UID':
|
case 'Appwrite\Database\Validator\UID':
|
||||||
$node['type'] = 'string';
|
$node['schema']['type'] = 'string';
|
||||||
$node['x-example'] = '['.\strtoupper(Template::fromCamelCaseToSnake($node['name'])).']';
|
$node['schema']['x-example'] = '['.\strtoupper(Template::fromCamelCaseToSnake($node['name'])).']';
|
||||||
break;
|
break;
|
||||||
case 'Utopia\Validator\Email':
|
case 'Utopia\Validator\Email':
|
||||||
$node['type'] = 'string';
|
$node['schema']['type'] = 'string';
|
||||||
$node['format'] = 'email';
|
$node['schema']['format'] = 'email';
|
||||||
$node['x-example'] = 'email@example.com';
|
$node['schema']['x-example'] = 'email@example.com';
|
||||||
break;
|
break;
|
||||||
case 'Utopia\Validator\URL':
|
case 'Utopia\Validator\URL':
|
||||||
$node['type'] = 'string';
|
$node['schema']['type'] = 'string';
|
||||||
$node['format'] = 'url';
|
$node['schema']['format'] = 'url';
|
||||||
$node['x-example'] = 'https://example.com';
|
$node['schema']['x-example'] = 'https://example.com';
|
||||||
break;
|
break;
|
||||||
case 'Utopia\Validator\JSON':
|
case 'Utopia\Validator\JSON':
|
||||||
case 'Utopia\Validator\Mock':
|
case 'Utopia\Validator\Mock':
|
||||||
case 'Utopia\Validator\Assoc':
|
case 'Utopia\Validator\Assoc':
|
||||||
$node['type'] = 'object';
|
$node['schema']['type'] = 'object';
|
||||||
$node['type'] = 'object';
|
$node['schema']['type'] = 'object';
|
||||||
$node['x-example'] = '{}';
|
$node['schema']['x-example'] = '{}';
|
||||||
//$node['format'] = 'json';
|
//$node['schema']['format'] = 'json';
|
||||||
break;
|
break;
|
||||||
case 'Appwrite\Storage\Validator\File':
|
case 'Appwrite\Storage\Validator\File':
|
||||||
$requestType = 'multipart/form-data';
|
$consumes = ['multipart/form-data'];
|
||||||
$node['type'] = 'file';
|
$node['schema']['type'] = 'string';
|
||||||
|
$node['schema']['format'] = 'binary';
|
||||||
break;
|
break;
|
||||||
case 'Utopia\Validator\ArrayList':
|
case 'Utopia\Validator\ArrayList':
|
||||||
$node['type'] = 'array';
|
$node['schema']['type'] = 'array';
|
||||||
$node['collectionFormat'] = 'multi';
|
$node['schema']['items'] = [
|
||||||
$node['items'] = [
|
|
||||||
'type' => 'string',
|
'type' => 'string',
|
||||||
];
|
];
|
||||||
break;
|
break;
|
||||||
case 'Appwrite\Auth\Validator\Password':
|
case 'Appwrite\Auth\Validator\Password':
|
||||||
$node['type'] = 'string';
|
$node['schema']['type'] = 'string';
|
||||||
$node['format'] = 'format';
|
$node['schema']['format'] = 'format';
|
||||||
$node['x-example'] = 'password';
|
$node['schema']['x-example'] = 'password';
|
||||||
break;
|
break;
|
||||||
case 'Utopia\Validator\Range': /* @var $validator \Utopia\Validator\Range */
|
case 'Utopia\Validator\Range': /* @var $validator \Utopia\Validator\Range */
|
||||||
$node['type'] = 'integer';
|
$node['schema']['type'] = 'integer';
|
||||||
$node['format'] = 'int32';
|
$node['schema']['format'] = 'int32';
|
||||||
$node['x-example'] = $validator->getMin();
|
$node['schema']['x-example'] = $validator->getMin();
|
||||||
break;
|
break;
|
||||||
case 'Utopia\Validator\Numeric':
|
case 'Utopia\Validator\Numeric':
|
||||||
$node['type'] = 'integer';
|
$node['schema']['type'] = 'integer';
|
||||||
$node['format'] = 'int32';
|
$node['schema']['format'] = 'int32';
|
||||||
break;
|
break;
|
||||||
case 'Utopia\Validator\Length':
|
case 'Utopia\Validator\Length':
|
||||||
$node['type'] = 'string';
|
$node['schema']['type'] = 'string';
|
||||||
break;
|
break;
|
||||||
case 'Utopia\Validator\Host':
|
case 'Utopia\Validator\Host':
|
||||||
$node['type'] = 'string';
|
$node['schema']['type'] = 'string';
|
||||||
$node['format'] = 'url';
|
$node['schema']['format'] = 'url';
|
||||||
$node['x-example'] = 'https://example.com';
|
$node['schema']['x-example'] = 'https://example.com';
|
||||||
break;
|
break;
|
||||||
case 'Utopia\Validator\WhiteList': /* @var $validator \Utopia\Validator\WhiteList */
|
case 'Utopia\Validator\WhiteList': /* @var $validator \Utopia\Validator\WhiteList */
|
||||||
$node['type'] = 'string';
|
$node['schema']['type'] = 'string';
|
||||||
$node['x-example'] = $validator->getList()[0];
|
$node['schema']['x-example'] = $validator->getList()[0];
|
||||||
break;
|
break;
|
||||||
default:
|
default:
|
||||||
$node['type'] = 'string';
|
$node['schema']['type'] = 'string';
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($param['optional'] && !\is_null($param['default'])) { // Param has default value
|
if ($param['optional'] && !\is_null($param['default'])) { // Param has default value
|
||||||
$node['default'] = $param['default'];
|
$node['schema']['default'] = $param['default'];
|
||||||
}
|
}
|
||||||
|
|
||||||
if (false !== \strpos($url, ':'.$name)) { // Param is in URL path
|
if (false !== \strpos($url, ':'.$name)) { // Param is in URL path
|
||||||
|
@ -232,18 +270,38 @@ class OpenAPI3 extends Format
|
||||||
$node['in'] = 'query';
|
$node['in'] = 'query';
|
||||||
$temp['parameters'][] = $node;
|
$temp['parameters'][] = $node;
|
||||||
} else { // Param is in payload
|
} else { // Param is in payload
|
||||||
$node['in'] = 'formData';
|
if(!$param['optional']) {
|
||||||
$temp['parameters'][] = $node;
|
$bodyRequired[] = $name;
|
||||||
$requestBody['content']['application/x-www-form-urlencoded']['schema']['properties'][] = $node;
|
}
|
||||||
|
|
||||||
if (!$param['optional']) {
|
$body['content'][$consumes[0]]['schema']['properties'][$name] = [
|
||||||
$requestBody['content']['application/x-www-form-urlencoded']['required'][] = $name;
|
'type' => $node['schema']['type'],
|
||||||
|
'description' => $node['description'],
|
||||||
|
'default' => $node['default'] ?? null,
|
||||||
|
'x-example' => $node['x-example'] ?? null,
|
||||||
|
];
|
||||||
|
|
||||||
|
if(\array_key_exists('items', $node['schema'])) {
|
||||||
|
$body['content'][$consumes[0]]['schema']['properties'][$name]['items'] = $node['schema']['items'];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
$url = \str_replace(':'.$name, '{'.$name.'}', $url);
|
$url = \str_replace(':'.$name, '{'.$name.'}', $url);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
var_dump($body);
|
||||||
|
var_dump('-------');
|
||||||
|
if(!empty($body['content'][$consumes[0]]['schema']['properties'])) {
|
||||||
|
$temp['requestBody'] = $body;
|
||||||
|
}
|
||||||
|
|
||||||
|
if(!empty($bodyRequired)) {
|
||||||
|
$body['schema']['required'] = $bodyRequired;
|
||||||
|
}
|
||||||
|
|
||||||
|
//$temp['consumes'] = $consumes;
|
||||||
|
|
||||||
$output['paths'][$url][\strtolower($route->getMethod())] = $temp;
|
$output['paths'][$url][\strtolower($route->getMethod())] = $temp;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -252,10 +310,14 @@ class OpenAPI3 extends Format
|
||||||
$rules = $model->getRules();
|
$rules = $model->getRules();
|
||||||
|
|
||||||
$output['components']['schemas'][$model->getType()] = [
|
$output['components']['schemas'][$model->getType()] = [
|
||||||
|
'description' => $model->getName(),
|
||||||
'type' => 'object',
|
'type' => 'object',
|
||||||
'properties' => (empty($rules)) ? new \stdClass : [],
|
|
||||||
];
|
];
|
||||||
|
|
||||||
|
if(!empty($rules)) {
|
||||||
|
$output['components']['schemas'][$model->getType()]['properties'] = [];
|
||||||
|
}
|
||||||
|
|
||||||
if($model->isAny()) {
|
if($model->isAny()) {
|
||||||
$output['components']['schemas'][$model->getType()]['additionalProperties'] = true;
|
$output['components']['schemas'][$model->getType()]['additionalProperties'] = true;
|
||||||
}
|
}
|
||||||
|
@ -286,7 +348,7 @@ class OpenAPI3 extends Format
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case 'boolean':
|
case 'boolean':
|
||||||
$type = 'string';
|
$type = 'boolean';
|
||||||
break;
|
break;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
|
@ -294,7 +356,6 @@ class OpenAPI3 extends Format
|
||||||
$rule['type'] = ($rule['type']) ? $rule['type'] : 'none';
|
$rule['type'] = ($rule['type']) ? $rule['type'] : 'none';
|
||||||
|
|
||||||
$items = [
|
$items = [
|
||||||
'type' => $type,
|
|
||||||
'$ref' => '#/components/schemas/'.$rule['type'],
|
'$ref' => '#/components/schemas/'.$rule['type'],
|
||||||
];
|
];
|
||||||
break;
|
break;
|
||||||
|
@ -320,6 +381,8 @@ class OpenAPI3 extends Format
|
||||||
$output['components']['schemas'][$model->getType()]['properties'][$name] = [
|
$output['components']['schemas'][$model->getType()]['properties'][$name] = [
|
||||||
'type' => $type,
|
'type' => $type,
|
||||||
'description' => $rule['description'] ?? '',
|
'description' => $rule['description'] ?? '',
|
||||||
|
//'default' => $rule['default'] ?? null,
|
||||||
|
'x-example' => $rule['example'] ?? null,
|
||||||
];
|
];
|
||||||
|
|
||||||
if($format) {
|
if($format) {
|
||||||
|
|
Loading…
Reference in a new issue