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
4 changes: 2 additions & 2 deletions group_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ func TestGroupConvenienceEquivalency(t *testing.T) {
// Ensure group overrides still work.
grp4 := huma.NewGroup(api, "/v4")
grp4.UseModifier(func(op *huma.Operation, next func(*huma.Operation)) {
op.OperationID = "custom-id"
op.OperationID = "custom-id-2"
op.Summary = "Custom summary"
next(op)
})
Expand All @@ -123,7 +123,7 @@ func TestGroupConvenienceEquivalency(t *testing.T) {
assert.Equal(t, "get-v1-users", oapi.Paths["/v1/users"].Get.OperationID)
assert.Equal(t, "get-v2-users", oapi.Paths["/v2/users"].Get.OperationID)
assert.Equal(t, "custom-id", oapi.Paths["/v3/users"].Get.OperationID)
assert.Equal(t, "custom-id", oapi.Paths["/v4/users"].Get.OperationID)
assert.Equal(t, "custom-id-2", oapi.Paths["/v4/users"].Get.OperationID)
assert.Equal(t, "get-v5-users", oapi.Paths["/v5/users/"].Get.OperationID)

assert.Equal(t, "Get v1 users", oapi.Paths["/v1/users"].Get.Summary)
Expand Down
16 changes: 15 additions & 1 deletion openapi.go
Original file line number Diff line number Diff line change
Expand Up @@ -1495,9 +1495,23 @@ type OpenAPI struct {

// AddOperation adds an operation to the OpenAPI. This is the preferred way to
// add operations to the OpenAPI, as it will ensure that the operation is
// properly added to the Paths map, and will call any registered OnAddOperation
// properly added to the Paths map and will call any registered OnAddOperation
// functions.
func (o *OpenAPI) AddOperation(op *Operation) {
// Check this won't create a duplicate operation ID.
if op.OperationID != "" {
for _, pathItem := range o.Paths {
for _, existingOp := range []*Operation{
pathItem.Get, pathItem.Post, pathItem.Put, pathItem.Patch,
pathItem.Delete, pathItem.Head, pathItem.Options, pathItem.Trace,
} {
if existingOp != nil && existingOp.OperationID == op.OperationID {
panic("duplicate operation ID: " + op.OperationID)
}
}
}
}

if o.Paths == nil {
o.Paths = map[string]*PathItem{}
}
Expand Down
18 changes: 18 additions & 0 deletions openapi_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package huma_test

import (
"net/http"
"testing"

"github.com/danielgtaylor/huma/v2"
Expand Down Expand Up @@ -265,3 +266,20 @@ func TestDowngrade(t *testing.T) {
// Check that the downgrade worked as expected.
assert.JSONEq(t, expected, string(v30))
}

func TestAddOperationForceUniqueOperationIDs(t *testing.T) {
oapi := &huma.OpenAPI{}
oapi.AddOperation(&huma.Operation{
OperationID: "test",
Method: http.MethodGet,
Path: "/test",
})

assert.PanicsWithValue(t, "duplicate operation ID: test", func() {
oapi.AddOperation(&huma.Operation{
OperationID: "test",
Method: http.MethodPost,
Path: "/test",
})
})
}
Loading