mirror of
https://github.com/pterodactyl/panel.git
synced 2026-12-04 21:35:47 -05:00
openapi baseline
This commit is contained in:
@@ -129,6 +129,7 @@
|
||||
],
|
||||
"docs:openapi": "@php artisan scribe:generate --force --no-upgrade-check",
|
||||
"docs:openapi:test": "@php vendor/bin/pest --bootstrap vendor/autoload.php tests/Architecture/OpenApiContractTest.php",
|
||||
"docs:openapi:baseline": "@php vendor/bin/pest --bootstrap vendor/autoload.php tests/Architecture/OpenApiBaselineTest.php",
|
||||
"docs:openapi:responses": [
|
||||
"@putenv OPENAPI_VALIDATE=1",
|
||||
"@php -d memory_limit=2G vendor/bin/pest --testsuite Integration"
|
||||
|
||||
@@ -0,0 +1,86 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Pterodactyl\Tests\Pest\Architecture\OpenApiBaselineTest;
|
||||
|
||||
use Pterodactyl\Tests\Support\OpenApiSpecValidator;
|
||||
use Pterodactyl\Tests\TestCase;
|
||||
use stdClass;
|
||||
use Symfony\Component\Yaml\Yaml;
|
||||
|
||||
/*
|
||||
* Pins the generated OpenAPI document to a committed baseline, so changing how responses are
|
||||
* produced (the Fractal to API Resources port) cannot change the documented contract without
|
||||
* a visible diff. Regenerate the specification and the clients first, then run this test:
|
||||
*
|
||||
* npm run api:refresh && npm run sdk:generate
|
||||
* composer docs:openapi:baseline
|
||||
*
|
||||
* The TypeScript generated from the specification is committed, so the other half of the
|
||||
* check is that the regeneration above leaves it untouched:
|
||||
*
|
||||
* git diff --exit-code -- resources/scripts/api/generated packages/sdk/types
|
||||
*
|
||||
* Two parts of the document are left out of the comparison because they change on every
|
||||
* generation: example values (Scribe builds them from factory models with random attributes
|
||||
* and the time of generation) and the server URL (APP_URL). Everything else, every path,
|
||||
* parameter, schema, type, nullable flag, enum and description, must match byte for byte.
|
||||
*
|
||||
* A deliberate documentation change is accepted by rewriting the baseline from the
|
||||
* regenerated specification:
|
||||
*
|
||||
* OPENAPI_BASELINE_UPDATE=1 composer docs:openapi:baseline
|
||||
*/
|
||||
|
||||
uses(TestCase::class);
|
||||
|
||||
/**
|
||||
* Drop every example from the document. Maps are parsed as objects so an empty map and an
|
||||
* empty list stay distinct, and a field that is itself called "example" is a key of a
|
||||
* schema's properties rather than an example, so it is kept.
|
||||
*/
|
||||
function withoutExamples(mixed $node, bool $inProperties = false): mixed
|
||||
{
|
||||
if (is_array($node)) {
|
||||
return array_map(fn (mixed $value): mixed => withoutExamples($value), $node);
|
||||
}
|
||||
|
||||
if (! $node instanceof stdClass) {
|
||||
return $node;
|
||||
}
|
||||
|
||||
$result = new stdClass;
|
||||
foreach (get_object_vars($node) as $key => $value) {
|
||||
if (! $inProperties && in_array($key, ['example', 'examples'], true)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$result->{$key} = withoutExamples($value, ! $inProperties && $key === 'properties');
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
function normalizedSpecification(string $path): string
|
||||
{
|
||||
$specification = Yaml::parseFile($path, Yaml::PARSE_OBJECT_FOR_MAP);
|
||||
expect($specification)->toBeInstanceOf(stdClass::class);
|
||||
unset($specification->servers);
|
||||
|
||||
return Yaml::dump(withoutExamples($specification), 64, 2, Yaml::DUMP_OBJECT_AS_MAP | Yaml::DUMP_EMPTY_ARRAY_AS_SEQUENCE);
|
||||
}
|
||||
|
||||
test('the generated OpenAPI specification matches the committed baseline', function (): void {
|
||||
$generated = OpenApiSpecValidator::specPath();
|
||||
$baseline = base_path('tests/Fixtures/openapi/baseline.yaml');
|
||||
|
||||
expect($generated)->toBeFile('Generate the specification with npm run api:refresh first.');
|
||||
|
||||
if (env('OPENAPI_BASELINE_UPDATE')) {
|
||||
file_put_contents($baseline, normalizedSpecification($generated));
|
||||
}
|
||||
|
||||
expect($baseline)->toBeFile()
|
||||
->and(normalizedSpecification($generated))->toBe(file_get_contents($baseline), 'The generated OpenAPI specification differs from tests/Fixtures/openapi/baseline.yaml.');
|
||||
});
|
||||
File diff suppressed because it is too large
Load Diff
Reference in New Issue
Block a user