Skip to content

Commit 5303809

Browse files
authored
Merge pull request #137 from kravetsone/main
Exclude routes with details.hide: true from OpenAPI/swagger file
2 parents ca5bdfa + 54c38a9 commit 5303809

File tree

4 files changed

+23
-2
lines changed

4 files changed

+23
-2
lines changed

bun.lockb

0 Bytes
Binary file not shown.

src/index.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,8 +110,10 @@ export const swagger = async <Path extends string = '/swagger'>(
110110

111111
if (routes.length !== totalRoutes) {
112112
totalRoutes = routes.length
113-
113+
114114
routes.forEach((route: InternalRoute) => {
115+
if (route.hooks?.detail?.hide === true) return
116+
// TODO: route.hooks?.detail?.hide !== false add ability to hide: false to prevent excluding
115117
if (excludeMethods.includes(route.method)) return
116118

117119
registerSchemaPath({

src/utils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ export const registerSchemaPath = ({
118118
models: Record<string, TSchema>
119119
}) => {
120120
if (hook) hook = deepClone(hook)
121-
121+
122122
const contentType = hook?.type ?? [
123123
'application/json',
124124
'multipart/form-data',

test/index.test.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,4 +200,23 @@ describe('Swagger', () => {
200200
const response = await res.json()
201201
expect(response.paths).toContainKey('/id/{id}')
202202
})
203+
204+
it('should hide routes with hide = true from paths', async () => {
205+
const app = new Elysia().use(swagger())
206+
.get("/public", "omg")
207+
.guard({
208+
detail: {
209+
hide: true
210+
}
211+
})
212+
.get("/hidden", "ok")
213+
214+
await app.modules
215+
216+
const res = await app.handle(req('/swagger/json'))
217+
expect(res.status).toBe(200)
218+
const response = await res.json()
219+
expect(response.paths['/public']).not.toBeUndefined();
220+
expect(response.paths['/hidden']).toBeUndefined();
221+
})
203222
})

0 commit comments

Comments
 (0)