remove specs task HTTP bootstrap

This commit is contained in:
Chirag Aggarwal
2026-04-09 10:41:04 +05:30
parent 597b20a6cb
commit 05dc264df9
5 changed files with 93 additions and 21 deletions
+19 -13
View File
@@ -21,7 +21,6 @@ use Utopia\Database\Adapter\MySQL;
use Utopia\Database\Database;
use Utopia\Database\Document;
use Utopia\DI\Container;
use Utopia\Http\Adapter\FPM\Server as FPMServer;
use Utopia\Http\Http;
use Utopia\Http\Request as UtopiaRequest;
use Utopia\Http\Response as UtopiaResponse;
@@ -60,6 +59,22 @@ class Specs extends Action
return new AppwriteResponse(new SwooleResponse());
}
protected function getSpecsContainer(UtopiaResponse $response): Container
{
$container = new Container();
$container->set('request', fn () => $this->getRequest());
$container->set('response', fn () => $response);
$container->set('dbForPlatform', fn () => new Database(new MySQL(''), new Cache(new None())));
$container->set('dbForProject', fn () => new Database(new MySQL(''), new Cache(new None())));
$container->set('redirectValidator', fn () => new Redirect([], []));
$container->set('project', fn () => new Document([]));
$container->set('passwordsDictionary', fn () => []);
$container->set('localeCodes', fn () => \array_map(fn ($locale) => $locale['code'], Config::getParam('locale-codes', [])));
$container->set('plan', fn () => []);
return $container;
}
protected function getFormatInstance(string $format, array $arguments)
{
return match ($format) {
@@ -340,17 +355,8 @@ class Specs extends Action
$mocks = ($mode === 'mocks');
// Mock dependencies needed by param validator injections in route definitions
$specsContainer = new Container();
$specsContainer->set('request', fn () => $this->getRequest());
$specsContainer->set('response', fn () => $response);
$specsContainer->set('dbForPlatform', fn () => new Database(new MySQL(''), new Cache(new None())));
$specsContainer->set('dbForProject', fn () => new Database(new MySQL(''), new Cache(new None())));
$specsContainer->set('redirectValidator', fn () => new Redirect([], []));
$specsContainer->set('project', fn () => new Document([]));
$specsContainer->set('passwordsDictionary', fn () => []);
$specsContainer->set('localeCodes', fn () => \array_map(fn ($locale) => $locale['code'], Config::getParam('locale-codes', [])));
$specsContainer->set('plan', fn () => []);
// Specs generation only needs route validator dependencies, not an HTTP server runtime.
$specsContainer = $this->getSpecsContainer($response);
$platforms = static::getPlatforms();
$authCounts = $this->getAuthCounts();
@@ -448,7 +454,7 @@ class Specs extends Action
}
$arguments = [
new Http(new FPMServer($specsContainer), 'UTC'),
$specsContainer,
$services,
$routes,
$models,
+26 -4
View File
@@ -4,12 +4,12 @@ namespace Appwrite\SDK\Specification;
use Appwrite\Utopia\Response\Model;
use Utopia\Config\Config;
use Utopia\Http\Http;
use Utopia\DI\Container;
use Utopia\Http\Route;
abstract class Format
{
protected Http $app;
protected Container $container;
/**
* @var array<Route>
@@ -80,9 +80,9 @@ abstract class Format
protected array $enumBlacklist = [];
public function __construct(Http $app, array $services, array $routes, array $models, array $keys, int $authCount, string $platform)
public function __construct(Container $container, array $services, array $routes, array $models, array $keys, int $authCount, string $platform)
{
$this->app = $app;
$this->container = $container;
$this->services = $services;
$this->routes = $routes;
$this->models = $models;
@@ -210,6 +210,28 @@ abstract class Format
return $this->services;
}
/**
* @param list<string> $injections
* @return array<string, mixed>
*/
protected function getResources(array $injections): array
{
$resources = [];
foreach ($injections as $name) {
$resources[$name] = $this->container->get($name);
}
return $resources;
}
protected function getValidator(array $param): mixed
{
return \is_callable($param['validator'])
? ($param['validator'])(...$this->getResources($param['injections'] ?? []))
: $param['validator'];
}
protected function getDescriptionContents(?string $description): string
{
if ($description === null || $description === '') {
@@ -379,7 +379,7 @@ class OpenAPI3 extends Format
/**
* @var \Utopia\Validator $validator
*/
$validator = (\is_callable($param['validator'])) ? call_user_func_array($param['validator'], $this->app->getResources($param['injections'])) : $param['validator'];
$validator = $this->getValidator($param);
$node = [
'name' => $name,
@@ -381,9 +381,7 @@ class Swagger2 extends Format
}
/** @var Validator $validator */
$validator = (\is_callable($param['validator']))
? ($param['validator'])(...$this->app->getResources($param['injections']))
: $param['validator'];
$validator = $this->getValidator($param);
$node = [
'name' => $name,
@@ -0,0 +1,46 @@
<?php
namespace Tests\Unit\SDK\Specification;
use Appwrite\SDK\Specification\Format;
use PHPUnit\Framework\TestCase;
use Utopia\DI\Container;
use Utopia\Validator\Text;
class FormatTest extends TestCase
{
public function testResolvesCallableValidatorResourcesFromContainer(): void
{
$container = new Container();
$container->set('first', fn () => 'alpha');
$container->set('second', fn () => 'beta');
$format = new class($container, [], [], [], [], 0, APP_SDK_PLATFORM_SERVER) extends Format {
public function getName(): string
{
return 'stub';
}
public function parse(): array
{
return [];
}
public function resolveValidatorForTest(array $param): mixed
{
return $this->getValidator($param);
}
};
$validator = $format->resolveValidatorForTest([
'validator' => fn (string $first, string $second) => new Text(
($first === 'alpha' && $second === 'beta') ? 9 : 1
),
'injections' => ['second', 'first'],
]);
$this->assertInstanceOf(Text::class, $validator);
$this->assertTrue($validator->isValid('123456789'));
$this->assertFalse($validator->isValid('1234567890'));
}
}