-
Notifications
You must be signed in to change notification settings - Fork 0
/
validate_examples.go
268 lines (220 loc) · 8.03 KB
/
validate_examples.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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
package validate_examples
import (
"encoding/json"
"fmt"
"os"
"sort"
"strconv"
"github.com/PaesslerAG/jsonpath"
"github.com/bmatcuk/doublestar"
"github.com/xeipuuv/gojsonschema"
"io/ioutil"
"path/filepath"
"strings"
)
type Example struct {
schemaPath string
examplePath string
}
const arrayError = "Invalid type. Expected: object, given: array"
// Find all examples and their respective schemas.
// Unfortunately, `jsonpath` seems not to implement JsonPath filters on recursive traversal queries.
// This introduces an layer of complexity related to the dynamic traversal of the JSON nodes.
// The function can be simplified if you'll find a lib that implements queries like:
// $..[?(@.examples)] <- find all nodes that have the examples property
// Or if you find any library that has a decent implementation of JSON Walker
// Related: https://github.com/PaesslerAG/jsonpath/issues/24
func FindExamples(jsonObject map[string]interface{}, cb func(Example, error)) {
jsonNodes, _ := jsonpath.Get("$..*", jsonObject)
nodes := sortNodes(jsonNodes.([]interface{}))
for _, node := range nodes {
schema, _ := jsonpath.Get(`$["schema"]["$ref"]`, node)
example, _ := jsonpath.Get(`$["example"]["$ref"]`, node)
schemaStr, schemaOk := schema.(string)
exampleStr, exampleOk := example.(string)
if !schemaOk || !exampleOk {
if exampleOk {
cb(Example{}, fmt.Errorf("can't find schema of the example"))
}
continue
}
cb(Example{schemaStr, exampleStr}, nil)
}
}
// Deterministic sort json nodes.
// Return only nodes that are map[string]interface{}.
func sortNodes(jsonNodes []interface{}) []map[string]interface{} {
nodes := map[string]interface{}{}
keys := []string(nil)
for _, node := range jsonNodes {
key := fmt.Sprint(node)
keys = append(keys, key)
nodes[key] = node
}
sort.Strings(keys)
sortedNodes := []map[string]interface{}(nil)
for _, key := range keys {
if node, ok := nodes[key].(map[string]interface{}); ok {
sortedNodes = append(sortedNodes, node)
}
}
return sortedNodes
}
// Read JSON object from a file and Unmarshal it as a generic map.
func GetObjectFromFile(filePath string) (map[string]interface{}, error) {
fileObject := map[string]interface{}{}
jsonBytes, err := ioutil.ReadFile(filePath)
if err != nil {
return nil, fmt.Errorf("can't read the file:%s:%s", filePath, err)
}
err = json.Unmarshal(jsonBytes, &fileObject)
if err != nil {
return nil, fmt.Errorf("can't unmarshal contents: %s: %+v", filePath, err)
}
return fileObject, nil
}
// Generator to retrieve contents of next json files and pass them to a scan function.
func ScanJSONFiles(mainPath string, scanFunction func(string)) {
jsonFiles, _ := doublestar.Glob(mainPath + "/**/*.json")
for _, jsonFile := range jsonFiles {
if strings.Contains(jsonFile, ".partial.json") {
continue
}
file, err := os.Stat(jsonFile)
if err != nil || !file.IsDir() {
scanFunction(jsonFile)
}
}
}
func ScanForExamples(rootPath string) []error {
var errors []error
ScanJSONFiles(rootPath, func(jsonPath string) {
jsonObject, err := GetObjectFromFile(jsonPath)
if err != nil {
errors = append(errors, err)
return
}
FindExamples(jsonObject, func(example Example, parseErr error) {
if example.examplePath == "" || example.schemaPath == "" {
errors = append(errors, parseErr)
return
}
examplePath := filepath.Join(filepath.Dir(jsonPath), example.examplePath)
schemaPath := filepath.Join(filepath.Dir(jsonPath), example.schemaPath)
// Windows
examplePath = strings.ReplaceAll(examplePath, `\`, `/`)
schemaPath = strings.ReplaceAll(schemaPath, `\`, `/`)
if parseErr != nil {
errors = append(errors, parseErr)
return
}
exampleLoader, exampleLoaderErr := GetReferenceLoader(examplePath)
exampleSchemaLoader, exampleSchemaLoaderErr := GetReferenceLoader(schemaPath)
if exampleLoaderErr != nil {
errors = append(errors, fmt.Errorf("[example=%s, schema=%s] %s", example.examplePath, example.schemaPath, exampleLoaderErr))
return
}
if exampleSchemaLoaderErr != nil {
errors = append(errors, fmt.Errorf("[example=%s, schema=%s] %s", example.examplePath, example.schemaPath, exampleSchemaLoaderErr))
return
}
result, valErr := gojsonschema.Validate(*exampleSchemaLoader, *exampleLoader)
if valErr != nil {
errors = append(errors, fmt.Errorf("%s: %s", example.examplePath, valErr))
return
}
// Handle example array.
if len(result.Errors()) > 0 && strings.Contains(result.Errors()[0].String(), arrayError) {
exampleLoaders, err := unpackArray(*exampleLoader)
if err != nil {
errors = append(errors, fmt.Errorf("%s: %s", example.examplePath, err))
return
}
for _, exampleLoader := range exampleLoaders {
result, valErr := gojsonschema.Validate(*exampleSchemaLoader, exampleLoader)
if valErr != nil {
errors = append(errors, fmt.Errorf("%s: %s", example.examplePath, valErr))
return
}
for _, err := range result.Errors() {
errors = append(errors, fmt.Errorf("%s: %s", example.examplePath, err.String()))
}
}
}
for _, err := range result.Errors() {
if !strings.Contains(result.Errors()[0].String(), arrayError) {
errors = append(errors, fmt.Errorf("%s: %s", example.examplePath, err.String()))
}
}
})
})
return errors
}
// `gojsonschema` doesn't handle reference paths that point to specific fields like:
// file://aaa/bbb.json#definitions/example/something
// It will return the root object of that JSON file, completely ignoring the part after #
// As a workaround, this function loads that file, calls a JsonPath query to get the referenced object.
// Also, gojsonschema is not able to resolve schema references if a JSON object is not saved on disk.
// *Side effects*
// If arefPath contains the link to an object, it will generate a file on your file system.
// Related: https://github.com/xeipuuv/gojsonschema/issues/262
func GetReferenceLoader(refPath string) (*gojsonschema.JSONLoader, error) {
simpleLoader := gojsonschema.NewReferenceLoader(fmt.Sprintf("file://%s", refPath))
if !strings.Contains(refPath, "#") {
return &simpleLoader, nil
}
pathParts := strings.Split(refPath, "#")
filePath := pathParts[0]
objectPath := pathParts[1]
if len(filePath) == 0 || len(objectPath) == 0 {
return &simpleLoader, nil
}
jsonLoader := gojsonschema.NewReferenceLoader(fmt.Sprintf("file://%s", filePath))
jsonPath := TranslateReferenceToJSONPath(objectPath)
jsonObj, refErr := jsonLoader.LoadJSON()
if refErr != nil {
return nil, fmt.Errorf("can't load the path: %s:%s", jsonPath, refErr)
}
foundObject, jsonPathErr := jsonpath.Get(jsonPath, jsonObj)
if jsonPathErr != nil {
return nil, fmt.Errorf("can't find the path: %s:%s %+v ", jsonPath, jsonPathErr, jsonObj)
}
partialLoader := gojsonschema.NewGoLoader(foundObject)
return &partialLoader, nil
}
// unpack examples array into separate objects
func unpackArray(loader gojsonschema.JSONLoader) ([]gojsonschema.JSONLoader, error) {
obj, err := loader.LoadJSON()
if err != nil {
return nil, err
}
objArray, ok := obj.([]interface{})
if !ok {
return nil, fmt.Errorf("can't unpack example array")
}
loaders := make([]gojsonschema.JSONLoader, 0, len(objArray))
for _, elem := range objArray {
loaders = append(loaders, gojsonschema.NewGoLoader(elem))
}
return loaders, nil
}
// Translate a OpenAPI reference path ($ref) to a JsonPath query in a dialect accepted by the `jsonpath` lib.
// e.g.
// /definitions/request -> $.definitions.request
// /definitions/request/200 -> $.definitions.request["200"]
// `jsonpath` throws an error if you try to access fields like this:
// `$.definitions.request.200`
// Related: https://github.com/PaesslerAG/jsonpath/issues/23
func TranslateReferenceToJSONPath(refPath string) string {
var jsonPath []string
for _, part := range strings.Split(refPath, "/") {
if _, err := strconv.Atoi(part); err == nil {
jsonPath = append(jsonPath, fmt.Sprintf("[\"%s\"]", part))
} else {
if len(part) > 0 {
jsonPath = append(jsonPath, ".", part)
}
}
}
return fmt.Sprintf("$%s", strings.Join(jsonPath, ""))
}