huma icon indicating copy to clipboard operation
huma copied to clipboard

[feature] cannot add headers in error response in openapi doc

Open oliverdding opened this issue 7 months ago • 1 comments

Image

Image

Those header key are set by http middleware, so adding to openapi is the job to be done.

oliverdding avatar Jul 02 '25 03:07 oliverdding

You need to update your middleware then. Use factory pattern, For example, for my routing group, I want to ensure everything need to be authed. So, I wrote my coed as follow

items.UseMiddleware(middleware.HumaAuthMiddleware(items))
func HumaAuthMiddleware(g *huma.Group) func(huma.Context, func(huma.Context)) {
	routes.UseSecurity(g, routes.BearerScheme)
	return wrapHTTPMiddleware(AuthMiddleware)
}
func UseSecurity(g *huma.Group, scheme string) {
	g.UseSimpleModifier(func(o *huma.Operation) {
		if len(o.Security) == 0 {
			o.Security = []map[string][]string{{scheme: {}}}
		}
	})
}

You can do similar for the response header. Just need to tweak the code. I hope this can help you

MeGaNeKoS avatar Aug 02 '25 14:08 MeGaNeKoS