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

Fix adapter.Unwrap() not working after huma.WithContext() #829

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jun 18, 2025

Conversation

Serjlee
Copy link
Contributor

@Serjlee Serjlee commented May 26, 2025

Closes #790
Closes #784

Since I was also affected by this issue I decided to open a PR

I used the (very naive) strategy of exposing a func to extract the original huma.Context from a subContext, and made use of it in every adapter.Unwrap(ctx) func.

I updated the adapters test to make sure all adapters don't panic while adding a simple context value (and added the missing humago test case)

While updating the test I also discovered that humabunrouter.Unwrap(ctx) simply doesn't work with Bun in compat mode. I didn't fix it since it's not really related to this change, and I wasn't sure on the preferred way (duplicated func? new package? return *http.Request for both context implementations?)

@Copilot Copilot AI review requested due to automatic review settings May 26, 2025 13:12
Copy link
Contributor

@Copilot 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 fixes the issue with adapter.Unwrap() after using huma.WithContext() by exposing the original context through a new API. Key changes include the addition of HumaContext and OriginalContext functions in api.go, and updating each adapter’s Unwrap method to use OriginalContext. The tests have also been updated to ensure that using huma.WithContext() does not cause any panics when unwrapping.

Reviewed Changes

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

Show a summary per file
File Description
api.go Added HumaContext and OriginalContext methods to extract the original context.
adapters/humamux/humamux.go Updated Unwrap to call OriginalContext for correct type assertion.
adapters/humahttprouter/humahttprouter.go Updated Unwrap using OriginalContext for httprouter adapter.
adapters/humago/humago.go Updated Unwrap using OriginalContext for humago adapter.
adapters/humagin/humagin.go Updated Unwrap using OriginalContext for gin adapter.
adapters/humaflow/humaflow.go Updated Unwrap using OriginalContext for humaflow adapter.
adapters/humafiber/humafiber.go Updated Unwrap using OriginalContext for humafiber adapter.
adapters/humaecho/humaecho.go Updated Unwrap using OriginalContext for humaecho adapter.
adapters/humachi/humachi.go Updated Unwrap using OriginalContext for chi adapter.
adapters/humabunrouter/humabunrouter.go Updated Unwrap using OriginalContext (with separate handling in compat mode).
adapters/adapters_test.go Modified test setup to include an unwrapper function and added a new humago test case.

Copy link
Contributor

@costela costela left a comment

Choose a reason for hiding this comment

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

Sorry for the drive-by review 🙈

Personally I'm not sure about increasing the package API for this.

Wouldn't extending the Context interface with Context.WithValue (requiring the individual adapters to implement their own self-wrapping) be a bit simpler for the consumer?

@Serjlee
Copy link
Contributor Author

Serjlee commented Jun 9, 2025

Sorry for the drive-by review 🙈

Personally I'm not sure about increasing the package API for this.

Wouldn't extending the Context interface with Context.WithValue (requiring the individual adapters to implement their own self-wrapping) be a bit simpler for the consumer?

I actually thought about that, but I was worried about introducing a breaking change, both for avoiding annoyances to other lib users and to increase the likelyhood of a merge

Since the Unwrap issue is currently affecting at least me and a couple of other people, I'd love to have some patch-level release in the near future, then a more well-thought solution in a later release

I didn't look too far into such a solution, but if @danielgtaylor is OK with changing the Context interface without having to bump to v3 I'd gladly take a spin at it and replace this PR!

Copy link

codecov bot commented Jun 18, 2025

Codecov Report

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

Project coverage is 93.01%. Comparing base (2671cf1) to head (42c87a8).
Report is 8 commits behind head on main.

Files with missing lines Patch % Lines
api.go 0.00% 2 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #829      +/-   ##
==========================================
- Coverage   93.04%   93.01%   -0.04%     
==========================================
  Files          23       23              
  Lines        5392     5394       +2     
==========================================
  Hits         5017     5017              
- Misses        319      321       +2     
  Partials       56       56              

☔ 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.

@danielgtaylor
Copy link
Owner

danielgtaylor commented Jun 18, 2025

Okay, good feedback @costela and I agree for now we can do this without the new public function call. I've updated the code to mimic the standard library errors package with optional Unwrap() Context support which the individual adapters all use and the sub context implements. Some code is duplicated in the adapters but that keeps our dependency graph clean. Hope this works for you @Serjlee!

@danielgtaylor danielgtaylor merged commit c1c4f3e into danielgtaylor:main Jun 18, 2025
2 of 4 checks passed
@Serjlee
Copy link
Contributor Author

Serjlee commented Jun 19, 2025

Interesting implementation, yeah, it works for me. Thank you!

@Serjlee Serjlee deleted the fix-unwrap branch June 19, 2025 06:09
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.

SubContext breaks unwrapping huma.WithContext() destroys original humago Context
3 participants