diff --git a/group_test.go b/group_test.go index 9057f143..a6bc594c 100644 --- a/group_test.go +++ b/group_test.go @@ -26,6 +26,24 @@ func TestGroupNoPrefix(t *testing.T) { assert.Equal(t, http.StatusNoContent, resp.Result().StatusCode) } +func TestGroupEmptyPath(t *testing.T) { + _, api := humatest.New(t) + + grp := huma.NewGroup(api, "/users") + + huma.Get(grp, "", func(ctx context.Context, input *struct{}) (*struct{}, error) { + return nil, nil + }) + + assert.Nil(t, api.OpenAPI().Paths["/"]) + assert.Nil(t, api.OpenAPI().Paths[""]) + assert.NotNil(t, api.OpenAPI().Paths["/users"]) + + resp := api.Get("/users") + assert.Equal(t, http.StatusNoContent, resp.Result().StatusCode) + +} + func TestGroupMultiPrefix(t *testing.T) { _, api := humatest.New(t) diff --git a/huma.go b/huma.go index 54e932c0..709e1a73 100644 --- a/huma.go +++ b/huma.go @@ -622,8 +622,13 @@ func Register[I, O any](api API, op Operation, handler func(context.Context, *I) oapi := api.OpenAPI() registry := oapi.Components.Schemas - if op.Method == "" || op.Path == "" { - panic("method and path must be specified in operation") + if op.Method == "" { + panic("method must be specified in operation") + } + if op.Path == "" { + if grp, ok := api.(*Group); !ok || len(grp.prefixes) == 0 { + panic("path must be specified in operation") + } } initResponses(&op)