Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ability to exclude routes from OpenAPI docs #984

Merged
merged 4 commits into from
Feb 28, 2025
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/hot-moles-fix.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@hono/zod-openapi': patch
---

Add ability to exclude specific routes from OpenAPI docs
11 changes: 11 additions & 0 deletions packages/zod-openapi/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -451,6 +451,17 @@ app.doc('/doc', (c) => ({
}))
```

### How to exclude a specific route from OpenAPI docs

You can use `hide` property as follows:

```ts
const route = createRoute({
// ...
hide: true,
})
```

## Limitations

### Combining with `Hono`
Expand Down
7 changes: 5 additions & 2 deletions packages/zod-openapi/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ type MaybePromise<T> = Promise<T> | T

export type RouteConfig = RouteConfigBase & {
middleware?: MiddlewareHandler | MiddlewareHandler[]
hide?: boolean
}

type RequestTypes = {
Expand Down Expand Up @@ -418,7 +419,7 @@ export class OpenAPIHono<
InputTypeJson<R>,
P extends string = ConvertPathType<R['path']>
>(
{ middleware: routeMiddleware, ...route }: R,
{ middleware: routeMiddleware, hide, ...route }: R,
handler: Handler<
// use the env from the middleware if it's defined
R['middleware'] extends MiddlewareHandler[] | MiddlewareHandler
Expand Down Expand Up @@ -462,7 +463,9 @@ export class OpenAPIHono<
S & ToSchema<R['method'], MergePath<BasePath, P>, I, RouteConfigToTypedResponse<R>>,
BasePath
> => {
this.openAPIRegistry.registerPath(route)
if (!hide) {
this.openAPIRegistry.registerPath(route)
}

const validators: MiddlewareHandler[] = []

Expand Down
45 changes: 45 additions & 0 deletions packages/zod-openapi/test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1923,3 +1923,48 @@ describe('Generate YAML', () => {
expect(() => stringify(doc)).to.not.throw()
})
})

describe('Hide Routes', () => {
it('Should hide the route', async () => {
const app = new OpenAPIHono()
app.openapi(
createRoute({
method: 'get',
hide: true,
path: '/books',
responses: {
200: {
description: 'Books',
content: {
'application/json': {
schema: z.array(
z.object({
title: z.string(),
})
),
},
},
},
},
}),
(c) => c.json([{ title: 'foo' }])
)
const doc = app.getOpenAPIDocument({
openapi: '3.0.0',
info: {
title: 'My API',
version: '1.0.0',
},
})

const doc31 = app.getOpenAPI31Document({
openapi: '3.1.0',
info: {
title: 'My API',
version: '1.0.0',
},
})
expect(doc.paths).not.toHaveProperty('/books')
expect(doc31.paths).not.toHaveProperty('/books')
})
})