这是indexloc提供的服务,不要输入任何密码
Skip to content
Open
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
22 changes: 12 additions & 10 deletions adapters/adapters_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,17 +91,19 @@ func TestAdapters(t *testing.T) {
}

wrap := func(h huma.API, isFiber bool) 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)
h.UseMiddleware(func(next func(huma.Context)) func(ctx huma.Context) {
return func(ctx 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)
}
next(ctx)
}
next(ctx)
})
return h
}
Expand Down
12 changes: 7 additions & 5 deletions adapters/humafiber/humafiber_context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -213,12 +213,14 @@ func FiberMiddlewareUserContext(c *fiber.Ctx) error {
return c.Next()
}

func HumaMiddleware(ctx huma.Context, next func(huma.Context)) {
value := ctx.Header(HeaderNameHuma)
if value != "" {
ctx = huma.WithValue(ctx, contextValueHuma, value)
func HumaMiddleware(next func(huma.Context)) func(ctx huma.Context) {
return func(ctx huma.Context) {
value := ctx.Header(HeaderNameHuma)
if value != "" {
ctx = huma.WithValue(ctx, contextValueHuma, value)
}
next(ctx)
}
next(ctx)
}

func TestHumaFiber(t *testing.T) {
Expand Down
4 changes: 2 additions & 2 deletions api.go
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ type API interface {
// route to a specific handler, which provides opportunity to respond early,
// change the course of the request execution, or set request-scoped values for
// the next Middleware.
UseMiddleware(middlewares ...func(ctx Context, next func(Context)))
UseMiddleware(middlewares ...Middleware)

// Middlewares returns a slice of middleware handler functions that will be
// run for all operations. Middleware are run in the order they are added.
Expand Down Expand Up @@ -328,7 +328,7 @@ func (a *api) Marshal(w io.Writer, ct string, v any) error {
return f.Marshal(w, v)
}

func (a *api) UseMiddleware(middlewares ...func(ctx Context, next func(Context))) {
func (a *api) UseMiddleware(middlewares ...Middleware) {
a.middlewares = append(a.middlewares, middlewares...)
}

Expand Down
12 changes: 7 additions & 5 deletions api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,13 @@ func ExampleAdapter_handle() {
func TestContextValue(t *testing.T) {
_, api := humatest.New(t)

api.UseMiddleware(func(ctx huma.Context, next func(huma.Context)) {
// Make an updated context available to the handler.
ctx = huma.WithValue(ctx, "foo", "bar")
next(ctx)
assert.Equal(t, http.StatusNoContent, ctx.Status())
api.UseMiddleware(func(next func(huma.Context)) func(huma.Context) {
return func(ctx huma.Context) {
// Make an updated context available to the handler.
ctx = huma.WithValue(ctx, "foo", "bar")
next(ctx)
assert.Equal(t, http.StatusNoContent, ctx.Status())
}
})

// Register a simple hello world operation in the API.
Expand Down
4 changes: 2 additions & 2 deletions autopatch/autopatch.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
import (
"bytes"
"encoding/json"
"fmt"
"errors"
"io"
"net/http"
"net/http/httptest"
Expand Down Expand Up @@ -329,7 +329,7 @@
var nullabilitySettings MergePatchNullabilitySettings
if extension, ok := oapi.Extensions[MergePatchNullabilityExtension]; ok {
if nullabilitySettings, ok = extension.(MergePatchNullabilitySettings); !ok {
huma.WriteErr(api, ctx, http.StatusInternalServerError, "Unable to parse nullability settings", fmt.Errorf("invalid nullability settings type"))
huma.WriteErr(api, ctx, http.StatusInternalServerError, "Unable to parse nullability settings", errors.New("invalid nullability settings type"))

Check warning on line 332 in autopatch/autopatch.go

View check run for this annotation

Codecov / codecov/patch

autopatch/autopatch.go#L332

Added line #L332 was not covered by tests
return
} else if nullabilitySettings.Enabled {
preserveNullsInMergePatch = true
Expand Down
5 changes: 3 additions & 2 deletions autopatch/autopatch_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"testing/iotest"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/danielgtaylor/huma/v2"
"github.com/danielgtaylor/huma/v2/humatest"
Expand Down Expand Up @@ -520,7 +521,7 @@ func TestReplaceNulls(t *testing.T) {
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
result, err := replaceNulls([]byte(tc.input), settings)
assert.NoError(t, err)
require.NoError(t, err)
assert.JSONEq(t, tc.expected, string(result))
})
}
Expand Down Expand Up @@ -566,7 +567,7 @@ func TestRestoreNulls(t *testing.T) {
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
result, err := restoreNulls([]byte(tc.input), settings)
assert.NoError(t, err)
require.NoError(t, err)
assert.JSONEq(t, tc.expected, string(result))
})
}
Expand Down
15 changes: 5 additions & 10 deletions chain.go
Original file line number Diff line number Diff line change
@@ -1,22 +1,17 @@
package huma

type Middleware func(next func(Context)) func(Context)

// Middlewares is a list of middleware functions that can be attached to an
// API and will be called for all incoming requests.
type Middlewares []func(ctx Context, next func(Context))
type Middlewares []Middleware

// Handler builds and returns a handler func from the chain of middlewares,
// with `endpoint func` as the final handler.
func (m Middlewares) Handler(endpoint func(Context)) func(Context) {
return m.chain(endpoint)
}

// wrap user middleware func with the next func to one func
func wrap(fn func(Context, func(Context)), next func(Context)) func(Context) {
return func(ctx Context) {
fn(ctx, next)
}
}

// chain builds a Middleware composed of an inline middleware stack and endpoint
// handler in the order they are passed.
func (m Middlewares) chain(endpoint func(Context)) func(Context) {
Expand All @@ -26,9 +21,9 @@ func (m Middlewares) chain(endpoint func(Context)) func(Context) {
}

// Wrap the end handler with the middleware chain
w := wrap(m[len(m)-1], endpoint)
w := m[len(m)-1](endpoint)
for i := len(m) - 2; i >= 0; i-- {
w = wrap(m[i], w)
w = m[i](w)
}
return w
}
2 changes: 1 addition & 1 deletion group.go
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ func (g *Group) ModifyOperation(op *Operation, next func(*Operation)) {
// UseMiddleware adds one or more middleware functions to the group that will be
// run on all operations in the group. Use this to add common functionality to
// all operations in the group, e.g. authentication/authorization.
func (g *Group) UseMiddleware(middlewares ...func(ctx Context, next func(Context))) {
func (g *Group) UseMiddleware(middlewares ...Middleware) {
g.middlewares = append(g.middlewares, middlewares...)
}

Expand Down
18 changes: 11 additions & 7 deletions group_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,13 +147,17 @@ func TestGroupCustomizations(t *testing.T) {
opModifier1Called = true
})

grp.UseMiddleware(func(ctx huma.Context, next func(huma.Context)) {
middleware1Called = true
next(ctx)
})
grp.UseMiddleware(func(ctx huma.Context, next func(huma.Context)) {
middleware2Called = true
next(ctx)
grp.UseMiddleware(func(next func(huma.Context)) func(huma.Context) {
return func(ctx huma.Context) {
middleware1Called = true
next(ctx)
}
})
grp.UseMiddleware(func(next func(huma.Context)) func(huma.Context) {
return func(ctx huma.Context) {
middleware2Called = true
next(ctx)
}
})

grp.UseTransformer(func(ctx huma.Context, status string, v any) (any, error) {
Expand Down
Loading