-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjson_test.go
More file actions
129 lines (109 loc) · 3.07 KB
/
Copy pathjson_test.go
File metadata and controls
129 lines (109 loc) · 3.07 KB
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
package openapi_test
import (
"bytes"
"encoding/json/jsontext"
"encoding/json/v2"
"path/filepath"
"testing"
"github.com/MarkRosemaker/jsonutil"
"github.com/MarkRosemaker/openapi"
)
var jsonOpts = json.JoinOptions([]json.Options{
// unevaluatedProperties is set to false in most objects according to the OpenAPI specification
// also protect against deleting unknown fields when overwriting later
json.RejectUnknownMembers(true),
json.WithMarshalers(json.JoinMarshalers(
json.MarshalToFunc(jsonutil.URLMarshal),
)),
json.WithUnmarshalers(json.JoinUnmarshalers(
json.UnmarshalFromFunc(jsonutil.URLUnmarshal),
)),
jsontext.WithIndent(" "), // indent with two spaces
}...)
func resolveSchemaRef(s *openapi.SchemaRef) {
if s != nil && s.Ref != nil && s.Value == nil {
s.Value = &openapi.Schema{}
}
}
func resolveExamples(examples openapi.Examples) {
for _, ex := range examples {
if ex.Ref != nil && ex.Value == nil {
ex.Value = &openapi.Example{}
}
}
}
type validator interface{ Validate() error }
func testJSON(t *testing.T, exampleJSON []byte, v validator) {
t.Helper()
switch v.(type) {
case *openapi.Document:
doc, err := openapi.LoadFromDataJSON(exampleJSON)
if err != nil {
t.Fatalf("load from data: %v", err)
}
v = doc
if _, err = doc.ToJSON(); err != nil {
t.Fatalf("to json: %v", err)
}
if err := doc.WriteToFile(filepath.Join(t.TempDir(), "foo", "openapi.json")); err != nil {
t.Fatalf("write to file: %v", err)
}
default:
if err := json.Unmarshal(exampleJSON, v, jsonOpts); err != nil {
t.Fatalf("initial unmarshal: %v", err)
}
}
// manually add unresolved references
fixReferences(v)
if err := v.Validate(); err != nil {
t.Fatalf("validate: %v", err)
}
b, err := json.Marshal(v, jsonOpts)
if err != nil {
t.Fatal(err)
}
got := jsontext.Value(b)
want := jsontext.Value(exampleJSON)
if err := want.Indent(); err != nil {
t.Fatal(err)
}
if err := got.Indent(); err != nil {
t.Fatal(err)
}
// NOTE: we want to avoid this dependency
// require.Equal(t, string(want), string(got))
if !bytes.Equal(want, got) {
t.Fatalf("not equal, want:\n%s\ngot:\n%s", exampleJSON, got)
}
}
func fixReferences(v validator) {
switch v := v.(type) {
case *openapi.Callback:
for _, pi := range *v {
resolveSchemaRef(pi.Value.Post.RequestBody.Value.Content[openapi.MediaRangeJSON].Schema)
}
case *openapi.Content:
for _, mt := range *v {
resolveSchemaRef(mt.Schema)
resolveExamples(mt.Examples)
}
case *openapi.RequestBody:
for _, c := range v.Content {
resolveSchemaRef(c.Schema)
}
case *openapi.ParameterList:
for _, p := range *v {
resolveExamples(p.Value.Examples)
}
case *openapi.OperationResponses:
for _, r := range *v {
mt := r.Value.Content[openapi.MediaRangeJSON]
resolveSchemaRef(mt.Schema)
resolveExamples(mt.Examples)
}
case *openapi.PathItem:
resolveSchemaRef(v.Get.Responses["default"].Value.Content[openapi.MediaRangeHTML].Schema)
case *openapi.Components:
v.Responses["GeneralError"].Value.Content[openapi.MediaRangeJSON].Schema.Value = v.Schemas["GeneralError"]
}
}