From 1a0c7d91f7f24a8f2906a9abfb252fcc2b18426f Mon Sep 17 00:00:00 2001 From: provokateurin Date: Tue, 17 Dec 2024 14:56:54 +0100 Subject: [PATCH 1/3] refactor(generate-spec): Use correct variable name for used refs Signed-off-by: provokateurin --- generate-spec.php | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/generate-spec.php b/generate-spec.php index 1baa394..02ad11a 100755 --- a/generate-spec.php +++ b/generate-spec.php @@ -955,29 +955,29 @@ $openapiScope['paths'] = array_merge(...$fullScopePathArrays); $openapiScope['components']['schemas'] = $schemas; } else { - $usedSchemas = []; + $usedRefs = []; foreach ($paths as $urlRoutes) { foreach ($urlRoutes as $routeData) { foreach ($routeData['responses'] as $responseData) { if (isset($responseData['content']) && $responseData['content'] !== []) { - $usedSchemas[] = Helpers::collectUsedRefs($responseData['content']); + $usedRefs[] = Helpers::collectUsedRefs($responseData['content']); } } if (isset($routeData['requestBody']['content']) && $routeData['requestBody']['content'] !== []) { - $usedSchemas[] = Helpers::collectUsedRefs($routeData['requestBody']['content']); + $usedRefs[] = Helpers::collectUsedRefs($routeData['requestBody']['content']); } } } - $usedSchemas = array_merge(...$usedSchemas); + $usedRefs = array_merge(...$usedRefs); $scopedSchemas = []; - while ($usedSchema = array_shift($usedSchemas)) { - if (!str_starts_with((string)$usedSchema, '#/components/schemas/')) { + while ($usedRef = array_shift($usedRefs)) { + if (!str_starts_with((string)$usedRef, '#/components/schemas/')) { continue; } - $schemaName = substr((string)$usedSchema, strlen('#/components/schemas/')); + $schemaName = substr((string)$usedRef, strlen('#/components/schemas/')); if (!isset($schemas[$schemaName])) { Logger::error('app', "Schema $schemaName used by scope $scope is not defined"); @@ -986,7 +986,7 @@ $newRefs = Helpers::collectUsedRefs($schemas[$schemaName]); foreach ($newRefs as $newRef) { if (!isset($scopedSchemas[substr((string)$newRef, strlen('#/components/schemas/'))])) { - $usedSchemas[] = $newRef; + $usedRefs[] = $newRef; } } From 3378afdabb098d8851fa5a1daf79fb23c149af32 Mon Sep 17 00:00:00 2001 From: provokateurin Date: Tue, 17 Dec 2024 14:57:03 +0100 Subject: [PATCH 2/3] feat(generate-spec): Disallow unused schemas Signed-off-by: provokateurin --- generate-spec.php | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/generate-spec.php b/generate-spec.php index 02ad11a..b31728d 100755 --- a/generate-spec.php +++ b/generate-spec.php @@ -940,6 +940,8 @@ } } +$usedSchemas = ['Capabilities', 'PublicCapabilities']; + foreach ($scopePaths as $scope => $paths) { $openapiScope = $openapi; @@ -991,6 +993,7 @@ } $scopedSchemas[$schemaName] = $schemas[$schemaName]; + $usedSchemas[] = $schemaName; } if (isset($schemas['Capabilities'])) { @@ -1031,6 +1034,11 @@ Logger::info('app', 'Generated scope ' . $scope . ' with ' . $pathsCount . ' routes!'); } +$unusedSchemas = array_diff(array_keys($schemas), $usedSchemas); +if ($unusedSchemas !== []) { + Logger::error('app', 'Unused schemas: ' . implode(', ', $unusedSchemas)); +} + if (Logger::$errorCount > 0) { Logger::panic('app', 'Encountered ' . Logger::$errorCount . ' errors that need to be fixed!'); } From 86dfdc3fd8ce2752a1d39cade17c106d0b56b923 Mon Sep 17 00:00:00 2001 From: provokateurin Date: Tue, 17 Dec 2024 17:23:45 +0100 Subject: [PATCH 3/3] test: Add capabilities tests Signed-off-by: provokateurin --- tests/lib/Capabilities.php | 21 +++++++++++++++++ tests/lib/PublicCapabilities.php | 21 +++++++++++++++++ tests/openapi-administration.json | 39 +++++++++++++++++++++++++++++++ tests/openapi-ex_app.json | 39 +++++++++++++++++++++++++++++++ tests/openapi-federation.json | 39 +++++++++++++++++++++++++++++++ tests/openapi-full.json | 39 +++++++++++++++++++++++++++++++ tests/openapi.json | 39 +++++++++++++++++++++++++++++++ 7 files changed, 237 insertions(+) create mode 100644 tests/lib/Capabilities.php create mode 100644 tests/lib/PublicCapabilities.php diff --git a/tests/lib/Capabilities.php b/tests/lib/Capabilities.php new file mode 100644 index 0000000..fd415d5 --- /dev/null +++ b/tests/lib/Capabilities.php @@ -0,0 +1,21 @@ +