Skip to content

Commit fcd9058

Browse files
authored
fix(humafiber): correct Fiber v2 EachHeader iteration and body decompression (#1058)
Header.All() returns iter.Seq2[[]byte, []byte] yielding (name, value) pairs, but EachHeader ranged the value as a byte slice, invoking the callback once per byte. This broke huma.ReadCookie/ReadCookies, which collect the Cookie header via EachHeader. Range both values instead. Also switch BodyReader from BodyRaw() to Body() to match the Fiber v3 adapter and v2.37.3, so compressed request bodies are decompressed before reaching handlers. Fixes #1055
1 parent 770e618 commit fcd9058

2 files changed

Lines changed: 90 additions & 5 deletions

File tree

‎adapters/humafiber/humafiber_v2.go‎

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -95,10 +95,8 @@ func (c *fiberV2Wrapper) Header(name string) string {
9595
}
9696

9797
func (c *fiberV2Wrapper) EachHeader(cb func(name, value string)) {
98-
for name, values := range c.orig.Request().Header.All() {
99-
for _, value := range values {
100-
cb(string(name), string(value))
101-
}
98+
for name, value := range c.orig.Request().Header.All() {
99+
cb(string(name), string(value))
102100
}
103101
}
104102

@@ -108,7 +106,7 @@ func (c *fiberV2Wrapper) BodyReader() io.Reader {
108106
// Streaming is enabled, so send the reader.
109107
return orig.Request().BodyStream()
110108
}
111-
return bytes.NewReader(orig.BodyRaw())
109+
return bytes.NewReader(orig.Body())
112110
}
113111

114112
func (c *fiberV2Wrapper) GetMultipartForm() (*multipart.Form, error) {

‎adapters/humafiber/humafiber_v2_test.go‎

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,101 @@
11
package humafiber
22

33
import (
4+
"bytes"
5+
"compress/gzip"
46
"context"
7+
"io"
58
"net/http"
69
"testing"
710

811
"github.com/danielgtaylor/huma/v2"
912
fiberV2 "github.com/gofiber/fiber/v2"
13+
"github.com/stretchr/testify/require"
1014
)
1115

16+
// TestFiberV2EachHeaderAndCookie ensures EachHeader yields one callback per
17+
// header value (not one per byte) so header-based helpers like huma.ReadCookie
18+
// continue to work under the Fiber v2 adapter. Regression test for #1055.
19+
func TestFiberV2EachHeaderAndCookie(t *testing.T) {
20+
r := fiberV2.New()
21+
api := NewV2(r, huma.DefaultConfig("Test API", "1.0.0"))
22+
23+
var authValue string
24+
var authCallbacks int
25+
var cookieValue string
26+
var cookieErr error
27+
api.UseMiddleware(func(ctx huma.Context, next func(huma.Context)) {
28+
ctx.EachHeader(func(name, value string) {
29+
if name == "Authorization" {
30+
authCallbacks++
31+
authValue = value
32+
}
33+
})
34+
cookie, err := huma.ReadCookie(ctx, "session")
35+
cookieErr = err
36+
if cookie != nil {
37+
cookieValue = cookie.Value
38+
}
39+
next(ctx)
40+
})
41+
42+
huma.Register(api, huma.Operation{
43+
OperationID: "get-root",
44+
Method: http.MethodGet,
45+
Path: "/",
46+
}, func(ctx context.Context, _ *struct{}) (*struct{}, error) {
47+
return &struct{}{}, nil
48+
})
49+
50+
req, _ := http.NewRequest(http.MethodGet, "http://example.com/", nil)
51+
req.Header.Set("Authorization", "Bearer abc")
52+
req.Header.Set("Cookie", "session=xyz")
53+
_, err := r.Test(req)
54+
require.NoError(t, err)
55+
56+
require.Equal(t, 1, authCallbacks, "EachHeader should fire once per header value, not once per byte")
57+
require.Equal(t, "Bearer abc", authValue)
58+
require.NoError(t, cookieErr)
59+
require.Equal(t, "xyz", cookieValue)
60+
}
61+
62+
// TestFiberV2BodyReaderDecompresses ensures BodyReader returns the decompressed
63+
// request body (Body) rather than the raw compressed bytes (BodyRaw), so
64+
// handlers receive usable payloads when Content-Encoding is set. Regression
65+
// test for #1055.
66+
func TestFiberV2BodyReaderDecompresses(t *testing.T) {
67+
r := fiberV2.New()
68+
api := NewV2(r, huma.DefaultConfig("Test API", "1.0.0"))
69+
70+
type Body struct {
71+
Name string `json:"name"`
72+
}
73+
huma.Register(api, huma.Operation{
74+
OperationID: "echo",
75+
Method: http.MethodPost,
76+
Path: "/echo",
77+
}, func(ctx context.Context, in *struct{ Body Body }) (*struct{ Body Body }, error) {
78+
return &struct{ Body Body }{Body: in.Body}, nil
79+
})
80+
81+
var buf bytes.Buffer
82+
gz := gzip.NewWriter(&buf)
83+
_, err := gz.Write([]byte(`{"name":"gzipped"}`))
84+
require.NoError(t, err)
85+
require.NoError(t, gz.Close())
86+
87+
req, _ := http.NewRequest(http.MethodPost, "http://example.com/echo", &buf)
88+
req.Header.Set("Content-Type", "application/json")
89+
req.Header.Set("Content-Encoding", "gzip")
90+
resp, err := r.Test(req)
91+
require.NoError(t, err)
92+
93+
body, err := io.ReadAll(resp.Body)
94+
require.NoError(t, err)
95+
require.Equal(t, http.StatusOK, resp.StatusCode, "compressed body should decompress and parse: %s", body)
96+
require.Contains(t, string(body), `"name":"gzipped"`)
97+
}
98+
1299
func BenchmarkHumaFiberV2(b *testing.B) {
13100
type GreetingInput struct {
14101
ID string `path:"id"`

0 commit comments

Comments
 (0)