Skip to content

Commit 86cca28

Browse files
compile/fix: panic compiling non jsonschema resource
1 parent 7ae7ff9 commit 86cca28

File tree

2 files changed

+14
-5
lines changed

2 files changed

+14
-5
lines changed

compiler_test.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,3 +229,15 @@ func TestCustomVocabSubschemaResource(t *testing.T) {
229229
t.Fatal(err)
230230
}
231231
}
232+
233+
func TestNonJSONResource(t *testing.T) {
234+
c := jsonschema.NewCompiler()
235+
err := c.AddResource("schema.json", strings.NewReader("123"))
236+
if err != nil {
237+
t.Fatal(err)
238+
}
239+
_, err = c.Compile("schema.json")
240+
if err == nil {
241+
t.Fatal("compile must fail")
242+
}
243+
}

roots.go

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,8 @@ func (rr *roots) collectResources(r *root, sch any, base url, schPtr jsonPointer
7979
}
8080

8181
func (rr *roots) _collectResources(r *root, sch any, base url, schPtr jsonPointer, fallback dialect) error {
82-
if _, ok := sch.(bool); ok {
82+
obj, ok := sch.(map[string]any)
83+
if !ok {
8384
if schPtr.isEmpty() {
8485
// root resource
8586
res := newResource(schPtr, base)
@@ -88,10 +89,6 @@ func (rr *roots) _collectResources(r *root, sch any, base url, schPtr jsonPointe
8889
}
8990
return nil
9091
}
91-
obj, ok := sch.(map[string]any)
92-
if !ok {
93-
return nil
94-
}
9592

9693
hasSchema := false
9794
if sch, ok := obj["$schema"]; ok {

0 commit comments

Comments
 (0)