-
Notifications
You must be signed in to change notification settings - Fork 8
/
ref.go
191 lines (163 loc) · 4.8 KB
/
ref.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
package jsonschema
import (
"net/url"
"strconv"
"strings"
)
// resolveRef resolves a reference to another schema, either locally or globally, supporting both $ref and $dynamicRef.
func (s *Schema) resolveRef(ref string) (*Schema, error) {
if ref == "#" {
return s.getRootSchema(), nil
}
if strings.HasPrefix(ref, "#") {
return s.resolveAnchor(ref[1:])
}
// Resolve the full URL if ref is a relative URL
if !isAbsoluteURI(ref) && s.baseURI != "" {
ref = resolveRelativeURI(s.baseURI, ref)
}
// Handle full URL references
return s.resolveRefWithFullURL(ref)
}
func (s *Schema) resolveAnchor(anchorName string) (*Schema, error) {
var schema *Schema
var err error
if strings.HasPrefix(anchorName, "/") {
schema, err = s.resolveJSONPointer(anchorName)
} else {
if schema, ok := s.anchors[anchorName]; ok {
return schema, nil
}
if schema, ok := s.dynamicAnchors[anchorName]; ok {
return schema, nil
}
}
if schema == nil && s.parent != nil {
return s.parent.resolveAnchor(anchorName)
}
return schema, err
}
// resolveRefWithFullURL resolves a full URL reference to another schema.
func (s *Schema) resolveRefWithFullURL(ref string) (*Schema, error) {
root := s.getRootSchema()
if resolved, err := root.getSchema(ref); err == nil {
return resolved, nil
}
// If not found in the current schema or its parents, look for the reference in the compiler
if resolved, err := s.compiler.GetSchema(ref); err != nil {
return nil, ErrFailedToResolveGlobalReference
} else {
return resolved, nil
}
}
// resolveJSONPointer resolves a JSON Pointer within the schema based on JSON Schema structure.
func (s *Schema) resolveJSONPointer(pointer string) (*Schema, error) {
if pointer == "/" {
return s, nil
}
segments := strings.Split(strings.TrimPrefix(pointer, "/"), "/")
currentSchema := s
previousSegment := ""
for i, segment := range segments {
decodedSegment, err := url.PathUnescape(strings.ReplaceAll(strings.ReplaceAll(segment, "~1", "/"), "~0", "~"))
if err != nil {
return nil, ErrFailedToDecodeSegmentWithJSONPointer
}
nextSchema, found := findSchemaInSegment(currentSchema, decodedSegment, previousSegment)
if found {
currentSchema = nextSchema
previousSegment = decodedSegment // Update the context for the next iteration
continue
}
if !found && i == len(segments)-1 {
// If no schema is found and it's the last segment, throw error
return nil, ErrSegmentNotFoundForJSONPointer
}
previousSegment = decodedSegment // Update the context for the next iteration
}
return currentSchema, nil
}
// Helper function to find a schema within a given segment
func findSchemaInSegment(currentSchema *Schema, segment string, previousSegment string) (*Schema, bool) {
switch previousSegment {
case "properties":
if currentSchema.Properties != nil {
if schema, exists := (*currentSchema.Properties)[segment]; exists {
return schema, true
}
}
case "prefixItems":
index, err := strconv.Atoi(segment)
if err == nil && currentSchema.PrefixItems != nil && index < len(currentSchema.PrefixItems) {
return currentSchema.PrefixItems[index], true
}
case "$defs":
if defSchema, exists := currentSchema.Defs[segment]; exists {
return defSchema, true
}
case "items":
if currentSchema.Items != nil {
return currentSchema.Items, true
}
}
return nil, false
}
func (s *Schema) resolveReferences() {
// Resolve the root reference if this schema itself is a reference
if s.Ref != "" {
resolved, _ := s.resolveRef(s.Ref) // Resolve against root schema
s.ResolvedRef = resolved
}
if s.DynamicRef != "" {
resolved, _ := s.resolveRef(s.DynamicRef) // Resolve dynamic references against root schema
s.ResolvedDynamicRef = resolved
}
// Recursively resolve references within definitions
if s.Defs != nil {
for _, defSchema := range s.Defs {
defSchema.resolveReferences()
}
}
// Recursively resolve references in properties
if s.Properties != nil {
for _, schema := range *s.Properties {
if schema != nil {
schema.resolveReferences()
}
}
}
// Additional fields that can have subschemas
resolveSubschemaList(s.AllOf)
resolveSubschemaList(s.AnyOf)
resolveSubschemaList(s.OneOf)
if s.Not != nil {
s.Not.resolveReferences()
}
if s.Items != nil {
s.Items.resolveReferences()
}
if s.PrefixItems != nil {
for _, schema := range s.PrefixItems {
schema.resolveReferences()
}
}
if s.AdditionalProperties != nil {
s.AdditionalProperties.resolveReferences()
}
if s.Contains != nil {
s.Contains.resolveReferences()
}
if s.PatternProperties != nil {
for _, schema := range *s.PatternProperties {
schema.resolveReferences()
}
}
}
// Helper function to resolve references in a list of schemas
func resolveSubschemaList(schemas []*Schema) {
for _, schema := range schemas {
if schema != nil {
schema.resolveReferences()
}
}
}