Echo v5 router builder for OpenAPI 3 contracts, ported from Vert.x Web's vertx-web-openapi-router behavior.
It builds Echo routes from OpenAPI operations, converts OpenAPI path parameters (/pets/{id}) to Echo path parameters (/pets/:id), mounts generated routes directly into Echo, and installs root middleware, request validation, security checks, route handlers, and route failure handlers. Request validation is delegated to github.com/responsibleapi/echo-middleware at v1.0.3-responsibleapi.3.
go get github.com/responsibleapi/echo-openapi-routerimport (
"net/http"
openapirouter "github.com/responsibleapi/echo-openapi-router""github.com/labstack/echo/v5"
validator "github.com/responsibleapi/echo-middleware"
)builder, err:=openapirouter.LoadFromFile("openapi.yaml", validator.Options{})
iferr!=nil {
returnerr
}
builder.AddRoute("getPet", func(c*echo.Context) error {
returnc.JSON(http.StatusOK, map[string]string{"id": c.PathValue("id")})
})
e:=echo.New()
e.GET("/healthz", func(c*echo.Context) error {
returnc.NoContent(http.StatusNoContent)
})
iferr:=builder.Mount(e); err!=nil {
returnerr
}
returne.Start(":8080")Use builder.MountAt(e, "/api") to mount generated routes under a path prefix. Use builder.CreateRouter() when you want a standalone *echo.Echo.
Security is configured per OpenAPI security scheme:
builder.Security("api_key", func(c*echo.Context, scheme*openapi3.SecurityScheme, scopes []string) error {
ifc.Request().Header.Get(scheme.Name) =="" {
returnecho.NewHTTPError(http.StatusUnauthorized, "missing api key")
}
returnnil
})Only security schemes registered with builder.Security are evaluated by the
router. Unregistered schemes remain in the OpenAPI contract and are treated as
externally enforced, for example by a trusted reverse proxy. They are excluded
from router-side security alternatives, so they cannot bypass registered
security handlers.
Operations without handlers are still mounted and return 501 Not Implemented, matching the Vert.x module's fallback route behavior.