This repository has been archived by the owner on Jul 24, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
reference.go
206 lines (189 loc) · 5.14 KB
/
reference.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
package jsonschema
import (
"encoding/json"
"errors"
"fmt"
"net/http"
"net/url"
"strconv"
"strings"
)
// Using a pointer allows us to handle recursive embedded schemas.
type EmbeddedSchemas map[string]*Schema
func (e *EmbeddedSchemas) UnmarshalJSON(b []byte) error {
*e = make(EmbeddedSchemas)
err1 := e.UnmarshalArray(b)
err2 := e.UnmarshalObject(b)
err3 := e.UnmarshalSingle(b)
if err1 != nil && err2 != nil && err3 != nil {
return errors.New("no valid embedded schemas")
}
return nil
}
func (e *EmbeddedSchemas) UnmarshalArray(b []byte) error {
var schemas []*Schema
if err := json.Unmarshal(b, &schemas); err != nil {
return err
}
for i, v := range schemas {
(*e)[strconv.Itoa(i)] = v
}
return nil
}
func (e *EmbeddedSchemas) UnmarshalObject(b []byte) error {
var m map[string]*Schema
if err := json.Unmarshal(b, &m); err != nil {
return err
}
for k, v := range m {
(*e)[k] = v
}
return nil
}
func (e *EmbeddedSchemas) UnmarshalSingle(b []byte) error {
var s Schema
if err := json.Unmarshal(b, &s); err != nil {
return err
}
(*e)[""] = &s
return nil
}
// ResolveRefs starts a depth-first search through a document for schemas containing
// the 'ref' validator. It completely resolves each one found.
func (s *Schema) ResolveRefs(loadExternal bool) {
s.resolveSelfAndBelow(*s, *s, loadExternal)
}
func (s *Schema) resolveSelfAndBelow(parentSchema, rootSchema Schema, loadExternal bool) {
if parentSchema.id != "" && parentSchema.id != s.id {
s.parentId = parentSchema.id
sURL, sURLErr := url.Parse(s.id)
pURL, pURLErr := url.Parse(parentSchema.id)
if sURLErr == nil && pURLErr == nil && pURL.IsAbs() && !sURL.IsAbs() {
if strings.HasPrefix(s.id, "#") {
pURL.Fragment = strings.TrimPrefix(s.id, "#")
} else {
s.id = pURL.ResolveReference(sURL).String()
}
}
}
cacheKey, err := resolveCacheKey(s.id)
if err == nil {
if _, ok := rootSchema.Cache[cacheKey]; !ok {
rootSchema.Cache[cacheKey] = s
}
}
s.resolveSelf(rootSchema, loadExternal)
s.resolveBelow(rootSchema, loadExternal)
}
func (s *Schema) resolveSelf(rootSchema Schema, loadExternal bool) {
if str, ok := s.hasRef(); ok {
sch, err := s.refToSchema(str, rootSchema, loadExternal)
if err != nil {
return
}
*s = *sch
s.resolveSelf(rootSchema, loadExternal)
}
}
// TODO: test that we fail gracefully if the schema contains infinitely looping "$ref"s.
func (s *Schema) resolveBelow(rootSchema Schema, loadExternal bool) {
if s.resolved == true {
return
}
s.resolved = true
for _, n := range s.nodes {
for _, sch := range n.EmbeddedSchemas {
sch.resolveSelfAndBelow(*s, rootSchema, loadExternal)
}
}
}
func (s *Schema) hasRef() (string, bool) {
for _, n := range s.nodes {
if r, ok := n.Validator.(*ref); ok {
return string(*r), true
}
}
return "", false
}
// TODO: This is hacky. Look into using a library like gojsonpointer[1] instead.
//
// [1] https://github.com/xeipuuv/gojsonpointer
func (s *Schema) refToSchema(str string, rootSchema Schema, loadExternal bool) (*Schema, error) {
parentURL, err := url.Parse(s.parentId)
if err == nil && parentURL.IsAbs() {
sURL, err := url.Parse(str)
if err == nil && !sURL.IsAbs() && !strings.HasPrefix(str, "#") {
str = parentURL.ResolveReference(sURL).String()
}
}
var split []string
url, err := url.Parse(str)
cacheKey, cacheKeyErr := resolveCacheKey(str)
if err == nil && cacheKeyErr == nil {
cachedSchema, ok := rootSchema.Cache[cacheKey]
if ok {
rootSchema = *cachedSchema
} else {
// Handle external URIs.
if !loadExternal {
return new(Schema), errors.New("external schemas are disabled")
}
resp, err := http.Get(str)
if err != nil {
return new(Schema), errors.New("bad external url")
}
defer resp.Body.Close()
s, err := ParseWithCache(resp.Body, loadExternal, &rootSchema.Cache)
if err != nil {
return new(Schema), errors.New("error parsing external doc")
}
rootSchema.Cache[cacheKey] = s
rootSchema = *s
}
str = url.Fragment
}
// Remove the prefix from internal URIs.
str = strings.TrimPrefix(str, "#")
str = strings.TrimPrefix(str, "/")
split = strings.Split(str, "/")
// Make replacements.
for i, v := range split {
r := strings.NewReplacer("~0", "~", "~1", "/", "%25", "%")
split[i] = r.Replace(v)
}
// Resolve the local part of the URI.
return resolveLocalPath(split, rootSchema, str)
}
// TODO: add code and tests for references more than one level deep.
func resolveLocalPath(split []string, rootSchema Schema, str string) (*Schema, error) {
switch len(split) {
case 1:
if split[0] == "" {
return &rootSchema, nil
}
v, ok := rootSchema.nodes[split[0]]
if ok == false {
break
}
if s, ok := v.EmbeddedSchemas[""]; ok {
return s, nil
}
case 2:
v, ok := rootSchema.nodes[split[0]]
if ok == false {
break
}
if s, ok := v.EmbeddedSchemas[split[1]]; ok {
return s, nil
}
}
return new(Schema), fmt.Errorf("failed to resolve %s", str)
}
func resolveCacheKey(id string) (string, error) {
url, err := url.Parse(id)
if err == nil && url.IsAbs() {
url.Fragment = ""
return strings.TrimSuffix(url.String(), "/"), nil
}
return "", fmt.Errorf("failed to resolve cache key for %s", id)
}