8000 [Group] Use modifier in group to add middleware not working · Issue #804 · danielgtaylor/huma · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

[Group] Use modifier in group to add middleware not working #804

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
zhenzou opened this issue Apr 16, 2025 · 0 comments
Open

[Group] Use modifier in group to add middleware not working #804

zhenzou opened this issue Apr 16, 2025 · 0 comments

Comments

@zhenzou
Copy link
zhenzou commented Apr 16, 2025

Use modifier to add middleware in group not working.

Reproduce code

package main

import (
	"context"
	"net/http"
	"net/http/httptest"

	"github.com/danielgtaylor/huma/v2"
	"github.com/danielgtaylor/huma/v2/adapters/humagin"
	"github.com/gin-gonic/gin"
)

type TestOutput struct {
	Body struct {
		ID string `json:"id"`
	}
}

type TestInput struct {
	ID string `path:"id"`
}

func main() {
	engine := gin.New()
	api := humagin.New(engine, huma.DefaultConfig("My API", "1.0.0"))

	v1 := huma.NewGroup(api, "/v1")

        // For some reason, we can not use v1.UseMiddleware
	v1.UseSimpleModifier(func(o *huma.Operation) {
		o.Middlewares = append(o.Middlewares, func(ctx huma.Context, next func(huma.Context)) {
			header := ctx.Header("X-Test")
			if header == "" {
				ctx.SetStatus(http.StatusUnauthorized)
				return
			}
			next(ctx)
		})
	})

	huma.Get(v1, "/test/{id}", func(ctx context.Context, input *TestInput) (*TestOutput, error) {
		return &TestOutput{
			Body: struct {
				ID string `json:"id"`
			}{
				ID: input.ID,
			},
		}, nil
	})

	w := httptest.NewRecorder()
	req, _ := http.NewRequest(http.MethodGet, "/v1/test/123", nil)
	engine.ServeHTTP(w, req)

	println(w.Code)
}

We do not pass the required test header, so Expected: 401 But Actual: 200.

We find the adapter do not respect the Operation's middlewares.

func (a *ginAdapter) Handle(op *huma.Operation, handler func(huma.Context)) {
	// Convert {param} to :param
	path := op.Path
	path = strings.ReplaceAll(path, "{", ":")
	path = strings.ReplaceAll(path, "}", "")
	a.router.Handle(op.Method, path, func(c *gin.Context) {
		ctx := &ginCtx{op: op, orig: c}
		handler(ctx)
	})
}

And we change the adapter to the code below, the middleware worked.

func (a *ginAdapter) Handle(op *huma.Operation, handler func(huma.Context)) {
	// Convert {param} to :param
	path := op.Path
	path = strings.ReplaceAll(path, "{", ":")
	path = strings.ReplaceAll(path, "}", "")
	a.router.Handle(op.Method, path, func(c *gin.Context) {
		ctx := &ginCtx{op: op, orig: c}
		op.Middlewares.Handler(handler)(ctx)
	})
}

However, we're uncertain if this behavior is intentional. Implementing middleware via modifiers may lead to unintended side effects and should be avoided unless explicitly required by the design.

Please help, thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant
0