From 9f6179a41b2ff860eaf76c141b698224ab782f65 Mon Sep 17 00:00:00 2001 From: Dave Amit Date: Fri, 1 Mar 2019 13:10:31 +0530 Subject: [PATCH] fix: An issue where if $id starts with # caused a slice bounds out of range panic while Unmarshaling --- schema.go | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/schema.go b/schema.go index 84b9d02..4de0351 100644 --- a/schema.go +++ b/schema.go @@ -10,9 +10,11 @@ package jsonschema import ( "encoding/json" "fmt" - "github.com/qri-io/jsonpointer" "net/http" "net/url" + "strings" + + "github.com/qri-io/jsonpointer" ) // Must turns a JSON string into a *RootSchema, panicing if parsing fails. @@ -77,8 +79,8 @@ func (rs *RootSchema) UnmarshalJSON(data []byte) error { } root := &RootSchema{ - Schema: *sch, - SchemaURI: suri.SchemaURI, + Schema: *sch, + SchemaURI: suri.SchemaURI, } // collect IDs for internal referencing: @@ -87,10 +89,16 @@ func (rs *RootSchema) UnmarshalJSON(data []byte) error { if sch, ok := elem.(*Schema); ok { if sch.ID != "" { ids[sch.ID] = sch - - // For the record, I think this is rediculous. + // For the record, I think this is ridiculous. if u, err := url.Parse(sch.ID); err == nil { - ids[u.Path[1:]] = sch + // This is if the identifier is defined as a reference (with #) + // i.e. #/properties/firstName + // in this case, u.Fragment will have /properties/firstName + if strings.HasPrefix(sch.ID, "#") { + ids[u.Fragment[1:]] = sch + } else { + ids[u.Path[1:]] = sch + } } } } @@ -130,8 +138,8 @@ func (rs *RootSchema) UnmarshalJSON(data []byte) error { } *rs = RootSchema{ - Schema: *sch, - SchemaURI: suri.SchemaURI, + Schema: *sch, + SchemaURI: suri.SchemaURI, } return nil }