Skip to content

Commit

Permalink
fix: An issue where if $id starts with # caused a slice bounds out of…
Browse files Browse the repository at this point in the history
… range panic while Unmarshaling

Merge pull request #39 from daveamit/master
  • Loading branch information
b5 authored Mar 1, 2019
2 parents c7761c7 + 9f6179a commit 6e3faa5
Showing 1 changed file with 16 additions and 8 deletions.
24 changes: 16 additions & 8 deletions schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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:
Expand All @@ -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
}
}
}
}
Expand Down Expand Up @@ -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
}
Expand Down

0 comments on commit 6e3faa5

Please sign in to comment.