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

Conversation

@costela
Copy link
Contributor

@costela costela commented Jul 23, 2025

This PR introduces:

  • WithContext methods to adapters' context for proper context propagation. This is then opportunistically used in huma.WithContext to avoid the general subContext.
  • middleware conversion functions for each adapter

The new middleware functions in particular could be exported as utilities. If we do that, we could also deduplicate the new TestWithValueShouldPropagateContext into a single table-driven test across all adapters.

Enhancements to context propagation:

  • Added WithContext methods to propagate context in router-specific context wrappers (bunContext, bunCompatContext, chiContext, echoCtx, fiberWrapper, ginCtx, goContext, and httprouterContext). These methods create new instances of the context wrappers with the updated context. [1] [2] [3] [4] [5] [6] [7] [8]

Middleware handling improvements:

  • Added middleware conversion functions for all router adapters (bunrouter, chi, echo, fiber, gin, go, and httprouter). These functions wrap router-specific middleware into the Huma middleware format, enabling seamless integration. [1] [2] [3] [4] [5] [6]

Testing and validation:

  • Introduced new test cases to verify context propagation using the WithContext method and middleware functionality for each router adapter. These tests ensure that context values are correctly passed through middleware layers and accessible in handlers. [1] [2] [3] [4] [5] [6]

Closes #859

@costela costela force-pushed the leo.antunes/propagate-context-to-adapter branch from cf62e22 to d92df8f Compare July 23, 2025 10:36
@codecov
Copy link

codecov bot commented Jul 23, 2025

Codecov Report

Attention: Patch coverage is 0% with 3 lines in your changes missing coverage. Please review.

Project coverage is 92.50%. Comparing base (df27bef) to head (6ef5c50).
Report is 2 commits behind head on main.

Files with missing lines Patch % Lines
api.go 0.00% 2 Missing and 1 partial ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #867      +/-   ##
==========================================
- Coverage   92.56%   92.50%   -0.06%     
==========================================
  Files          23       23              
  Lines        5511     5514       +3     
==========================================
  Hits         5101     5101              
- Misses        351      353       +2     
- Partials       59       60       +1     

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

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@costela costela marked this pull request as ready for review July 23, 2025 10:44
Copilot AI review requested due to automatic review settings July 23, 2025 10:44
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.

Pull Request Overview

This PR introduces context propagation improvements by adding WithContext methods to all router adapter context wrappers and middleware conversion functions. The key enhancement allows proper context propagation through adapter-specific context handling instead of relying on a general subContext wrapper.

  • Adds WithContext methods to all adapter context types for proper context propagation
  • Introduces middleware conversion functions for each router adapter to enable seamless integration
  • Includes comprehensive test coverage to verify context propagation functionality

Reviewed Changes

Copilot reviewed 17 out of 17 changed files in this pull request and generated 4 comments.

Show a summary per file
File Description
api.go Adds opportunistic WithContext interface check to improve context propagation
adapters/humamux/humamux.go Implements WithContext method and middleware conversion for Gorilla Mux
adapters/humahttprouter/humahttprouter.go Implements WithContext method and middleware conversion for httprouter
adapters/humago/humago.go Implements WithContext method and middleware conversion for Go stdlib
adapters/humagin/humagin.go Implements WithContext method and middleware conversion for Gin
adapters/humafiber/humafiber.go Implements WithContext method and middleware conversion for Fiber
adapters/humaecho/humaecho.go Implements WithContext method and middleware conversion for Echo
adapters/humachi/humachi.go Implements WithContext method and middleware conversion for Chi
adapters/humabunrouter/humabunrouter.go Implements WithContext methods and middleware conversion for BunRouter
Test files Adds context propagation verification tests for each adapter

@costela costela force-pushed the leo.antunes/propagate-context-to-adapter branch 2 times, most recently from 16c161c to 383e38c Compare July 23, 2025 10:48
@costela costela force-pushed the leo.antunes/propagate-context-to-adapter branch from 383e38c to 6ef5c50 Compare July 23, 2025 10:51
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.

huma.WithValue + context.Unwrap loses request context

1 participant