这是indexloc提供的服务,不要输入任何密码
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 42 additions & 9 deletions adapters/adapters_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/danielgtaylor/huma/v2/adapters/humaecho"
"github.com/danielgtaylor/huma/v2/adapters/humafiber"
"github.com/danielgtaylor/huma/v2/adapters/humagin"
"github.com/danielgtaylor/huma/v2/adapters/humago"
"github.com/danielgtaylor/huma/v2/adapters/humahttprouter"
"github.com/danielgtaylor/huma/v2/adapters/humamux"
"github.com/danielgtaylor/huma/v2/humatest"
Expand All @@ -27,6 +28,8 @@ import (
"github.com/uptrace/bunrouter"
)

type key struct{}

// Test the various input types (path, query, header, body).
type TestInput struct {
Group string `path:"group"`
Expand Down Expand Up @@ -90,17 +93,26 @@ func TestAdapters(t *testing.T) {
return huma.DefaultConfig("Test", "1.0.0")
}

wrap := func(h huma.API, isFiber bool) huma.API {
wrap := func(h huma.API, isFiber bool, unwrapper func(ctx huma.Context)) huma.API {
h.UseMiddleware(func(ctx huma.Context, next func(huma.Context)) {
assert.Nil(t, ctx.TLS())
v := ctx.Version()

if !isFiber {
assert.Equal(t, "HTTP/1.1", v.Proto)
assert.Equal(t, 1, v.ProtoMajor)
assert.Equal(t, 1, v.ProtoMinor)
} else {
assert.Equal(t, "http", v.Proto)
}

// Make sure huma.WithValue works correctly
ctx = huma.WithContext(ctx, context.WithValue(ctx.Context(), key{}, "value"))

next(ctx)
}, func(ctx huma.Context, next func(huma.Context)) {
// Make sure the Unwrap func does not panic even when the context is wrapped by WithContext
assert.NotPanics(t, func() { unwrapper(ctx) })
next(ctx)
})
return h
Expand All @@ -110,14 +122,35 @@ func TestAdapters(t *testing.T) {
name string
new func() huma.API
}{
{"chi", func() huma.API { return wrap(humachi.New(chi.NewMux(), config()), false) }},
{"echo", func() huma.API { return wrap(humaecho.New(echo.New(), config()), false) }},
{"fiber", func() huma.API { return wrap(humafiber.New(fiber.New(), config()), true) }},
{"gin", func() huma.API { return wrap(humagin.New(gin.New(), config()), false) }},
{"httprouter", func() huma.API { return wrap(humahttprouter.New(httprouter.New(), config()), false) }},
{"mux", func() huma.API { return wrap(humamux.New(mux.NewRouter(), config()), false) }},
{"bunrouter", func() huma.API { return wrap(humabunrouter.New(bunrouter.New(), config()), false) }},
{"bunroutercompat", func() huma.API { return wrap(humabunrouter.NewCompat(bunrouter.New().Compat(), config()), false) }},
{"chi", func() huma.API {
return wrap(humachi.New(chi.NewMux(), config()), false, func(ctx huma.Context) { humachi.Unwrap(ctx) })
}},
{"echo", func() huma.API {
return wrap(humaecho.New(echo.New(), config()), false, func(ctx huma.Context) { humaecho.Unwrap(ctx) })
}},
{"fiber", func() huma.API {
return wrap(humafiber.New(fiber.New(), config()), true, func(ctx huma.Context) { humafiber.Unwrap(ctx) })
}},
{"go", func() huma.API {
return wrap(humago.New(http.NewServeMux(), config()), false, func(ctx huma.Context) { humago.Unwrap(ctx) })
}},
{"gin", func() huma.API {
return wrap(humagin.New(gin.New(), config()), false, func(ctx huma.Context) { humagin.Unwrap(ctx) })
}},
{"httprouter", func() huma.API {
return wrap(humahttprouter.New(httprouter.New(), config()), false, func(ctx huma.Context) { humahttprouter.Unwrap(ctx) })
}},
{"mux", func() huma.API {
return wrap(humamux.New(mux.NewRouter(), config()), false, func(ctx huma.Context) { humamux.Unwrap(ctx) })
}},
{"bunrouter", func() huma.API {
return wrap(humabunrouter.New(bunrouter.New(), config()), false, func(ctx huma.Context) { humabunrouter.Unwrap(ctx) })
}},
{"bunroutercompat", func() huma.API {
return wrap(humabunrouter.NewCompat(bunrouter.New().Compat(), config()), false, func(ctx huma.Context) {
// FIXME: humabunrouter.Unwrap(ctx) doesn't work with compat mode
})
}},
} {
t.Run(adapter.name, func(t *testing.T) {
testAdapter(t, adapter.new())
Expand Down
7 changes: 7 additions & 0 deletions adapters/humabunrouter/humabunrouter.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,13 @@ var MultipartMaxMemory int64 = 8 * 1024
// Unwrap extracts the underlying HTTP request and response writer from a Huma
// context. If passed a context from a different adapter it will panic.
func Unwrap(ctx huma.Context) (bunrouter.Request, http.ResponseWriter) {
for {
if c, ok := ctx.(interface{ Unwrap() huma.Context }); ok {
ctx = c.Unwrap()
continue
}
break
}
if c, ok := ctx.(*bunContext); ok {
return c.Unwrap()
}
Expand Down
7 changes: 7 additions & 0 deletions adapters/humachi/humachi.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,13 @@ var MultipartMaxMemory int64 = 8 * 1024
// Unwrap extracts the underlying HTTP request and response writer from a Huma
// context. If passed a context from a different adapter it will panic.
func Unwrap(ctx huma.Context) (*http.Request, http.ResponseWriter) {
for {
if c, ok := ctx.(interface{ Unwrap() huma.Context }); ok {
ctx = c.Unwrap()
continue
}
break
}
if c, ok := ctx.(*chiContext); ok {
return c.Unwrap()
}
Expand Down
7 changes: 7 additions & 0 deletions adapters/humaecho/humaecho.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,13 @@ var MultipartMaxMemory int64 = 8 * 1024
// Unwrap extracts the underlying Echo context from a Huma context. If passed a
// context from a different adapter it will panic.
func Unwrap(ctx huma.Context) echo.Context {
for {
if c, ok := ctx.(interface{ Unwrap() huma.Context }); ok {
ctx = c.Unwrap()
continue
}
break
}
if c, ok := ctx.(*echoCtx); ok {
return c.Unwrap()
}
Expand Down
7 changes: 7 additions & 0 deletions adapters/humafiber/humafiber.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,13 @@ import (
// memory-safety: https://docs.gofiber.io/#zero-allocation. Do not keep
// references to the underlying context or its values!
func Unwrap(ctx huma.Context) *fiber.Ctx {
for {
if c, ok := ctx.(interface{ Unwrap() huma.Context }); ok {
ctx = c.Unwrap()
continue
}
break
}
if c, ok := ctx.(*fiberWrapper); ok {
return c.Unwrap()
}
Expand Down
7 changes: 7 additions & 0 deletions adapters/humaflow/humaflow.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,13 @@ var MultipartMaxMemory int64 = 8 * 1024
// Unwrap extracts the underlying HTTP request and response writer from a Huma
// context. If passed a context from a different adapter it will panic.
func Unwrap(ctx huma.Context) (*http.Request, http.ResponseWriter) {
for {
if c, ok := ctx.(interface{ Unwrap() huma.Context }); ok {
ctx = c.Unwrap()
continue
}
break
}
if c, ok := ctx.(*goContext); ok {
return c.Unwrap()
}
Expand Down
7 changes: 7 additions & 0 deletions adapters/humagin/humagin.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,13 @@ var MultipartMaxMemory int64 = 8 * 1024
// Unwrap extracts the underlying Gin context from a Huma context. If passed a
// context from a different adapter it will panic.
func Unwrap(ctx huma.Context) *gin.Context {
for {
if c, ok := ctx.(interface{ Unwrap() huma.Context }); ok {
ctx = c.Unwrap()
continue
}
break
}
if c, ok := ctx.(*ginCtx); ok {
return c.Unwrap()
}
Expand Down
7 changes: 7 additions & 0 deletions adapters/humago/humago.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,13 @@ var MultipartMaxMemory int64 = 8 * 1024
// Unwrap extracts the underlying HTTP request and response writer from a Huma
// context. If passed a context from a different adapter it will panic.
func Unwrap(ctx huma.Context) (*http.Request, http.ResponseWriter) {
for {
if c, ok := ctx.(interface{ Unwrap() huma.Context }); ok {
ctx = c.Unwrap()
continue
}
break
}
if c, ok := ctx.(*goContext); ok {
return c.Unwrap()
}
Expand Down
7 changes: 7 additions & 0 deletions adapters/humahttprouter/humahttprouter.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,13 @@ var MultipartMaxMemory int64 = 8 * 1024
// Unwrap extracts the underlying HTTP request and response writer from a Huma
// context. If passed a context from a different adapter it will panic.
func Unwrap(ctx huma.Context) (*http.Request, http.ResponseWriter, httprouter.Params) {
for {
if c, ok := ctx.(interface{ Unwrap() huma.Context }); ok {
ctx = c.Unwrap()
continue
}
break
}
if c, ok := ctx.(*httprouterContext); ok {
return c.Unwrap()
}
Expand Down
7 changes: 7 additions & 0 deletions adapters/humamux/humamux.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,13 @@ var MultipartMaxMemory int64 = 8 * 1024
// Unwrap extracts the underlying HTTP request and response writer from a Huma
// context. If passed a context from a different adapter it will panic.
func Unwrap(ctx huma.Context) (*http.Request, http.ResponseWriter) {
for {
if c, ok := ctx.(interface{ Unwrap() huma.Context }); ok {
ctx = c.Unwrap()
continue
}
break
}
if c, ok := ctx.(*gmuxContext); ok {
return c.Unwrap()
}
Expand Down
6 changes: 6 additions & 0 deletions api.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,12 @@
return c.override
}

// Unwrap returns the underlying Huma context, which enables you to access
// the original adapter-specific request context.
func (c subContext) Unwrap() Context {
return c.humaContext

Check warning on line 149 in api.go

View check run for this annotation

Codecov / codecov/patch

api.go#L148-L149

Added lines #L148 - L149 were not covered by tests
}

// WithContext returns a new `huma.Context` with the underlying `context.Context`
// replaced with the given one. This is useful for middleware that needs to
// modify the request context.
Expand Down
Loading