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

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

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.

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
1 participant