Skip to content

Commit 8f5e4b8

Browse files
committed
🔧 fix: exclude.paths is not working when using RegExp
1 parent 070919e commit 8f5e4b8

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

‎src/openapi.ts‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -314,7 +314,12 @@ export function toOpenAPISchema(
314314

315315
if (
316316
(excludeStaticFile && route.path.includes('.')) ||
317-
excludePaths.includes(route.path) ||
317+
excludePaths.some(match => {
318+
if (typeof match === 'string') {
319+
return match === route.path
320+
}
321+
return match.exec(route.path);
322+
}) ||
318323
excludeMethods.includes(method)
319324
)
320325
continue

‎test/index.test.ts‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,4 +272,28 @@ describe('Swagger', () => {
272272
const response = await res.json()
273273
expect(Object.keys(response.paths['/all'])).toBeArrayOfSize(8)
274274
})
275+
276+
// https://github.com/elysiajs/elysia-openapi/issues/275
277+
it.only('should exclude entry points excluded by `exclude.path` option', async () => {
278+
const app = new Elysia()
279+
.use(
280+
openapi({
281+
exclude: {
282+
paths: [/^\/v1/, "/v2"],
283+
},
284+
})
285+
)
286+
.get("/", () => "index")
287+
.get("/v1", () => "v1")
288+
.get("/v1/foo", () => "v1")
289+
.get("/v2", () => "v2")
290+
.listen(3000);
291+
292+
await app.modules
293+
294+
const res = await app.handle(req('/openapi/json'))
295+
expect(res.status).toBe(200)
296+
const response = await res.json()
297+
expect(Object.keys(response.paths)).toStrictEqual(["/"])
298+
})
275299
})

0 commit comments

Comments
 (0)