这是indexloc提供的服务,不要输入任何密码
Skip to content

Conversation

@danielgtaylor
Copy link
Owner

This PR is an alternative to #699 which enables unwrapping a huma.Context back into its router-specific request/response object or router-specific context, effectively escaping Huma and giving back full control to the user. It's generally recommended to not do this, but there are legitimate use-cases where this could be handy to have as an option. Example:

router := http.NewServeMux()
api := humago.New(router, huma.DefaultConfig("My API", "1.0.0"))

api.UseMiddleware(func(ctx huma.Context, next func(huma.Context)) {
	r, w := humago.Unwrap(ctx)
	fmt.Println("Request started", r.Method, r.URL)
	w.Header().Set("X-Request-Start", time.Now().Format(time.RFC3339))
	next(ctx)
})

This implementation does not modify any interfaces in Huma itself, nor does it require manually casting any or things like that. Each adapter simply provides a convenience Unwrap() function that returns the underlying data.

This should also make it fairly simple to switch routers as you can find/replace the router-specific package (e.g. humachihumago) and things will just work or provide a compile-time error if the return types changed, making it easy to fix, instead of waiting for a runtime error.

@codecov
Copy link

codecov bot commented Feb 19, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 92.90%. Comparing base (68c578f) to head (3aec955).
Report is 5 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main     #736   +/-   ##
=======================================
  Coverage   92.90%   92.90%           
=======================================
  Files          22       22           
  Lines        5174     5174           
=======================================
  Hits         4807     4807           
  Misses        315      315           
  Partials       52       52           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot reviewed 5 out of 12 changed files in this pull request and generated 1 comment.

Files not reviewed (7)
  • adapters/humago/humago.go: Evaluated as low risk
  • docs/docs/features/request-resolvers.md: Evaluated as low risk
  • adapters/humabunrouter/humabunrouter.go: Evaluated as low risk
  • adapters/humachi/humachi.go: Evaluated as low risk
  • adapters/humamux/humamux.go: Evaluated as low risk
  • adapters/humagin/humagin.go: Evaluated as low risk
  • adapters/humaecho/humaecho.go: Evaluated as low risk
Comments suppressed due to low confidence (2)

adapters/humaflow/humaflow.go:28

  • The panic message 'not a humaflow context' is unclear. It should be more descriptive, such as 'context does not implement humaflow.ContextUnwrapper'.
panic("not a humaflow context")

adapters/humafiber/humafiber.go:27

  • The panic message should be more specific. Consider changing it to 'not a humafiber context'.
panic("context does not implement humafiber.ContextUnwrapper")

@danielgtaylor danielgtaylor mentioned this pull request Feb 19, 2025
@excavador
Copy link
Contributor

@danielgtaylor I am fine with these changes, humefiber adapter will work well (as long as nobody will capture *fiber.Ctx or underlying structures outside of the handlers)

@danielgtaylor danielgtaylor merged commit 5720ad0 into main Feb 24, 2025
7 checks passed
@danielgtaylor danielgtaylor deleted the unwrap branch February 24, 2025 16:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants