oapi-codegen: chi router groups and middleware

Tue, Aug 8, 2023 One-minute read

oapi-codegen: chi router groups and middleware

Today I learned a valuable lession in assumptions.

I assumed that putting the oapivalidator with the router.Group where the endoints live was how it worked. Not realising that zero validation was being done. Not until I started implemented JWT authentication and noticing that no errors were propagating.

Here is the code that works.

router := chi.NewRouter()
// middlewares
router.Use(oapimiddleware.OapiRequestValidator(openapiSpec))
// endpoints
router.Group(func(api chi.Router){
    server.HandlerFromMuxWithBaseURL(s, router, "/api/v1")
})

Tags:

#go #til #aar