2020-11-12 11:02:42 +13:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace Appwrite\Specification\Format;
|
|
|
|
|
|
|
|
use Appwrite\Specification\Format;
|
|
|
|
use Appwrite\Template\Template;
|
2021-01-07 01:49:11 +13:00
|
|
|
use stdClass;
|
2021-05-21 19:08:53 +12:00
|
|
|
use Utopia\Validator;
|
2020-11-12 11:02:42 +13:00
|
|
|
|
|
|
|
class OpenAPI3 extends Format
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* Get Name.
|
|
|
|
*
|
|
|
|
* Get format name
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getName():string
|
|
|
|
{
|
|
|
|
return 'Open API 3';
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Parse
|
|
|
|
*
|
|
|
|
* Parses Appwrite App to given format
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function parse(): array
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* Specifications (v3.0.0):
|
|
|
|
* https://github.com/OAI/OpenAPI-Specification/blob/master/versions/3.0.0.md
|
|
|
|
*/
|
|
|
|
$output = [
|
|
|
|
'openapi' => '3.0.0',
|
|
|
|
'info' => [
|
|
|
|
'version' => $this->getParam('version'),
|
|
|
|
'title' => $this->getParam('name'),
|
|
|
|
'description' => $this->getParam('description'),
|
|
|
|
'termsOfService' => $this->getParam('terms'),
|
|
|
|
'contact' => [
|
|
|
|
'name' => $this->getParam('contact.name'),
|
|
|
|
'url' => $this->getParam('contact.url'),
|
|
|
|
'email' => $this->getParam('contact.email'),
|
|
|
|
],
|
|
|
|
'license' => [
|
|
|
|
'name' => 'BSD-3-Clause',
|
|
|
|
'url' => 'https://raw.githubusercontent.com/appwrite/appwrite/master/LICENSE',
|
|
|
|
],
|
|
|
|
],
|
|
|
|
'servers' => [
|
|
|
|
[
|
|
|
|
'url' => $this->getParam('endpoint', ''),
|
|
|
|
],
|
|
|
|
],
|
|
|
|
'paths' => [],
|
2021-01-27 00:54:06 +13:00
|
|
|
'tags' => $this->services,
|
2020-11-12 11:02:42 +13:00
|
|
|
'components' => [
|
|
|
|
'schemas' => [],
|
2020-11-15 00:52:38 +13:00
|
|
|
'securitySchemes' => $this->keys,
|
2020-11-12 11:02:42 +13:00
|
|
|
],
|
|
|
|
'externalDocs' => [
|
|
|
|
'description' => $this->getParam('docs.description'),
|
|
|
|
'url' => $this->getParam('docs.url'),
|
|
|
|
],
|
|
|
|
];
|
|
|
|
|
|
|
|
if (isset($output['components']['securitySchemes']['Project'])) {
|
|
|
|
$output['components']['securitySchemes']['Project']['x-appwrite'] = ['demo' => '5df5acd0d48c2'];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($output['components']['securitySchemes']['Key'])) {
|
|
|
|
$output['components']['securitySchemes']['Key']['x-appwrite'] = ['demo' => '919c2d18fb5d4...a2ae413da83346ad2'];
|
|
|
|
}
|
2021-03-29 10:47:17 +13:00
|
|
|
|
|
|
|
if (isset($output['securityDefinitions']['JWT'])) {
|
|
|
|
$output['securityDefinitions']['JWT']['x-appwrite'] = ['demo' => 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ...'];
|
|
|
|
}
|
2020-11-12 11:02:42 +13:00
|
|
|
|
|
|
|
if (isset($output['components']['securitySchemes']['Locale'])) {
|
|
|
|
$output['components']['securitySchemes']['Locale']['x-appwrite'] = ['demo' => 'en'];
|
|
|
|
}
|
|
|
|
|
|
|
|
if (isset($output['components']['securitySchemes']['Mode'])) {
|
|
|
|
$output['components']['securitySchemes']['Mode']['x-appwrite'] = ['demo' => ''];
|
|
|
|
}
|
|
|
|
|
2021-04-08 20:46:45 +12:00
|
|
|
$usedModels = [];
|
|
|
|
|
|
|
|
foreach ($this->routes as $route) { /** @var \Utopia\Route $route */
|
2021-08-19 17:05:21 +12:00
|
|
|
$url = \str_replace('/v1', '', $route->getPath());
|
2020-11-12 11:02:42 +13:00
|
|
|
$scope = $route->getLabel('scope', '');
|
|
|
|
$hide = $route->getLabel('sdk.hide', false);
|
2020-11-15 00:52:38 +13:00
|
|
|
$consumes = [$route->getLabel('sdk.request.type', 'application/json')];
|
2020-11-12 11:02:42 +13:00
|
|
|
|
|
|
|
if ($hide) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2020-11-15 00:52:38 +13:00
|
|
|
$id = $route->getLabel('sdk.method', \uniqid());
|
|
|
|
$desc = (!empty($route->getLabel('sdk.description', ''))) ? \realpath(__DIR__.'/../../../../'.$route->getLabel('sdk.description', '')) : null;
|
2021-02-02 19:02:35 +13:00
|
|
|
$produces = $route->getLabel('sdk.response.type', null);
|
2020-11-15 00:52:38 +13:00
|
|
|
$model = $route->getLabel('sdk.response.model', 'none');
|
2021-05-20 01:10:32 +12:00
|
|
|
$routeSecurity = $route->getLabel('sdk.auth', []);
|
|
|
|
$sdkPlatofrms = [];
|
|
|
|
|
|
|
|
foreach ($routeSecurity as $value) {
|
|
|
|
switch ($value) {
|
|
|
|
case APP_AUTH_TYPE_SESSION:
|
|
|
|
$sdkPlatofrms[] = APP_PLATFORM_CLIENT;
|
|
|
|
break;
|
|
|
|
case APP_AUTH_TYPE_KEY:
|
|
|
|
$sdkPlatofrms[] = APP_PLATFORM_SERVER;
|
|
|
|
break;
|
|
|
|
case APP_AUTH_TYPE_JWT:
|
|
|
|
$sdkPlatofrms[] = APP_PLATFORM_SERVER;
|
|
|
|
break;
|
|
|
|
case APP_AUTH_TYPE_ADMIN:
|
|
|
|
$sdkPlatofrms[] = APP_PLATFORM_CONSOLE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2021-05-20 03:29:06 +12:00
|
|
|
|
|
|
|
if(empty($routeSecurity)) {
|
|
|
|
$sdkPlatofrms[] = APP_PLATFORM_CLIENT;
|
|
|
|
}
|
2020-11-15 00:52:38 +13:00
|
|
|
|
2020-11-12 11:02:42 +13:00
|
|
|
$temp = [
|
|
|
|
'summary' => $route->getDesc(),
|
2020-11-15 00:52:38 +13:00
|
|
|
'operationId' => $route->getLabel('sdk.namespace', 'default').ucfirst($id),
|
|
|
|
// 'consumes' => [],
|
|
|
|
// 'produces' => [$produces],
|
2020-11-12 11:02:42 +13:00
|
|
|
'tags' => [$route->getLabel('sdk.namespace', 'default')],
|
|
|
|
'description' => ($desc) ? \file_get_contents($desc) : '',
|
2020-11-15 00:52:38 +13:00
|
|
|
'responses' => [],
|
2020-11-12 11:02:42 +13:00
|
|
|
'x-appwrite' => [ // Appwrite related metadata
|
2020-11-15 00:52:38 +13:00
|
|
|
'method' => $route->getLabel('sdk.method', \uniqid()),
|
2020-11-12 11:02:42 +13:00
|
|
|
'weight' => $route->getOrder(),
|
|
|
|
'cookies' => $route->getLabel('sdk.cookies', false),
|
|
|
|
'type' => $route->getLabel('sdk.methodType', ''),
|
2020-12-26 01:11:17 +13:00
|
|
|
'demo' => Template::fromCamelCaseToDash($route->getLabel('sdk.namespace', 'default')).'/'.Template::fromCamelCaseToDash($id).'.md',
|
2020-11-12 11:02:42 +13:00
|
|
|
'edit' => 'https://github.com/appwrite/appwrite/edit/master' . $route->getLabel('sdk.description', ''),
|
|
|
|
'rate-limit' => $route->getLabel('abuse-limit', 0),
|
|
|
|
'rate-time' => $route->getLabel('abuse-time', 3600),
|
|
|
|
'rate-key' => $route->getLabel('abuse-key', 'url:{url},ip:{ip}'),
|
|
|
|
'scope' => $route->getLabel('scope', ''),
|
2021-05-20 01:10:32 +12:00
|
|
|
'platforms' => $sdkPlatofrms,
|
2021-01-30 05:13:11 +13:00
|
|
|
'packaging' => $route->getLabel('sdk.packaging', false),
|
2020-11-12 11:02:42 +13:00
|
|
|
],
|
|
|
|
];
|
|
|
|
|
2020-11-15 00:52:38 +13:00
|
|
|
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 {
|
2021-04-08 20:46:45 +12:00
|
|
|
$usedModels[] = $model->getType();
|
2020-11-15 00:52:38 +13:00
|
|
|
$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']);
|
|
|
|
}
|
|
|
|
|
2020-11-12 11:02:42 +13:00
|
|
|
if ((!empty($scope))) { // && 'public' != $scope
|
2021-03-29 10:22:12 +13:00
|
|
|
$securities = ['Project' => []];
|
|
|
|
|
2021-04-16 19:22:17 +12:00
|
|
|
foreach($route->getLabel('sdk.auth', []) as $security) {
|
2021-03-29 10:22:12 +13:00
|
|
|
if(array_key_exists($security, $this->keys)) {
|
|
|
|
$securities[$security] = [];
|
|
|
|
}
|
|
|
|
}
|
2021-03-29 10:47:17 +13:00
|
|
|
|
2021-05-20 02:26:06 +12:00
|
|
|
$temp['x-appwrite']['auth'] = array_slice($securities, 0, $this->authCount);
|
2021-03-29 10:22:12 +13:00
|
|
|
$temp['security'][] = $securities;
|
2020-11-12 11:02:42 +13:00
|
|
|
}
|
|
|
|
|
2020-11-15 00:52:38 +13:00
|
|
|
$body = [
|
|
|
|
'content' => [
|
|
|
|
$consumes[0] => [
|
|
|
|
'schema' => [
|
|
|
|
'type' => 'object',
|
|
|
|
'properties' => [],
|
|
|
|
],
|
|
|
|
],
|
|
|
|
],
|
|
|
|
];
|
|
|
|
|
|
|
|
$bodyRequired = [];
|
|
|
|
|
|
|
|
foreach ($route->getParams() as $name => $param) { // Set params
|
2020-12-27 07:52:58 +13:00
|
|
|
$validator = (\is_callable($param['validator'])) ? call_user_func_array($param['validator'], $this->app->getResources($param['injections'])) : $param['validator']; /* @var $validator \Utopia\Validator */
|
2020-11-12 11:02:42 +13:00
|
|
|
|
|
|
|
$node = [
|
|
|
|
'name' => $name,
|
|
|
|
'description' => $param['description'],
|
|
|
|
'required' => !$param['optional'],
|
|
|
|
];
|
|
|
|
|
|
|
|
switch ((!empty($validator)) ? \get_class($validator) : '') {
|
|
|
|
case 'Utopia\Validator\Text':
|
2021-05-21 19:08:53 +12:00
|
|
|
$node['schema']['type'] = $validator->getType();
|
2020-11-15 00:52:38 +13:00
|
|
|
$node['schema']['x-example'] = '['.\strtoupper(Template::fromCamelCaseToSnake($node['name'])).']';
|
2020-11-12 11:02:42 +13:00
|
|
|
break;
|
|
|
|
case 'Utopia\Validator\Boolean':
|
2021-05-21 19:08:53 +12:00
|
|
|
$node['schema']['type'] = $validator->getType();
|
2020-11-15 00:52:38 +13:00
|
|
|
$node['schema']['x-example'] = false;
|
2020-11-12 11:02:42 +13:00
|
|
|
break;
|
|
|
|
case 'Appwrite\Database\Validator\UID':
|
2021-05-21 19:08:53 +12:00
|
|
|
$node['schema']['type'] = $validator->getType();
|
2020-11-15 00:52:38 +13:00
|
|
|
$node['schema']['x-example'] = '['.\strtoupper(Template::fromCamelCaseToSnake($node['name'])).']';
|
2020-11-12 11:02:42 +13:00
|
|
|
break;
|
2021-04-21 18:54:28 +12:00
|
|
|
case 'Appwrite\Network\Validator\Email':
|
2021-05-21 19:08:53 +12:00
|
|
|
$node['schema']['type'] = $validator->getType();
|
2020-11-15 00:52:38 +13:00
|
|
|
$node['schema']['format'] = 'email';
|
|
|
|
$node['schema']['x-example'] = 'email@example.com';
|
2020-11-12 11:02:42 +13:00
|
|
|
break;
|
2021-04-21 18:54:28 +12:00
|
|
|
case 'Appwrite\Network\Validator\URL':
|
2021-05-21 19:08:53 +12:00
|
|
|
$node['schema']['type'] = $validator->getType();
|
2020-11-15 00:52:38 +13:00
|
|
|
$node['schema']['format'] = 'url';
|
|
|
|
$node['schema']['x-example'] = 'https://example.com';
|
2020-11-12 11:02:42 +13:00
|
|
|
break;
|
|
|
|
case 'Utopia\Validator\JSON':
|
|
|
|
case 'Utopia\Validator\Mock':
|
|
|
|
case 'Utopia\Validator\Assoc':
|
2021-01-07 01:49:11 +13:00
|
|
|
$param['default'] = (empty($param['default'])) ? new stdClass() : $param['default'];
|
2021-05-21 20:44:51 +12:00
|
|
|
$node['schema']['type'] = 'object';
|
2020-11-15 00:52:38 +13:00
|
|
|
$node['schema']['x-example'] = '{}';
|
|
|
|
//$node['schema']['format'] = 'json';
|
2020-11-12 11:02:42 +13:00
|
|
|
break;
|
2021-01-22 21:28:33 +13:00
|
|
|
case 'Utopia\Storage\Validator\File':
|
2020-11-15 00:52:38 +13:00
|
|
|
$consumes = ['multipart/form-data'];
|
2021-05-21 19:08:53 +12:00
|
|
|
$node['schema']['type'] = $validator->getType();
|
2020-11-15 00:52:38 +13:00
|
|
|
$node['schema']['format'] = 'binary';
|
2020-11-12 11:02:42 +13:00
|
|
|
break;
|
|
|
|
case 'Utopia\Validator\ArrayList':
|
2021-05-21 20:44:51 +12:00
|
|
|
$node['schema']['type'] = 'array';
|
2020-11-15 00:52:38 +13:00
|
|
|
$node['schema']['items'] = [
|
2020-11-12 11:02:42 +13:00
|
|
|
'type' => 'string',
|
|
|
|
];
|
|
|
|
break;
|
|
|
|
case 'Appwrite\Auth\Validator\Password':
|
2021-05-21 19:08:53 +12:00
|
|
|
$node['schema']['type'] = $validator->getType();
|
2021-05-21 20:44:51 +12:00
|
|
|
$node['schema']['format'] = 'password';
|
2020-11-15 00:52:38 +13:00
|
|
|
$node['schema']['x-example'] = 'password';
|
2020-11-12 11:02:42 +13:00
|
|
|
break;
|
2021-04-08 20:46:45 +12:00
|
|
|
case 'Utopia\Validator\Range': /** @var \Utopia\Validator\Range $validator */
|
2021-05-21 22:47:08 +12:00
|
|
|
$node['schema']['type'] = $validator->getType() === Validator::TYPE_FLOAT ? 'number': $validator->getType();
|
2021-05-21 19:08:53 +12:00
|
|
|
$node['schema']['format'] = $validator->getType() == Validator::TYPE_INTEGER ? 'int32' : 'float';
|
2020-11-15 00:52:38 +13:00
|
|
|
$node['schema']['x-example'] = $validator->getMin();
|
2020-11-12 11:02:42 +13:00
|
|
|
break;
|
|
|
|
case 'Utopia\Validator\Numeric':
|
2021-05-21 19:08:53 +12:00
|
|
|
$node['schema']['type'] = $validator->getType();
|
2020-11-15 00:52:38 +13:00
|
|
|
$node['schema']['format'] = 'int32';
|
2020-11-12 11:02:42 +13:00
|
|
|
break;
|
|
|
|
case 'Utopia\Validator\Length':
|
2021-05-21 19:08:53 +12:00
|
|
|
$node['schema']['type'] = $validator->getType();
|
2020-11-12 11:02:42 +13:00
|
|
|
break;
|
2021-04-13 20:46:30 +12:00
|
|
|
case 'Appwrite\Network\Validator\Host':
|
2021-05-21 19:08:53 +12:00
|
|
|
$node['schema']['type'] = $validator->getType();
|
2020-11-15 00:52:38 +13:00
|
|
|
$node['schema']['format'] = 'url';
|
|
|
|
$node['schema']['x-example'] = 'https://example.com';
|
2020-11-12 11:02:42 +13:00
|
|
|
break;
|
2021-04-08 20:46:45 +12:00
|
|
|
case 'Utopia\Validator\WhiteList': /** @var \Utopia\Validator\WhiteList $validator */
|
2021-04-13 20:46:30 +12:00
|
|
|
$node['schema']['type'] = $validator->getType();
|
2020-11-15 00:52:38 +13:00
|
|
|
$node['schema']['x-example'] = $validator->getList()[0];
|
2021-04-13 20:46:30 +12:00
|
|
|
|
|
|
|
if ($validator->getType() === 'integer') {
|
|
|
|
$node['format'] = 'int32';
|
|
|
|
}
|
2020-11-12 11:02:42 +13:00
|
|
|
break;
|
|
|
|
default:
|
2020-11-15 00:52:38 +13:00
|
|
|
$node['schema']['type'] = 'string';
|
2020-11-12 11:02:42 +13:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($param['optional'] && !\is_null($param['default'])) { // Param has default value
|
2020-11-15 00:52:38 +13:00
|
|
|
$node['schema']['default'] = $param['default'];
|
2020-11-12 11:02:42 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
if (false !== \strpos($url, ':'.$name)) { // Param is in URL path
|
|
|
|
$node['in'] = 'path';
|
|
|
|
$temp['parameters'][] = $node;
|
|
|
|
} elseif ($route->getMethod() == 'GET') { // Param is in query
|
|
|
|
$node['in'] = 'query';
|
|
|
|
$temp['parameters'][] = $node;
|
|
|
|
} else { // Param is in payload
|
2020-11-15 00:52:38 +13:00
|
|
|
if(!$param['optional']) {
|
|
|
|
$bodyRequired[] = $name;
|
|
|
|
}
|
2020-11-12 11:02:42 +13:00
|
|
|
|
2020-11-15 00:52:38 +13:00
|
|
|
$body['content'][$consumes[0]]['schema']['properties'][$name] = [
|
|
|
|
'type' => $node['schema']['type'],
|
|
|
|
'description' => $node['description'],
|
|
|
|
'x-example' => $node['x-example'] ?? null,
|
|
|
|
];
|
|
|
|
|
2020-11-19 11:08:29 +13:00
|
|
|
if(isset($node['default'])) {
|
2020-11-15 02:05:18 +13:00
|
|
|
$body['content'][$consumes[0]]['schema']['properties'][$name]['default'] = $node['default'];
|
|
|
|
}
|
|
|
|
|
2020-11-15 00:52:38 +13:00
|
|
|
if(\array_key_exists('items', $node['schema'])) {
|
|
|
|
$body['content'][$consumes[0]]['schema']['properties'][$name]['items'] = $node['schema']['items'];
|
2020-11-12 11:02:42 +13:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$url = \str_replace(':'.$name, '{'.$name.'}', $url);
|
|
|
|
}
|
|
|
|
|
2020-11-15 02:05:18 +13:00
|
|
|
if(!empty($bodyRequired)) {
|
|
|
|
$body['content'][$consumes[0]]['schema']['required'] = $bodyRequired;
|
|
|
|
}
|
2020-11-15 00:52:38 +13:00
|
|
|
|
|
|
|
if(!empty($body['content'][$consumes[0]]['schema']['properties'])) {
|
|
|
|
$temp['requestBody'] = $body;
|
|
|
|
}
|
|
|
|
|
|
|
|
//$temp['consumes'] = $consumes;
|
|
|
|
|
2020-11-12 11:02:42 +13:00
|
|
|
$output['paths'][$url][\strtolower($route->getMethod())] = $temp;
|
|
|
|
}
|
|
|
|
foreach ($this->models as $model) {
|
2021-04-08 21:24:41 +12:00
|
|
|
foreach ($model->getRules() as $rule) {
|
|
|
|
if (!in_array($rule['type'], ['string', 'integer', 'boolean', 'json', 'float'])) {
|
|
|
|
$usedModels[] = $rule['type'];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-11-12 11:02:42 +13:00
|
|
|
foreach ($this->models as $model) {
|
2021-04-08 21:27:59 +12:00
|
|
|
if (!in_array($model->getType(), $usedModels) && $model->getType() !== 'error') {
|
2021-04-08 20:46:45 +12:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2020-11-12 11:02:42 +13:00
|
|
|
$required = $model->getRequired();
|
|
|
|
$rules = $model->getRules();
|
|
|
|
|
|
|
|
$output['components']['schemas'][$model->getType()] = [
|
2020-11-15 00:52:38 +13:00
|
|
|
'description' => $model->getName(),
|
2020-11-12 11:02:42 +13:00
|
|
|
'type' => 'object',
|
|
|
|
];
|
|
|
|
|
2020-11-15 00:52:38 +13:00
|
|
|
if(!empty($rules)) {
|
|
|
|
$output['components']['schemas'][$model->getType()]['properties'] = [];
|
|
|
|
}
|
|
|
|
|
2020-11-12 11:02:42 +13:00
|
|
|
if($model->isAny()) {
|
|
|
|
$output['components']['schemas'][$model->getType()]['additionalProperties'] = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!empty($required)) {
|
|
|
|
$output['components']['schemas'][$model->getType()]['required'] = $required;
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach($model->getRules() as $name => $rule) {
|
|
|
|
$type = '';
|
|
|
|
$format = null;
|
|
|
|
$items = null;
|
|
|
|
|
|
|
|
switch ($rule['type']) {
|
|
|
|
case 'string':
|
|
|
|
case 'json':
|
|
|
|
$type = 'string';
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'integer':
|
|
|
|
$type = 'integer';
|
|
|
|
$format = 'int32';
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'float':
|
|
|
|
$type = 'number';
|
|
|
|
$format = 'float';
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'boolean':
|
2020-11-15 00:52:38 +13:00
|
|
|
$type = 'boolean';
|
2020-11-12 11:02:42 +13:00
|
|
|
break;
|
|
|
|
|
|
|
|
default:
|
|
|
|
$type = 'object';
|
|
|
|
$rule['type'] = ($rule['type']) ? $rule['type'] : 'none';
|
|
|
|
|
|
|
|
$items = [
|
|
|
|
'$ref' => '#/components/schemas/'.$rule['type'],
|
|
|
|
];
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if($rule['array']) {
|
|
|
|
$output['components']['schemas'][$model->getType()]['properties'][$name] = [
|
|
|
|
'type' => 'array',
|
|
|
|
'description' => $rule['description'] ?? '',
|
|
|
|
'items' => [
|
|
|
|
'type' => $type,
|
2021-02-01 12:07:11 +13:00
|
|
|
],
|
|
|
|
'x-example' => $rule['example'] ?? null,
|
2020-11-12 11:02:42 +13:00
|
|
|
];
|
|
|
|
|
|
|
|
if($format) {
|
|
|
|
$output['components']['schemas'][$model->getType()]['properties'][$name]['items']['format'] = $format;
|
|
|
|
}
|
|
|
|
|
|
|
|
if($items) {
|
|
|
|
$output['components']['schemas'][$model->getType()]['properties'][$name]['items'] = $items;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$output['components']['schemas'][$model->getType()]['properties'][$name] = [
|
|
|
|
'type' => $type,
|
|
|
|
'description' => $rule['description'] ?? '',
|
2020-11-15 00:52:38 +13:00
|
|
|
//'default' => $rule['default'] ?? null,
|
|
|
|
'x-example' => $rule['example'] ?? null,
|
2020-11-12 11:02:42 +13:00
|
|
|
];
|
|
|
|
|
|
|
|
if($format) {
|
|
|
|
$output['components']['schemas'][$model->getType()]['properties'][$name]['format'] = $format;
|
|
|
|
}
|
|
|
|
|
|
|
|
if($items) {
|
|
|
|
$output['components']['schemas'][$model->getType()]['properties'][$name]['items'] = $items;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
\ksort($output['paths']);
|
|
|
|
|
|
|
|
return $output;
|
|
|
|
}
|
|
|
|
}
|