huma icon indicating copy to clipboard operation
huma copied to clipboard

feat: propagate context to adapters on WithContext

Open costela opened this issue 5 months ago • 1 comments

This PR introduces:

  • :sparkles: WithContext methods to adapters' context for proper context propagation. This is then opportunistically used in huma.WithContext to avoid the general subContext.
  • :sparkles: 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 avatar Jul 23 '25 10:07 costela

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 :warning:
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     

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.

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

codecov[bot] avatar Jul 23 '25 10:07 codecov[bot]