-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparameter_test.go
208 lines (198 loc) · 5.76 KB
/
parameter_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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
package openapi_test
import (
"testing"
"github.com/MarkRosemaker/openapi"
"github.com/go-json-experiment/json/jsontext"
)
func TestParameter_JSON(t *testing.T) {
t.Parallel()
// A header parameter with an array of 64 bit integer numbers:
testJSON(t, []byte(`{
"name": "token",
"in": "header",
"description": "token to be passed as a header",
"required": true,
"style": "simple",
"schema": {
"type": "array",
"items": {
"type": "integer",
"format": "int64"
}
}
}`), &openapi.Parameter{})
// A path parameter of a string value:
testJSON(t, []byte(`{
"name": "username",
"in": "path",
"description": "username to fetch",
"required": true,
"schema": {
"type": "string"
}
}`), &openapi.Parameter{})
// An optional query parameter of a string value, allowing multiple values by repeating the query parameter:
testJSON(t, []byte(`{
"name": "id",
"in": "query",
"description": "ID of the object to fetch",
"style": "form",
"explode": true,
"schema": {
"type": "array",
"items": {
"type": "string"
}
}
}`), &openapi.Parameter{})
// A free-form query parameter, allowing undefined parameters of a specific type:
testJSON(t, []byte(`{
"name": "freeForm",
"in": "query",
"style": "form",
"schema": {
"type": "object",
"additionalProperties": {
"type": "integer"
}
}
}`), &openapi.Parameter{})
// A complex parameter using `content` to define serialization:
testJSON(t, []byte(`{
"name": "coordinates",
"in": "query",
"content": {
"application/json": {
"schema": {
"type": "object",
"properties": {
"lat": {
"type": "number"
},
"long": {
"type": "number"
}
},
"required": [
"lat",
"long"
]
}
}
}
}`), &openapi.Parameter{})
}
func TestParameter_Validate_Error(t *testing.T) {
t.Parallel()
for _, tc := range []struct {
p openapi.Parameter
err string
}{
{openapi.Parameter{}, "name is required"},
{openapi.Parameter{
Name: "myname",
}, "in is required"},
{openapi.Parameter{
Name: "myname",
In: "foo",
}, `in ("foo") is invalid, must be one of: "path", "query", "header", "cookie"`},
{openapi.Parameter{
Name: "myname",
In: openapi.ParameterLocationPath,
}, "required (false) is invalid: must be true for path parameters"},
{openapi.Parameter{
Name: "myname",
In: openapi.ParameterLocationPath,
Required: true,
}, "schema or content is required"},
{openapi.Parameter{
Name: "myname",
In: openapi.ParameterLocationPath,
Required: true,
Schema: &openapi.Schema{},
}, "schema.type is required"},
{openapi.Parameter{
Name: "myname",
In: openapi.ParameterLocationPath,
Required: true,
Schema: &openapi.Schema{Type: openapi.TypeString},
AllowEmptyValue: true,
}, `allowEmptyValue (true) is invalid: can only be true for query parameters, got "path"`},
{openapi.Parameter{
Name: "myname",
In: openapi.ParameterLocationPath,
Required: true,
Schema: &openapi.Schema{Type: openapi.TypeString},
AllowReserved: true,
}, `allowReserved (true) is invalid: only applies to query parameters, got "path"`},
{openapi.Parameter{
Name: "myname",
In: openapi.ParameterLocationPath,
Required: true,
Schema: &openapi.Schema{Type: openapi.TypeString},
Content: openapi.Content{},
}, `schema and content are mutually exclusive`},
{openapi.Parameter{
Name: "myname",
In: openapi.ParameterLocationPath,
Required: true,
Content: openapi.Content{},
}, `content is invalid: must contain exactly one entry, got 0`},
{openapi.Parameter{
Name: "myname",
In: openapi.ParameterLocationPath,
Required: true,
Content: openapi.Content{"foo/bar; baz": {}},
}, `content["foo/bar; baz"]: mime: invalid media parameter`},
{openapi.Parameter{
Name: "myname",
In: openapi.ParameterLocationPath,
Required: true,
Content: openapi.Content{"foo": {}},
Style: "foo",
}, `style ("foo") is invalid, must be one of: "matrix", "label", "form", "simple", "spaceDelimited", "pipeDelimited", "deepObject"`},
{openapi.Parameter{
Name: "myname",
In: openapi.ParameterLocationPath,
Required: true,
Content: openapi.Content{"foo": {}},
Explode: true,
}, `explode (true) is invalid: property has no effect when schema is not present`},
{openapi.Parameter{
Name: "myname",
In: openapi.ParameterLocationPath,
Required: true,
Schema: &openapi.Schema{Type: openapi.TypeString},
Explode: true,
}, `explode (true) is invalid: property has no effect when schema type is not array or object, got "string"`},
{openapi.Parameter{
Name: "myname",
In: openapi.ParameterLocationPath,
Required: true,
Schema: &openapi.Schema{Type: openapi.TypeString},
Example: jsontext.Value("foo"),
Examples: openapi.Examples{},
}, `example and examples are mutually exclusive`},
{openapi.Parameter{
Name: "myname",
In: openapi.ParameterLocationPath,
Required: true,
Schema: &openapi.Schema{Type: openapi.TypeString},
Extensions: []byte(`{"foo": "bar"}`),
}, `foo: ` + openapi.ErrUnknownField.Error()},
{openapi.Parameter{
Name: "myname",
In: openapi.ParameterLocationQuery,
Schema: &openapi.Schema{Type: openapi.TypeString},
Examples: openapi.Examples{
"foo": invalidExample,
},
}, `examples["foo"]: value and externalValue are mutually exclusive`},
} {
t.Run(tc.err, func(t *testing.T) {
if err := tc.p.Validate(); err == nil || err.Error() != tc.err {
t.Fatalf("want: %s, got: %s", tc.err, err)
}
})
}
}