Skip to content

Commit

Permalink
compile/fix: panic compiling non jsonschema resource
Browse files Browse the repository at this point in the history
  • Loading branch information
santhosh-tekuri committed Sep 17, 2024
1 parent 7ae7ff9 commit 86cca28
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 5 deletions.
12 changes: 12 additions & 0 deletions compiler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -229,3 +229,15 @@ func TestCustomVocabSubschemaResource(t *testing.T) {
t.Fatal(err)
}
}

func TestNonJSONResource(t *testing.T) {
c := jsonschema.NewCompiler()
err := c.AddResource("schema.json", strings.NewReader("123"))
if err != nil {
t.Fatal(err)
}
_, err = c.Compile("schema.json")
if err == nil {
t.Fatal("compile must fail")
}
}
7 changes: 2 additions & 5 deletions roots.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,8 @@ func (rr *roots) collectResources(r *root, sch any, base url, schPtr jsonPointer
}

func (rr *roots) _collectResources(r *root, sch any, base url, schPtr jsonPointer, fallback dialect) error {
if _, ok := sch.(bool); ok {
obj, ok := sch.(map[string]any)
if !ok {
if schPtr.isEmpty() {
// root resource
res := newResource(schPtr, base)
Expand All @@ -88,10 +89,6 @@ func (rr *roots) _collectResources(r *root, sch any, base url, schPtr jsonPointe
}
return nil
}
obj, ok := sch.(map[string]any)
if !ok {
return nil
}

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

0 comments on commit 86cca28

Please sign in to comment.