Skip to content

Commit 214a18c

Browse files
Mcklmomcklmorobtme
authored
fix: avoid faulty duplicate detection (#893)
* fix: avoid faulty duplicate detection bug caused by missing reflection type name for inline struct definitions, combined with an empty OperationID * fix: resolve inline schema name collisions in the registry Move duplicate-name handling into mapRegistry.Schema, where type identity is already tracked via the seen set. Identical inline types reuse the existing ref (dedup preserved), distinct anonymous types get a numeric suffix, and named-type collisions still panic. This drops the NameExistsInSchema addition to the public Registry interface. --------- Co-authored-by: mcklmo <[email protected]> Co-authored-by: Robert Thomas <[email protected]>
1 parent 4e53fef commit 214a18c

3 files changed

Lines changed: 127 additions & 6 deletions

File tree

‎adapters/humamux/humamux_test.go‎

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,3 +133,63 @@ func BenchmarkHumaGorillaMux(b *testing.B) {
133133
}
134134
}
135135
}
136+
137+
func TestOperationWithoutIDAndInlineRequestTypeDefinitionNotPanics(t *testing.T) {
138+
r := mux.NewRouter()
139+
api := New(r, huma.DefaultConfig("Test", "1.0.0"))
140+
141+
var (
142+
op1 = huma.Operation{Method: http.MethodGet, Path: "/1"} // no OperationID
143+
op2 = huma.Operation{Method: http.MethodGet, Path: "/2"} // no OperationID
144+
)
145+
146+
huma.Register(api, op1, func(ctx context.Context, i *struct { // inline request input type definition
147+
Body struct {
148+
Test1 string // field name varying
149+
}
150+
},
151+
) (*struct{}, error,
152+
) {
153+
return nil, nil
154+
})
155+
156+
huma.Register(api, op2, func(ctx context.Context, i *struct { // inline request input type definition
157+
Body struct {
158+
Test2 string // field name varying
159+
}
160+
},
161+
) (*struct{}, error,
162+
) {
163+
return nil, nil
164+
})
165+
}
166+
167+
func TestOperationWithoutIDAndInlineResponseTypeDefinitionNotPanics(t *testing.T) {
168+
r := mux.NewRouter()
169+
api := New(r, huma.DefaultConfig("", ""))
170+
171+
var (
172+
op1 = huma.Operation{Method: http.MethodGet, Path: "/1"} // no OperationID
173+
op2 = huma.Operation{Method: http.MethodGet, Path: "/2"} // no OperationID
174+
)
175+
176+
huma.Register(api, op1, func(ctx context.Context, i *struct{},
177+
) (*struct { // inline response output type definition
178+
Body struct {
179+
Test1 string // field name varying
180+
}
181+
}, error,
182+
) {
183+
return nil, nil
184+
})
185+
186+
huma.Register(api, op2, func(ctx context.Context, i *struct{},
187+
) (*struct { // inline response output type definition
188+
Body struct {
189+
Test2 string // field name varying
190+
}
191+
}, error,
192+
) {
193+
return nil, nil
194+
})
195+
}

‎registry.go‎

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"encoding/json"
55
"fmt"
66
"reflect"
7+
"strconv"
78
"strings"
89
"unicode/utf8"
910
)
@@ -124,17 +125,30 @@ func (r *mapRegistry) Schema(t reflect.Type, allowRef bool, hint string) *Schema
124125

125126
if getsRef {
126127
if s, ok := r.schemas[name]; ok {
127-
if _, ok = r.seen[t]; !ok {
128-
// The name matches but the type is different, so we have a dupe.
128+
if _, seen := r.seen[t]; seen {
129+
// The name and type both match, so reuse the existing schema.
130+
if allowRef {
131+
return &Schema{Ref: r.prefix + name}
132+
}
129133

130-
panic(fmt.Errorf("duplicate name: %s, new type: %s, existing type: %s", name, t, r.types[name]))
134+
return s
131135
}
132136

133-
if allowRef {
134-
return &Schema{Ref: r.prefix + name}
137+
// The name matches but the type is different. Named types are a
138+
// genuine duplicate the caller must resolve (e.g. with a custom
139+
// namer), but unnamed inline types share a hint-derived name by
140+
// design, so disambiguate them with an incrementing numeric suffix.
141+
if t.Name() != "" {
142+
panic(fmt.Errorf("duplicate name: %s, new type: %s, existing type: %s", name, t, r.types[name]))
135143
}
136144

137-
return s
145+
base := name
146+
for i := 1; ; i++ {
147+
name = base + strconv.Itoa(i)
148+
if _, exists := r.schemas[name]; !exists {
149+
break
150+
}
151+
}
138152
}
139153
}
140154

‎registry_test.go‎

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,53 @@ func TestAllowAdditionalPropertiesByDefault(t *testing.T) {
9898
})
9999
}
100100

101+
func TestInlineTypeDisambiguation(t *testing.T) {
102+
// Distinct inline (unnamed) structs share the same hint-derived name and
103+
// must be disambiguated rather than panic. See issue reproduced in #893.
104+
r := NewMapRegistry("#/components/schemas/", DefaultSchemaNamer)
105+
106+
first := r.Schema(reflect.TypeOf(struct {
107+
Field1 string `json:"field1"`
108+
}{}), true, "Request")
109+
second := r.Schema(reflect.TypeOf(struct {
110+
Field2 string `json:"field2"`
111+
}{}), true, "Request")
112+
113+
assert.Equal(t, "#/components/schemas/Request", first.Ref)
114+
assert.Equal(t, "#/components/schemas/Request1", second.Ref)
115+
assert.Len(t, r.Map(), 2)
116+
}
117+
118+
func TestInlineTypeDeduplication(t *testing.T) {
119+
// Identical inline structs are the same reflect.Type and must still
120+
// deduplicate to a single schema.
121+
r := NewMapRegistry("#/components/schemas/", DefaultSchemaNamer)
122+
123+
first := r.Schema(reflect.TypeOf(struct {
124+
Field string `json:"field"`
125+
}{}), true, "Request")
126+
second := r.Schema(reflect.TypeOf(struct {
127+
Field string `json:"field"`
128+
}{}), true, "Request")
129+
130+
assert.Equal(t, "#/components/schemas/Request", first.Ref)
131+
assert.Equal(t, first.Ref, second.Ref)
132+
assert.Len(t, r.Map(), 1)
133+
}
134+
135+
func TestDuplicateNamedTypePanics(t *testing.T) {
136+
// Genuinely different named types that collide on a name remain a hard
137+
// error, directing the caller to a custom namer.
138+
r := NewMapRegistry("#/components/schemas/", func(reflect.Type, string) string {
139+
return "Collision"
140+
})
141+
142+
r.Schema(reflect.TypeFor[S](), true, "")
143+
assert.Panics(t, func() {
144+
r.Schema(reflect.TypeFor[Output[int]](), true, "")
145+
})
146+
}
147+
101148
func TestRegistryConfigValue(t *testing.T) {
102149
r := NewMapRegistry("/schemas", DefaultSchemaNamer)
103150

0 commit comments

Comments
 (0)