oapi-codegen icon indicating copy to clipboard operation
oapi-codegen copied to clipboard

Using per-endpoint middleware

Open screwyprof opened this issue 3 years ago • 6 comments

At the moment when RegisterHandlersWithBaseURL is generated there is no way to specify per-handler middleware. In our use case we need to use some middleware's on per-handler bases rather than global ones.

The EchoRouter interface has a 3rd param to pass on the middleware:

type EchoRouter interface {
	CONNECT(path string, h echo.HandlerFunc, m ...echo.MiddlewareFunc) *echo.Route
	DELETE(path string, h echo.HandlerFunc, m ...echo.MiddlewareFunc) *echo.Route
	GET(path string, h echo.HandlerFunc, m ...echo.MiddlewareFunc) *echo.Route
	HEAD(path string, h echo.HandlerFunc, m ...echo.MiddlewareFunc) *echo.Route
	OPTIONS(path string, h echo.HandlerFunc, m ...echo.MiddlewareFunc) *echo.Route
	PATCH(path string, h echo.HandlerFunc, m ...echo.MiddlewareFunc) *echo.Route
	POST(path string, h echo.HandlerFunc, m ...echo.MiddlewareFunc) *echo.Route
	PUT(path string, h echo.HandlerFunc, m ...echo.MiddlewareFunc) *echo.Route
	TRACE(path string, h echo.HandlerFunc, m ...echo.MiddlewareFunc) *echo.Route
}

However there's no way to let RegisterHandlersWithBaseURL know about it. Is there any way to do it without having a global middleware which has to check whether it can be run for the given request?

screwyprof avatar Feb 07 '22 14:02 screwyprof

Hey @screwyprof there is a started solution in 462

valensto avatar Feb 22 '22 18:02 valensto

Hi @valensto. Thanks for letting me know. Are there any plans to support echo?

screwyprof avatar Feb 23 '22 01:02 screwyprof

I think #462 is abandoned, i started another take on this issue here: #947

festinuz avatar Jan 28 '23 12:01 festinuz

Apologies for missing this folks.

The proposals in #462 and #947 look like a reasonable solution - at a first glance. I can try and come back to this early next year when I've got a bit more time to review + possibly propose some changes.

Related: https://github.com/getkin/kin-openapi/issues/825

jamietanna avatar Dec 13 '23 11:12 jamietanna