Fix operation spec gen

This commit is contained in:
Jake Barnby
2025-09-11 23:31:53 +12:00
parent 67b05a3f1a
commit fd6ddf6926
15 changed files with 14213 additions and 1219 deletions
@@ -8,6 +8,7 @@ use Appwrite\SDK\MethodType;
use Appwrite\SDK\Response;
use Appwrite\SDK\Specification\Format;
use Appwrite\Template\Template;
use Appwrite\Utopia\Database\Validator\Operation;
use Appwrite\Utopia\Response\Model;
use Utopia\Database\Database;
use Utopia\Database\Helpers\Permission;
@@ -396,7 +397,37 @@ class OpenAPI3 extends Format
$validator = $validator->getValidator();
}
switch ((!empty($validator)) ? \get_class($validator) : '') {
$class = !empty($validator)
? \get_class($validator)
: '';
$base = !empty($class)
? \get_parent_class($class)
: '';
switch ($base) {
case 'Appwrite\Utopia\Database\Validator\Queries\Base':
$class = $base;
break;
}
if ($class === 'Utopia\Validator\AnyOf') {
$validator = $param['validator']->getValidators()[0];
$class = \get_class($validator);
}
$array = false;
if ($class === 'Utopia\Validator\ArrayList') {
$array = true;
$subclass = \get_class($validator->getValidator());
switch ($subclass) {
case 'Appwrite\Utopia\Database\Validator\Operation':
$class = $subclass;
break;
}
}
switch ($class) {
case 'Utopia\Database\Validator\UID':
case 'Utopia\Validator\Text':
$node['schema']['type'] = $validator->getType();
@@ -418,6 +449,20 @@ class OpenAPI3 extends Format
$node['schema']['format'] = 'datetime';
$node['schema']['x-example'] = Model::TYPE_DATETIME_EXAMPLE;
break;
case 'Utopia\Database\Validator\Spatial':
/** @var Spatial $validator */
$node['schema']['type'] = 'array';
$node['schema']['items'] = [
'oneOf' => [
['type' => 'array']
]
];
$node['schema']['x-example'] = match ($validator->getSpatialType()) {
Database::VAR_POINT => '[1, 2]',
Database::VAR_LINESTRING => '[[1, 2], [3, 4], [5, 6]]',
Database::VAR_POLYGON => '[[[1, 2], [3, 4], [5, 6], [1, 2]]]',
};
break;
case 'Appwrite\Network\Validator\Email':
$node['schema']['type'] = $validator->getType();
$node['schema']['format'] = 'email';
@@ -449,20 +494,7 @@ class OpenAPI3 extends Format
'type' => $validator->getValidator()->getType(),
];
break;
case 'Utopia\Database\Validator\Spatial':
/** @var Spatial $validator */
$node['schema']['type'] = 'array';
$node['schema']['items'] = [
'oneOf' => [
['type' => 'array']
]
];
$node['schema']['x-example'] = match ($validator->getSpatialType()) {
Database::VAR_POINT => '[1, 2]',
Database::VAR_LINESTRING => '[[1, 2], [3, 4], [5, 6]]',
Database::VAR_POLYGON => '[[[1, 2], [3, 4], [5, 6], [1, 2]]]',
};
break;
case 'Appwrite\Utopia\Database\Validator\Queries\Base':
case 'Appwrite\Utopia\Database\Validator\Queries\Columns':
case 'Appwrite\Utopia\Database\Validator\Queries\Attributes':
case 'Appwrite\Utopia\Database\Validator\Queries\Buckets':
@@ -556,8 +588,7 @@ class OpenAPI3 extends Format
break;
}
}
if ($allowed) {
if ($allowed && $validator->getType() === 'string') {
$node['schema']['enum'] = $validator->getList();
$node['schema']['x-enum-name'] = $this->getEnumName($sdk->getNamespace() ?? '', $methodName, $name);
$node['schema']['x-enum-keys'] = $this->getEnumKeys($sdk->getNamespace() ?? '', $methodName, $name);
@@ -568,7 +599,35 @@ class OpenAPI3 extends Format
break;
case 'Appwrite\Utopia\Database\Validator\CompoundUID':
$node['schema']['type'] = $validator->getType();
$node['schema']['x-example'] = '[ID1:ID2]';
$node['schema']['x-example'] = '<ID1:ID2>';
break;
case 'Appwrite\Utopia\Database\Validator\Operation':
if ($array) {
$validator = $validator->getValidator();
}
/** @var Operation $validator */
$collectionIdKey = $validator->getCollectionIdKey();
$documentIdKey = $validator->getDocumentIdKey();
if ($array) {
$node['schema']['type'] = 'array';
$node['schema']['items'] = ['type' => 'object'];
} else {
$node['schema']['type'] = 'object';
}
$example = [
'action' => 'create',
'databaseId' => '<DATABASE_ID>',
$collectionIdKey => '<'.\strtoupper(Template::fromCamelCaseToSnake($collectionIdKey)).'>',
$documentIdKey => '<'.\strtoupper(Template::fromCamelCaseToSnake($documentIdKey)).'>',
'data' => [
'name' => 'Walter O\'Brien',
],
];
if ($array) {
$example = [$example];
}
$node['schema']['x-example'] = \str_replace("\n", "\n\t", \json_encode($example, JSON_PRETTY_PRINT));
break;
default:
$node['schema']['type'] = 'string';
@@ -8,6 +8,7 @@ use Appwrite\SDK\MethodType;
use Appwrite\SDK\Response;
use Appwrite\SDK\Specification\Format;
use Appwrite\Template\Template;
use Appwrite\Utopia\Database\Validator\Operation;
use Appwrite\Utopia\Response\Model;
use Utopia\Database\Database;
use Utopia\Database\Helpers\Permission;
@@ -422,6 +423,17 @@ class Swagger2 extends Format
$class = \get_class($validator);
}
$array = false;
if ($class === 'Utopia\Validator\ArrayList') {
$array = true;
$subclass = \get_class($validator->getValidator());
switch ($subclass) {
case 'Appwrite\Utopia\Database\Validator\Operation':
$class = $subclass;
break;
}
}
switch ($class) {
case 'Utopia\Validator\Text':
case 'Utopia\Database\Validator\UID':
@@ -444,6 +456,20 @@ class Swagger2 extends Format
$node['format'] = 'datetime';
$node['x-example'] = Model::TYPE_DATETIME_EXAMPLE;
break;
case 'Utopia\Database\Validator\Spatial':
/** @var Spatial $validator */
$node['type'] = 'array';
$node['schema']['items'] = [
'oneOf' => [
['type' => 'array']
]
];
$node['x-example'] = match ($validator->getSpatialType()) {
Database::VAR_POINT => '[1, 2]',
Database::VAR_LINESTRING => '[[1, 2], [3, 4], [5, 6]]',
Database::VAR_POLYGON => '[[[1, 2], [3, 4], [5, 6], [1, 2]]]',
};
break;
case 'Appwrite\Network\Validator\Email':
$node['type'] = $validator->getType();
$node['format'] = 'email';
@@ -464,20 +490,6 @@ class Swagger2 extends Format
'type' => $validator->getValidator()->getType(),
];
break;
case 'Utopia\Database\Validator\Spatial':
/** @var Spatial $validator */
$node['type'] = 'array';
$node['schema']['items'] = [
'oneOf' => [
['type' => 'array']
]
];
$node['x-example'] = match ($validator->getSpatialType()) {
Database::VAR_POINT => '[1, 2]',
Database::VAR_LINESTRING => '[[1, 2], [3, 4], [5, 6]]',
Database::VAR_POLYGON => '[[[1, 2], [3, 4], [5, 6], [1, 2]]]',
};
break;
case 'Utopia\Validator\JSON':
case 'Utopia\Validator\Mock':
case 'Utopia\Validator\Assoc':
@@ -562,20 +574,47 @@ class Swagger2 extends Format
break;
}
}
if ($allowed && $validator->getType() === 'string') {
$node['enum'] = $validator->getList();
$node['x-enum-name'] = $this->getEnumName($namespace, $methodName, $name);
$node['x-enum-keys'] = $this->getEnumKeys($namespace, $methodName, $name);
}
if ($validator->getType() === 'integer') {
$node['format'] = 'int32';
}
break;
case 'Appwrite\Utopia\Database\Validator\CompoundUID':
$node['type'] = $validator->getType();
$node['x-example'] = '[ID1:ID2]';
$node['x-example'] = '<ID1:ID2>';
break;
case 'Appwrite\Utopia\Database\Validator\Operation':
if ($array) {
$validator = $validator->getValidator();
}
/** @var Operation $validator */
$collectionIdKey = $validator->getCollectionIdKey();
$documentIdKey = $validator->getDocumentIdKey();
if ($array) {
$node['type'] = 'array';
$node['collectionFormat'] = 'multi';
$node['items'] = ['type' => 'object'];
} else {
$node['type'] = 'object';
}
$example = [
'action' => 'create',
'databaseId' => '<DATABASE_ID>',
$collectionIdKey => '<'.\strtoupper(Template::fromCamelCaseToSnake($collectionIdKey)).'>',
$documentIdKey => '<'.\strtoupper(Template::fromCamelCaseToSnake($documentIdKey)).'>',
'data' => [
'name' => 'Walter O\'Brien',
],
];
if ($array) {
$example = [$example];
}
$node['x-example'] = \str_replace("\n", "\n\t", \json_encode($example, JSON_PRETTY_PRINT));
break;
default:
$node['type'] = 'string';
@@ -64,12 +64,12 @@ class Operation extends Validator
return $this->description;
}
public function getCollectionIdName(): string
public function getCollectionIdKey(): string
{
return $this->collectionIdName;
}
public function getDocumentIdName(): string
public function getDocumentIdKey(): string
{
return $this->documentIdName;
}