From 91ad8cd94c67d5f5d06a72fa624312f80f0bc0f9 Mon Sep 17 00:00:00 2001 From: ildar Date: Tue, 2 Jun 2026 12:54:01 +0300 Subject: [PATCH] fix: avoid chi route context reuse --- autopatch/autopatch.go | 27 +++++++++++++++++++++++---- 1 file changed, 23 insertions(+), 4 deletions(-) diff --git a/autopatch/autopatch.go b/autopatch/autopatch.go index 1c21588f..0aa17999 100644 --- a/autopatch/autopatch.go +++ b/autopatch/autopatch.go @@ -10,6 +10,7 @@ package autopatch import ( "bytes" + "context" "encoding/json" "errors" "io" @@ -20,10 +21,12 @@ import ( "strconv" "strings" - "github.com/danielgtaylor/huma/v2" - "github.com/danielgtaylor/huma/v2/casing" "github.com/danielgtaylor/shorthand/v2" jsonpatch "github.com/evanphx/json-patch/v5" + "github.com/go-chi/chi/v5" + + "github.com/danielgtaylor/huma/v2" + "github.com/danielgtaylor/huma/v2/casing" ) const MergePatchNullabilityExtension = "x-merge-patch-nullability" @@ -43,6 +46,22 @@ func RegisterNullabilityExtension(api huma.API, stringRepresentationOfNull strin } } +type internalRequestContext struct { + context.Context +} + +func (c internalRequestContext) Value(key any) any { + if key == chi.RouteCtxKey { + return nil + } + + return c.Context.Value(key) +} + +func autopatchRequestContext(ctx context.Context) context.Context { + return internalRequestContext{Context: ctx} +} + func replaceNulls(data []byte, settings MergePatchNullabilitySettings) ([]byte, error) { var raw map[string]any if err := json.Unmarshal(data, &raw); err != nil { @@ -264,7 +283,7 @@ func PatchResource(api huma.API, path *huma.PathItem) { resourcePath := findRelativeResourcePath(ctx.URL().Path, put.Path) // Perform the get! - origReq, err := http.NewRequestWithContext(ctx.Context(), http.MethodGet, resourcePath, nil) + origReq, err := http.NewRequestWithContext(autopatchRequestContext(ctx.Context()), http.MethodGet, resourcePath, nil) if err != nil { huma.WriteErr(api, ctx, http.StatusInternalServerError, "Unable to get resource", err) return @@ -390,7 +409,7 @@ func PatchResource(api huma.API, path *huma.PathItem) { } // Write the updated data back to the server! - putReq, err := http.NewRequestWithContext(ctx.Context(), http.MethodPut, resourcePath, bytes.NewReader(patched)) + putReq, err := http.NewRequestWithContext(autopatchRequestContext(ctx.Context()), http.MethodPut, resourcePath, bytes.NewReader(patched)) if err != nil { huma.WriteErr(api, ctx, http.StatusInternalServerError, "Unable to put modified resource", err) return