Skip to content

Commit e114668

Browse files
baiyuxi930826AgentSmithClawrobtme
authored
fix: do not panic on unresolved schema $ref during Validate (#1065)
* fix: do not panic on unresolved schema $ref during Validate * test: cover discriminator and map[any]any unresolved ref branches * test: move unresolved ref tests into validate_test.go * refactor: hoist nil schema check out of ref-resolution loop --------- Co-authored-by: baiyuxi930826 <[email protected]> Co-authored-by: Robert Thomas <[email protected]>
1 parent d6f2a37 commit e114668

3 files changed

Lines changed: 105 additions & 4 deletions

File tree

‎validate.go‎

Lines changed: 39 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -371,7 +371,12 @@ func validateDiscriminator(r Registry, s *Schema, path *PathBuffer, mode Validat
371371
return
372372
}
373373

374-
Validate(r, r.SchemaFromRef(ref), path, mode, v, res)
374+
resolved := r.SchemaFromRef(ref)
375+
if resolved == nil {
376+
res.Addf(path, v, validation.MsgExpectedResolvableSchemaRef, ref)
377+
return
378+
}
379+
Validate(r, resolved, path, mode, v, res)
375380
}
376381

377382
// toFloat64 normalizes any supported numeric Go type to a float64. The second
@@ -430,8 +435,18 @@ func toFloat64(v any) (float64, bool) {
430435
// }
431436
func Validate(r Registry, s *Schema, path *PathBuffer, mode ValidateMode, v any, res *ValidateResult) {
432437
// Get the actual schema if this is a reference.
438+
if s == nil {
439+
res.Addf(path, v, validation.MsgExpectedResolvableSchemaRef, "")
440+
return
441+
}
442+
433443
for s.Ref != "" {
434-
s = r.SchemaFromRef(s.Ref)
444+
ref := s.Ref
445+
s = r.SchemaFromRef(ref)
446+
if s == nil {
447+
res.Addf(path, v, validation.MsgExpectedResolvableSchemaRef, ref)
448+
return
449+
}
435450
}
436451

437452
if s.OneOf != nil {
@@ -709,7 +724,17 @@ func handleMapString(r Registry, s *Schema, path *PathBuffer, mode ValidateMode,
709724
readOnly := v.ReadOnly
710725
writeOnly := v.WriteOnly
711726
for v.Ref != "" {
712-
v = r.SchemaFromRef(v.Ref)
727+
ref := v.Ref
728+
v = r.SchemaFromRef(ref)
729+
if v == nil {
730+
path.Push(k)
731+
res.Addf(path, m[k], validation.MsgExpectedResolvableSchemaRef, ref)
732+
path.Pop()
733+
break
734+
}
735+
}
736+
if v == nil {
737+
continue
713738
}
714739

715740
// We should be permissive by default to enable easy round-trips for the
@@ -826,7 +851,17 @@ func handleMapAny(r Registry, s *Schema, path *PathBuffer, mode ValidateMode, m
826851
readOnly := v.ReadOnly
827852
writeOnly := v.WriteOnly
828853
for v.Ref != "" {
829-
v = r.SchemaFromRef(v.Ref)
854+
ref := v.Ref
855+
v = r.SchemaFromRef(ref)
856+
if v == nil {
857+
path.Push(k)
858+
res.Addf(path, m[k], validation.MsgExpectedResolvableSchemaRef, ref)
859+
path.Pop()
860+
break
861+
}
862+
}
863+
if v == nil {
864+
continue
830865
}
831866

832867
// We should be permissive by default to enable easy round-trips for the

‎validate_test.go‎

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1854,3 +1854,68 @@ func TestPathBuffer_WithIndex_ReturnsExpectedString(t *testing.T) {
18541854

18551855
assert.Equal(t, "prefix[1]", pb.WithIndex(1))
18561856
}
1857+
1858+
func TestValidateUnresolvedSchemaRefNoPanic(t *testing.T) {
1859+
registry := huma.NewMapRegistry("#/components/schemas/", huma.DefaultSchemaNamer)
1860+
pb := huma.NewPathBuffer([]byte{}, 0)
1861+
res := &huma.ValidateResult{}
1862+
1863+
assertUnresolved := func(t *testing.T, fn func()) {
1864+
t.Helper()
1865+
res.Reset()
1866+
require.NotPanics(t, fn)
1867+
require.NotEmpty(t, res.Errors)
1868+
require.Contains(t, res.Errors[0].Error(), "expected schema $ref to resolve")
1869+
}
1870+
1871+
t.Run("top-level ref", func(t *testing.T) {
1872+
assertUnresolved(t, func() {
1873+
huma.Validate(registry, &huma.Schema{Ref: "#/components/schemas/Missing"}, pb, huma.ModeWriteToServer, map[string]any{"x": 1}, res)
1874+
})
1875+
})
1876+
1877+
t.Run("nil schema", func(t *testing.T) {
1878+
assertUnresolved(t, func() {
1879+
huma.Validate(registry, nil, pb, huma.ModeWriteToServer, map[string]any{"x": 1}, res)
1880+
})
1881+
})
1882+
1883+
t.Run("discriminator mapping ref", func(t *testing.T) {
1884+
schema := &huma.Schema{
1885+
OneOf: []*huma.Schema{{Type: huma.TypeObject}},
1886+
Discriminator: &huma.Discriminator{
1887+
PropertyName: "kind",
1888+
Mapping: map[string]string{"cat": "#/components/schemas/MissingCat"},
1889+
},
1890+
}
1891+
assertUnresolved(t, func() {
1892+
huma.Validate(registry, schema, pb, huma.ModeWriteToServer, map[string]any{"kind": "cat"}, res)
1893+
})
1894+
})
1895+
1896+
t.Run("property ref map[string]any", func(t *testing.T) {
1897+
schema := &huma.Schema{
1898+
Type: huma.TypeObject,
1899+
Properties: map[string]*huma.Schema{
1900+
"nested": {Ref: "#/components/schemas/AlsoMissing"},
1901+
},
1902+
}
1903+
schema.PrecomputeMessages()
1904+
assertUnresolved(t, func() {
1905+
huma.Validate(registry, schema, pb, huma.ModeWriteToServer, map[string]any{"nested": "v"}, res)
1906+
})
1907+
})
1908+
1909+
t.Run("property ref map[any]any", func(t *testing.T) {
1910+
schema := &huma.Schema{
1911+
Type: huma.TypeObject,
1912+
Properties: map[string]*huma.Schema{
1913+
"nested": {Ref: "#/components/schemas/StillMissing"},
1914+
},
1915+
}
1916+
schema.PrecomputeMessages()
1917+
assertUnresolved(t, func() {
1918+
huma.Validate(registry, schema, pb, huma.ModeWriteToServer, map[any]any{"nested": "v"}, res)
1919+
})
1920+
})
1921+
}

‎validation/messages.go‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,4 +48,5 @@ var (
4848
MsgExpectedMaxProperties = "expected object with at most %d properties"
4949
MsgExpectedRequiredProperty = "expected required property %s to be present"
5050
MsgExpectedDependentRequiredProperty = "expected property %s to be present when %s is present"
51+
MsgExpectedResolvableSchemaRef = "expected schema $ref to resolve: %s"
5152
)

0 commit comments

Comments
 (0)