Skip to content

Commit eb497ee

Browse files
Merge pull request #807 from Serjlee/main
Allow empty path in group operations
2 parents 6bfeeba + ffe4da5 commit eb497ee

File tree

2 files changed

+25
-2
lines changed

2 files changed

+25
-2
lines changed

group_test.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,24 @@ func TestGroupNoPrefix(t *testing.T) {
2626
assert.Equal(t, http.StatusNoContent, resp.Result().StatusCode)
2727
}
2828

29+
func TestGroupEmptyPath(t *testing.T) {
30+
_, api := humatest.New(t)
31+
32+
grp := huma.NewGroup(api, "/users")
33+
34+
huma.Get(grp, "", func(ctx context.Context, input *struct{}) (*struct{}, error) {
35+
return nil, nil
36+
})
37+
38+
assert.Nil(t, api.OpenAPI().Paths["/"])
39+
assert.Nil(t, api.OpenAPI().Paths[""])
40+
assert.NotNil(t, api.OpenAPI().Paths["/users"])
41+
42+
resp := api.Get("/users")
43+
assert.Equal(t, http.StatusNoContent, resp.Result().StatusCode)
44+
45+
}
46+
2947
func TestGroupMultiPrefix(t *testing.T) {
3048
_, api := humatest.New(t)
3149

huma.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -622,8 +622,13 @@ func Register[I, O any](api API, op Operation, handler func(context.Context, *I)
622622
oapi := api.OpenAPI()
623623
registry := oapi.Components.Schemas
624624

625-
if op.Method == "" || op.Path == "" {
626-
panic("method and path must be specified in operation")
625+
if op.Method == "" {
626+
panic("method must be specified in operation")
627+
}
628+
if op.Path == "" {
629+
if grp, ok := api.(*Group); !ok || len(grp.prefixes) == 0 {
630+
panic("path must be specified in operation")
631+
}
627632
}
628633
initResponses(&op)
629634

0 commit comments

Comments
 (0)