forked from emicklei/go-restful-openapi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdefinition_builder_test.go
187 lines (171 loc) · 5.72 KB
/
definition_builder_test.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
package restfulspec
import (
"testing"
"github.com/go-openapi/spec"
)
type Apple struct {
Species string
Volume int `json:"vol"`
Things *[]string
}
func TestAppleDef(t *testing.T) {
db := definitionBuilder{Definitions: spec.Definitions{}, Config: Config{}}
db.addModelFrom(Apple{})
if got, want := len(db.Definitions), 1; got != want {
t.Errorf("got %v want %v", got, want)
}
schema := db.Definitions["restfulspec.Apple"]
if got, want := len(schema.Required), 3; got != want {
t.Errorf("got %v want %v", got, want)
}
if got, want := schema.Required[0], "Species"; got != want {
t.Errorf("got %v want %v", got, want)
}
if got, want := schema.Required[1], "vol"; got != want {
t.Errorf("got %v want %v", got, want)
}
if got, want := schema.ID, ""; got != want {
t.Errorf("got %v want %v", got, want)
}
if got, want := schema.Properties["Things"].Items.Schema.Type.Contains("string"), true; got != want {
t.Errorf("got %v want %v", got, want)
}
if got, want := schema.Properties["Things"].Items.Schema.Ref.String(), ""; got != want {
t.Errorf("got %v want %v", got, want)
}
}
type MyDictionaryResponse struct {
Dictionary1 map[string]DictionaryValue `json:"dictionary1"`
Dictionary2 map[string]interface{} `json:"dictionary2"`
}
type DictionaryValue struct {
Key1 string `json:"key1"`
Key2 string `json:"key2"`
}
func TestDictionarySupport(t *testing.T) {
db := definitionBuilder{Definitions: spec.Definitions{}, Config: Config{}}
db.addModelFrom(MyDictionaryResponse{})
// Make sure that only the types that we want were created.
if got, want := len(db.Definitions), 2; got != want {
t.Errorf("got %v want %v", got, want)
}
schema, schemaFound := db.Definitions["restfulspec.MyDictionaryResponse"]
if !schemaFound {
t.Errorf("could not find schema")
} else {
if got, want := len(schema.Required), 2; got != want {
t.Errorf("got %v want %v", got, want)
} else {
if got, want := schema.Required[0], "dictionary1"; got != want {
t.Errorf("got %v want %v", got, want)
}
if got, want := schema.Required[1], "dictionary2"; got != want {
t.Errorf("got %v want %v", got, want)
}
}
if got, want := len(schema.Properties), 2; got != want {
t.Errorf("got %v want %v", got, want)
} else {
if property, found := schema.Properties["dictionary1"]; !found {
t.Errorf("could not find property")
} else {
if got, want := property.AdditionalProperties.Schema.SchemaProps.Ref.String(), "#/definitions/restfulspec.DictionaryValue"; got != want {
t.Errorf("got %v want %v", got, want)
}
}
if property, found := schema.Properties["dictionary2"]; !found {
t.Errorf("could not find property")
} else {
if property.AdditionalProperties != nil {
t.Errorf("unexpected additional properties")
}
}
}
}
schema, schemaFound = db.Definitions["restfulspec.DictionaryValue"]
if !schemaFound {
t.Errorf("could not find schema")
} else {
if got, want := len(schema.Required), 2; got != want {
t.Errorf("got %v want %v", got, want)
} else {
if got, want := schema.Required[0], "key1"; got != want {
t.Errorf("got %v want %v", got, want)
}
if got, want := schema.Required[1], "key2"; got != want {
t.Errorf("got %v want %v", got, want)
}
}
}
}
type MyRecursiveDictionaryResponse struct {
Dictionary1 map[string]RecursiveDictionaryValue `json:"dictionary1"`
}
type RecursiveDictionaryValue struct {
Key1 string `json:"key1"`
Key2 map[string]RecursiveDictionaryValue `json:"key2"`
}
func TestRecursiveDictionarySupport(t *testing.T) {
db := definitionBuilder{Definitions: spec.Definitions{}, Config: Config{}}
db.addModelFrom(MyRecursiveDictionaryResponse{})
// Make sure that only the types that we want were created.
if got, want := len(db.Definitions), 2; got != want {
t.Errorf("got %v want %v", got, want)
}
schema, schemaFound := db.Definitions["restfulspec.MyRecursiveDictionaryResponse"]
if !schemaFound {
t.Errorf("could not find schema")
} else {
if got, want := len(schema.Required), 1; got != want {
t.Errorf("got %v want %v", got, want)
} else {
if got, want := schema.Required[0], "dictionary1"; got != want {
t.Errorf("got %v want %v", got, want)
}
}
if got, want := len(schema.Properties), 1; got != want {
t.Errorf("got %v want %v", got, want)
} else {
if property, found := schema.Properties["dictionary1"]; !found {
t.Errorf("could not find property")
} else {
if got, want := property.AdditionalProperties.Schema.SchemaProps.Ref.String(), "#/definitions/restfulspec.RecursiveDictionaryValue"; got != want {
t.Errorf("got %v want %v", got, want)
}
}
}
}
schema, schemaFound = db.Definitions["restfulspec.RecursiveDictionaryValue"]
if !schemaFound {
t.Errorf("could not find schema")
} else {
if got, want := len(schema.Required), 2; got != want {
t.Errorf("got %v want %v", got, want)
} else {
if got, want := schema.Required[0], "key1"; got != want {
t.Errorf("got %v want %v", got, want)
}
if got, want := schema.Required[1], "key2"; got != want {
t.Errorf("got %v want %v", got, want)
}
}
if got, want := len(schema.Properties), 2; got != want {
t.Errorf("got %v want %v", got, want)
} else {
if property, found := schema.Properties["key1"]; !found {
t.Errorf("could not find property")
} else {
if property.AdditionalProperties != nil {
t.Errorf("unexpected additional properties")
}
}
if property, found := schema.Properties["key2"]; !found {
t.Errorf("could not find property")
} else {
if got, want := property.AdditionalProperties.Schema.SchemaProps.Ref.String(), "#/definitions/restfulspec.RecursiveDictionaryValue"; got != want {
t.Errorf("got %v want %v", got, want)
}
}
}
}
}