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

hono/combine compatibility #21

Open
oscartbeaumont opened this issue Dec 15, 2024 · 4 comments
Open

hono/combine compatibility #21

oscartbeaumont opened this issue Dec 15, 2024 · 4 comments
Labels

Comments

@oscartbeaumont
Copy link

It would be really nice if you could somehow use hono/combine with the hono-openapi middleware. In my case I want to build an opinionated high-level abstraction to reduce the boilerplate on each route.

Currently this does not work because the OpenAPI schema is stored to the prototype of the middleware function and wrapping it causes this information to be lost.

I suspect the easiest way to support this would be for hono-openapi to expose it's own hono-openapi/combine module because I think alternatives would require the OpenAPI schema's to be stored somewhere else and I am not sure if any other options exists that would be viable.

Thanks for this awesome project!

@MathurAditya724
Copy link
Member

This is a good idea, but implementing it poses significant challenges without relying on some form of global storage. Even if we attempt to create a version of hono/combine tailored for this purpose, it would likely fall outside the scope of this project.

The main issue lies in how we generate the OpenAPI specs. The current approach relies on static generation, which doesn’t account for dynamic runtime conditions like those defined in some or every. These conditions require running the middleware functions to determine which validations are applied—a process that's not feasible during static spec generation.

Moreover, even if we manage to address this, there’s a high risk the final generated spec won’t comply with OpenAPI standards, given the complexities of conditional middleware.

That said, I’d love to explore this further if you have a concrete proposal or workaround in mind. Feel free to submit a PR with your idea

@mordechaim
Copy link

mordechaim commented Jan 16, 2025

I'm having this exact issue, I want to create a higher level opinionated abstraction. Would love to see this solved.

Thanks for this great library


EDIT: I figured for validator() I can hook into the middleware via the 3rd argument. Perhaps something similar for describeRoute would work.

@MathurAditya724
Copy link
Member

MathurAditya724 commented Jan 20, 2025

Can you share a use case or an example of how you wanna use it?

@mordechaim
Copy link

mordechaim commented Jan 20, 2025

Sure, so I created accepts(schema) and returns(schema) middleware. I apply it to my routes and it automatically validates every request and response and parses the body into a Hono context variable. The API footprint is very light, and straightforward.

Now, when I discovered this library (as recommended by Hono docs) I wanted to superimpose the openapi part over the accepts and returns middleware without the ceremony of defining the status code (always 200 unless errors), return type (always json) and validation parts, I liked the existing API surface.

I tried wrapping the openapi logic in my own middleware but, reading the code, I saw it hooks into the middleware via a uniqueSymbol hook.

I ended up using a middleware factory function which simply returns the openapi validator with a hook to parse the body and some other logic, and the returns middleware is just a simplified wrapper over the defineRoute without the ceremony.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants