diff --git a/generate-spec.php b/generate-spec.php index 1baa394..b31728d 100755 --- a/generate-spec.php +++ b/generate-spec.php @@ -940,6 +940,8 @@ } } +$usedSchemas = ['Capabilities', 'PublicCapabilities']; + foreach ($scopePaths as $scope => $paths) { $openapiScope = $openapi; @@ -955,29 +957,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,11 +988,12 @@ $newRefs = Helpers::collectUsedRefs($schemas[$schemaName]); foreach ($newRefs as $newRef) { if (!isset($scopedSchemas[substr((string)$newRef, strlen('#/components/schemas/'))])) { - $usedSchemas[] = $newRef; + $usedRefs[] = $newRef; } } $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!'); } 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 @@ +