From 86cca28795c9e34c43371b70608d243667bee2a9 Mon Sep 17 00:00:00 2001 From: Santhosh Kumar Tekuri Date: Wed, 18 Sep 2024 00:38:34 +0530 Subject: [PATCH] compile/fix: panic compiling non jsonschema resource --- compiler_test.go | 12 ++++++++++++ roots.go | 7 ++----- 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/compiler_test.go b/compiler_test.go index 67e60c5..e236e9e 100644 --- a/compiler_test.go +++ b/compiler_test.go @@ -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") + } +} diff --git a/roots.go b/roots.go index b9b79ba..a8d0ef0 100644 --- a/roots.go +++ b/roots.go @@ -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) @@ -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 {