diff --git a/src/runtime/internal/routes/openapi.ts b/src/runtime/internal/routes/openapi.ts index 8aa4b7f4ae..db9092c48a 100644 --- a/src/runtime/internal/routes/openapi.ts +++ b/src/runtime/internal/routes/openapi.ts @@ -9,6 +9,9 @@ import type { import { joinURL } from "ufo"; import { handlersMeta } from "#nitro-internal-virtual/server-handlers-meta"; import { useRuntimeConfig } from "../config"; +import { createRouter as createRadixRouter, toRouteMatcher } from "radix3"; +import type { NitroRouteRules, NitroRuntimeConfig } from "nitropack/types"; +import defu from "defu"; // Served as /_openapi.json export default eventHandler((event) => { @@ -36,14 +39,27 @@ export default eventHandler((event) => { variables: {}, }, ], - paths: getPaths(), + paths: getPaths(runtimeConfig), }; }); -function getPaths(): PathsObject { +function getPaths(runtimeConfig: NitroRuntimeConfig): PathsObject { const paths: PathsObject = {}; + const _routeRulesMatcher = toRouteMatcher( + createRadixRouter({ routes: runtimeConfig.nitro.routeRules }) + ); + + const _getRouteRules = (path: string) => + defu({}, ..._routeRulesMatcher.matchAll(path).reverse()) as NitroRouteRules; + for (const h of handlersMeta) { + const enabledInRouteMeta = h.meta && h.meta.openAPIEnabled; + const enabledInRouteRules = + h.route && _getRouteRules(h.route).openAPIEnabled; + const openAPIEnabled = enabledInRouteMeta ?? enabledInRouteRules; + if (openAPIEnabled === false) continue; + const { route, parameters } = normalizeRoute(h.route || ""); const tags = defaultTags(h.route || ""); const method = (h.method || "get").toLowerCase() as Lowercase; diff --git a/src/types/handler.ts b/src/types/handler.ts index db2493ee7a..13fcafd165 100644 --- a/src/types/handler.ts +++ b/src/types/handler.ts @@ -7,6 +7,7 @@ type MaybeArray = T | T[]; /** @exprerimental */ export interface NitroRouteMeta { openAPI?: OperationObject; + openAPIEnabled?: boolean; } export interface NitroEventHandler { diff --git a/src/types/route-rules.ts b/src/types/route-rules.ts index 50771724b6..7d7b2445d9 100644 --- a/src/types/route-rules.ts +++ b/src/types/route-rules.ts @@ -11,6 +11,7 @@ export interface NitroRouteConfig { prerender?: boolean; proxy?: string | ({ to: string } & ProxyOptions); isr?: number /* expiration */ | boolean | VercelISRConfig; + openAPIEnabled?: boolean; // Shortcuts cors?: boolean;