Skip to content

Commit 9f6179a

Browse files
author
Dave Amit
committed
fix: An issue where if $id starts with # caused a slice bounds out of range panic while Unmarshaling
1 parent c7761c7 commit 9f6179a

File tree

1 file changed

+16
-8
lines changed

1 file changed

+16
-8
lines changed

schema.go

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,11 @@ package jsonschema
1010
import (
1111
"encoding/json"
1212
"fmt"
13-
"github.com/qri-io/jsonpointer"
1413
"net/http"
1514
"net/url"
15+
"strings"
16+
17+
"github.com/qri-io/jsonpointer"
1618
)
1719

1820
// Must turns a JSON string into a *RootSchema, panicing if parsing fails.
@@ -77,8 +79,8 @@ func (rs *RootSchema) UnmarshalJSON(data []byte) error {
7779
}
7880

7981
root := &RootSchema{
80-
Schema: *sch,
81-
SchemaURI: suri.SchemaURI,
82+
Schema: *sch,
83+
SchemaURI: suri.SchemaURI,
8284
}
8385

8486
// collect IDs for internal referencing:
@@ -87,10 +89,16 @@ func (rs *RootSchema) UnmarshalJSON(data []byte) error {
8789
if sch, ok := elem.(*Schema); ok {
8890
if sch.ID != "" {
8991
ids[sch.ID] = sch
90-
91-
// For the record, I think this is rediculous.
92+
// For the record, I think this is ridiculous.
9293
if u, err := url.Parse(sch.ID); err == nil {
93-
ids[u.Path[1:]] = sch
94+
// This is if the identifier is defined as a reference (with #)
95+
// i.e. #/properties/firstName
96+
// in this case, u.Fragment will have /properties/firstName
97+
if strings.HasPrefix(sch.ID, "#") {
98+
ids[u.Fragment[1:]] = sch
99+
} else {
100+
ids[u.Path[1:]] = sch
101+
}
94102
}
95103
}
96104
}
@@ -130,8 +138,8 @@ func (rs *RootSchema) UnmarshalJSON(data []byte) error {
130138
}
131139

132140
*rs = RootSchema{
133-
Schema: *sch,
134-
SchemaURI: suri.SchemaURI,
141+
Schema: *sch,
142+
SchemaURI: suri.SchemaURI,
135143
}
136144
return nil
137145
}

0 commit comments

Comments
 (0)