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

Conversation

rmichalak
Copy link
Contributor

Copy link

changeset-bot bot commented Feb 27, 2025

🦋 Changeset detected

Latest commit: f082120

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@hono/zod-openapi Minor

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@yusukebe
Copy link
Member

Hi @rmichalak

What use case is this feature? I think it's okay to use just app.get():

app.get('/books', (c) => c.json([{ title: 'foo' }]))

@rmichalak
Copy link
Contributor Author

Hi @yusukebe,

What use case is this feature? I think it's okay to use just app.get():

Will I still have all the type-safety and validation coming from the openapi specification?

@yusukebe
Copy link
Member

@rmichalak

The response can't be typed, but you can use @hono/zod-validator to validate and add types for requests.

@rmichalak
Copy link
Contributor Author

rmichalak commented Feb 28, 2025

@yusukebe

The response can't be typed, but you can use @hono/zod-validator to validate and add types for requests.

Well, but that would mean I need to write more code and use a package which I currently don't use explicitly (although I know it's used behind the scenes). On the other hand, with my change I can use exactly the same patterns as with any other endpoint.

Plus it's easier to expose the endpoint dynamically in runtime, e.g. for specific environments:

hide: process.env.ENV !== "test"

@yusukebe
Copy link
Member

@rmichalak

I still can't understand why this feature is needed. Is the following not good?

const app = new OpenAPIHono()
if (condition) {
  app.openapi(
    createRoute({
      /** */
    }),
    (c) => c.json([{ title: 'foo' }])
  )
}

@rmichalak
Copy link
Contributor Author

@yoshikouki but I do want the endpoint to be available for requests, just hidden from the docs. Let's imagine a new endpoint which we want to test internally, but not announce publicly yet. With your approach, when the condition is met, the endpoint will be both available and listed in OpenAPI docs. When the condition is not met, the endpoint with not even be deployed.

I want to achieve something in the middle - deployed but not listed.

@yusukebe
Copy link
Member

@rmichalak

Thank you for the explanation. I fully understand! The code is good, but I've added the comment.

Copy link
Member

@yusukebe yusukebe left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@yusukebe
Copy link
Member

@rmichalak

Looks good! I'll merge and release a new version. Thank you for your contribution!

@yusukebe yusukebe merged commit 59c6356 into honojs:main Feb 28, 2025
1 check passed
@github-actions github-actions bot mentioned this pull request Feb 28, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants