huma icon indicating copy to clipboard operation
huma copied to clipboard

Feedback

Open festo opened this issue 11 months ago • 3 comments

First of all, thank you for the great work @danielgtaylor. Huma has a few clever solutions and features that I miss from other frameworks. However, it has some cons as well. It tries to be router agnostic but enforces structures that are not suitable for all.

I was trying to use an existing middleware and discovered several issues related to route groups. If I use a chi middleware it gets applied for all the routes, if I define a route group, it breaks the docs ... all of this could be fixed with workarounds and hacks but I would expect more flexibility from a framework like this. This is definitely something that bothers others as well, I quickly found an issue and an extension that address the same problems. This blocks me from using this framework for bigger projects.

Unfortunately, I don't have a working proposal on how to fix all of this, maybe the community should consider merging Hureg into Huma

Edit: I know if I would rewrite those middlewares and attach them to each route one by one my issue would be solved, and probably that's what I will do, but I don't like to repeat myself, those middleware work, and I will less likely forget to attach them if I attach them to the whole group and not for each endpoint.

festo avatar Dec 26 '24 21:12 festo

Edit: I know if I would rewrite those middlewares and attach them to each route one by one my issue would be solved, and probably that's what I will do, but I don't like to repeat myself, those middleware work, and I will less likely forget to attach them if I attach them to the whole group and not for each endpoint.

When I ended up doing was defining slices of middlewares and then composing them:

aMiddlewareGroup := huma.Middlewares{
// define some middlewares here
}

anotherMiddlewareGroup := huma.Middlewares{
// define some middlewares here
}

huma.Register(api, huma.Operation{
	Method: http.MethodGet,
	Path:   "some_path",
	Middlewares: huma.Middlewares(
		slices.Concat(
			aMiddlewareGroup,
			anotherMiddlewareGroup,
		),
	),
}, SomeHandler)

Not perfect, but pretty easy to compose.

arolek-glovebox avatar Jan 10 '25 20:01 arolek-glovebox

This is good feedback to have, thanks! I'll look into that extension a bit and see if there is something that could be done to make this simpler. Definitely open to having help on this as I rarely use per-operation middlewares.

danielgtaylor avatar Jan 20 '25 19:01 danielgtaylor

@festo @arolek-glovebox please take and look at #736 & #728 and let me know if this addresses some of your concerns and makes things easier.

danielgtaylor avatar Feb 19 '25 18:02 danielgtaylor