@@ -12,15 +12,15 @@ import (
1212 "time"
1313
1414 "github.com/danielgtaylor/huma/v2"
15- "github.com/gofiber/fiber/v2 "
15+ "github.com/gofiber/fiber/v3 "
1616)
1717
1818// Unwrap extracts the underlying Fiber context from a Huma context. If passed a
1919// context from a different adapter it will panic. Keep in mind the limitations
2020// of the underlying Fiber/fasthttp libraries and how that impacts
2121// memory-safety: https://docs.gofiber.io/#zero-allocation. Do not keep
2222// references to the underlying context or its values!
23- func Unwrap (ctx huma.Context ) * fiber.Ctx {
23+ func Unwrap (ctx huma.Context ) fiber.Ctx {
2424 for {
2525 if c , ok := ctx .(interface { Unwrap () huma.Context }); ok {
2626 ctx = c .Unwrap ()
@@ -42,14 +42,14 @@ type fiberAdapter struct {
4242type fiberWrapper struct {
4343 op * huma.Operation
4444 status int
45- orig * fiber.Ctx
45+ orig fiber.Ctx
4646 ctx context.Context
4747}
4848
49- // check that fiberCtx implements huma.Context
49+ // check that fiberWrapper implements huma.Context
5050var _ huma.Context = & fiberWrapper {}
5151
52- func (c * fiberWrapper ) Unwrap () * fiber.Ctx {
52+ func (c * fiberWrapper ) Unwrap () fiber.Ctx {
5353 return c .orig
5454}
5555
@@ -74,7 +74,7 @@ func (c *fiberWrapper) Host() string {
7474}
7575
7676func (c * fiberWrapper ) RemoteAddr () string {
77- return c .orig .Context ().RemoteAddr ().String ()
77+ return c .orig .RequestCtx ().RemoteAddr ().String ()
7878}
7979
8080func (c * fiberWrapper ) URL () url.URL {
@@ -120,7 +120,7 @@ func (c *fiberWrapper) SetReadDeadline(deadline time.Time) error {
120120 // 2. Set the Fiber app's `BodyLimit` to some small value like `1`
121121 // Fiber will only call the request handler for streaming once the limit is
122122 // reached. This is annoying but currently how things work.
123- return c .orig .Context ().Conn ().SetReadDeadline (deadline )
123+ return c .orig .RequestCtx ().Conn ().SetReadDeadline (deadline )
124124}
125125
126126func (c * fiberWrapper ) SetStatus (code int ) {
@@ -132,6 +132,7 @@ func (c *fiberWrapper) SetStatus(code int) {
132132func (c * fiberWrapper ) Status () int {
133133 return c .status
134134}
135+
135136func (c * fiberWrapper ) AppendHeader (name string , value string ) {
136137 c .orig .Append (name , value )
137138}
@@ -141,11 +142,11 @@ func (c *fiberWrapper) SetHeader(name string, value string) {
141142}
142143
143144func (c * fiberWrapper ) BodyWriter () io.Writer {
144- return c .orig .Context ()
145+ return c .orig .RequestCtx ()
145146}
146147
147148func (c * fiberWrapper ) TLS () * tls.ConnectionState {
148- return c .orig .Context ().TLSConnectionState ()
149+ return c .orig .RequestCtx ().TLSConnectionState ()
149150}
150151
151152func (c * fiberWrapper ) Version () huma.ProtoVersion {
@@ -155,11 +156,11 @@ func (c *fiberWrapper) Version() huma.ProtoVersion {
155156}
156157
157158type router interface {
158- Add (method , path string , handlers ... fiber. Handler ) fiber.Router
159+ Add (methods [] string , path string , handler any , handlers ... any ) fiber.Router
159160}
160161
161162type requestTester interface {
162- Test (* http.Request , ... int ) (* http.Response , error )
163+ Test (* http.Request , ... fiber. TestConfig ) (* http.Response , error )
163164}
164165
165166type contextWrapperValue struct {
@@ -194,9 +195,9 @@ func (a *fiberAdapter) Handle(op *huma.Operation, handler func(huma.Context)) {
194195 path := op .Path
195196 path = strings .ReplaceAll (path , "{" , ":" )
196197 path = strings .ReplaceAll (path , "}" , "" )
197- a .router .Add (op .Method , path , func (c * fiber.Ctx ) error {
198+ a .router .Add ([] string { op .Method } , path , func (c fiber.Ctx ) error {
198199 var values []* contextWrapperValue
199- c .Context ().VisitUserValuesAll (func (key , value any ) {
200+ c .RequestCtx ().VisitUserValuesAll (func (key , value any ) {
200201 values = append (values , & contextWrapperValue {
201202 Key : key ,
202203 Value : value ,
@@ -207,16 +208,14 @@ func (a *fiberAdapter) Handle(op *huma.Operation, handler func(huma.Context)) {
207208 orig : c ,
208209 ctx : & contextWrapper {
209210 values : values ,
210- Context : c .UserContext (),
211+ Context : c .Context (),
211212 },
212213 })
213214 return nil
214215 })
215216}
216217
217218func (a * fiberAdapter ) ServeHTTP (w http.ResponseWriter , r * http.Request ) {
218- // b, _ := httputil.DumpRequest(r, true)
219- // fmt.Println(string(b))
220219 resp , err := a .tester .Test (r )
221220 if resp != nil && resp .Body != nil {
222221 defer func () {
@@ -236,10 +235,12 @@ func (a *fiberAdapter) ServeHTTP(w http.ResponseWriter, r *http.Request) {
236235 _ , _ = io .Copy (w , resp .Body )
237236}
238237
238+ // New creates a new Huma API using the Fiber adapter.
239239func New (r * fiber.App , config huma.Config ) huma.API {
240240 return huma .NewAPI (config , & fiberAdapter {tester : r , router : r })
241241}
242242
243+ // NewWithGroup creates a new Huma API using the Fiber adapter with a route group.
243244func NewWithGroup (r * fiber.App , g fiber.Router , config huma.Config ) huma.API {
244245 return huma .NewAPI (config , & fiberAdapter {tester : r , router : g })
245246}
0 commit comments