Repository navigation
Expand file tree
/
Copy pathdiscriminator_test.go
More file actions
275 lines (244 loc) · 7.73 KB
/
Copy pathdiscriminator_test.go
File metadata and controls
275 lines (244 loc) · 7.73 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
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
269
270
271
272
273
274
275
// SPDX-FileCopyrightText: Copyright 2015-2025 go-swagger maintainers
// SPDX-License-Identifier: Apache-2.0
package validate
import (
"encoding/json"
"fmt"
"testing"
"github.com/go-openapi/analysis"
"github.com/go-openapi/loads"
"github.com/go-openapi/strfmt"
"github.com/go-openapi/testify/v2/assert"
"github.com/go-openapi/testify/v2/require"
)
// discriminatorSpec builds a specification holding the given definitions and nothing else of note.
func discriminatorSpec(definitions string) string {
return fmt.Sprintf(`{
"swagger": "2.0",
"info": {"title": "t", "version": "1"},
"paths": {},
"definitions": %s
}`, definitions)
}
// discriminatorValidatorFromJSON builds a SpecValidator wired the way Validate does, up to the
// point validateDiscriminators needs.
func discriminatorValidatorFromJSON(t *testing.T, doc string) *SpecValidator {
t.Helper()
d, err := loads.Analyzed(json.RawMessage(doc), "")
require.NoError(t, err)
s := NewSpecValidator(d.Schema(), strfmt.Default)
s.spec = d
s.analyzer = analysis.New(d.Spec())
return s
}
func TestValidateDiscriminators(t *testing.T) {
t.Parallel()
const (
petTypeNotDefined = `discriminator "petType" of "Pet" is not defined as a property of that schema`
petTypeNotRequired = `discriminator "petType" of "Pet" is not in the required property list`
)
tests := []struct {
name string
defs string
wantErrors []string
}{
{
name: "discriminator defined and required",
defs: `{
"Pet": {
"type": "object",
"discriminator": "petType",
"required": ["petType"],
"properties": {"petType": {"type": "string"}}
}
}`,
},
{
name: "no discriminator at all",
defs: `{"Pet": {"type": "object", "properties": {"name": {"type": "string"}}}}`,
},
{
name: "discriminator names an undeclared property",
defs: `{
"Pet": {
"type": "object",
"discriminator": "petType",
"required": ["petType"],
"properties": {"name": {"type": "string"}}
}
}`,
wantErrors: []string{petTypeNotDefined},
},
{
name: "discriminator property is optional",
defs: `{
"Pet": {
"type": "object",
"discriminator": "petType",
"properties": {"petType": {"type": "string"}}
}
}`,
wantErrors: []string{petTypeNotRequired},
},
{
name: "discriminator neither defined nor required reports both",
defs: `{
"Pet": {
"type": "object",
"discriminator": "petType",
"properties": {"name": {"type": "string"}}
}
}`,
wantErrors: []string{
petTypeNotDefined,
petTypeNotRequired,
},
},
{
name: "property and required contributed by an allOf $ref",
defs: `{
"Base": {
"type": "object",
"required": ["petType"],
"properties": {"petType": {"type": "string"}}
},
"Pet": {
"discriminator": "petType",
"allOf": [
{"$ref": "#/definitions/Base"},
{"type": "object", "properties": {"name": {"type": "string"}}}
]
}
}`,
},
{
name: "property contributed by an inline allOf member",
defs: `{
"Pet": {
"discriminator": "petType",
"allOf": [
{"type": "object", "required": ["petType"], "properties": {"petType": {"type": "string"}}}
]
}
}`,
},
{
name: "allOf contributes the property but nothing requires it",
defs: `{
"Base": {"type": "object", "properties": {"petType": {"type": "string"}}},
"Pet": {
"discriminator": "petType",
"allOf": [{"$ref": "#/definitions/Base"}]
}
}`,
wantErrors: []string{petTypeNotRequired},
},
{
name: "several definitions are all checked, in name order",
defs: `{
"Alpha": {"type": "object", "discriminator": "kind", "required": ["kind"]},
"Beta": {"type": "object", "discriminator": "sort", "required": ["sort"]}
}`,
wantErrors: []string{
`discriminator "kind" of "Alpha" is not defined as a property of that schema`,
`discriminator "sort" of "Beta" is not defined as a property of that schema`,
},
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
t.Parallel()
res := discriminatorValidatorFromJSON(t, discriminatorSpec(tc.defs)).validateDiscriminators()
assert.Equal(t, tc.wantErrors, nonEmpty(errorMessages(res)))
assert.Empty(t, warningMessages(res), "the rule reports errors only")
})
}
}
// TestDiscriminatorInNestedSchema covers a discriminator that sits below a definition rather than
// on it: the same slip, wherever it is written.
func TestDiscriminatorInNestedSchema(t *testing.T) {
t.Parallel()
doc := discriminatorSpec(`{
"Zoo": {
"type": "object",
"properties": {
"resident": {
"type": "object",
"discriminator": "petType",
"properties": {"name": {"type": "string"}}
}
}
}
}`)
res := discriminatorValidatorFromJSON(t, doc).validateDiscriminators()
assert.Equal(t, []string{
`discriminator "petType" of "Zoo.resident" is not defined as a property of that schema`,
`discriminator "petType" of "Zoo.resident" is not in the required property list`,
}, errorMessages(res))
}
// TestDiscriminatorBehindRefCheckedOnce guards the choice not to follow a $ref: the fault is
// reported where the definition is written, not again at every schema pointing at it.
func TestDiscriminatorBehindRefCheckedOnce(t *testing.T) {
t.Parallel()
doc := discriminatorSpec(`{
"Pet": {"type": "object", "discriminator": "petType"},
"First": {"$ref": "#/definitions/Pet"},
"Second": {"type": "object", "properties": {"pet": {"$ref": "#/definitions/Pet"}}}
}`)
res := discriminatorValidatorFromJSON(t, doc).validateDiscriminators()
assert.Equal(t, []string{
`discriminator "petType" of "Pet" is not defined as a property of that schema`,
`discriminator "petType" of "Pet" is not in the required property list`,
}, errorMessages(res))
}
// TestDiscriminatorRecursiveDefinitionTerminates guards the same choice against the reason it was
// made: a definition holding itself would not terminate if the walk followed $ref.
func TestDiscriminatorRecursiveDefinitionTerminates(t *testing.T) {
t.Parallel()
doc := discriminatorSpec(`{
"Node": {
"type": "object",
"discriminator": "kind",
"required": ["kind"],
"properties": {
"kind": {"type": "string"},
"child": {"$ref": "#/definitions/Node"}
}
}
}`)
res := discriminatorValidatorFromJSON(t, doc).validateDiscriminators()
assert.Empty(t, errorMessages(res))
}
// TestDiscriminatorLocations pins down where a discriminator finding says it happened. The
// pointers come from a full Validate, which trims a location to a node the document holds.
func TestDiscriminatorLocations(t *testing.T) {
t.Parallel()
doc := discriminatorSpec(`{
"Pet": {"type": "object", "discriminator": "petType", "required": ["petType"]},
"Zoo": {
"type": "object",
"properties": {
"resident": {"type": "object", "discriminator": "kind", "required": ["kind"]}
}
}
}`)
d, err := loads.Analyzed(json.RawMessage(doc), "")
require.NoError(t, err)
validator := NewSpecValidator(d.Schema(), strfmt.Default)
validator.SetContinueOnErrors(true)
errs, _ := validator.Validate(d)
pointers := pointersOf(errs.LocatedErrors())
assert.SliceContainsT(t, pointers, "/definitions/Pet/discriminator")
assert.SliceContainsT(t, pointers, "/definitions/Zoo/properties/resident/discriminator")
}
// TestDiscriminatorMakesSpecInvalid checks that the rule reaches Spec, the package's front door,
// rather than only the validator method the other tests call.
func TestDiscriminatorMakesSpecInvalid(t *testing.T) {
t.Parallel()
doc := discriminatorSpec(`{"Pet": {"type": "object", "discriminator": "petType"}}`)
d, err := loads.Analyzed(json.RawMessage(doc), "")
require.NoError(t, err)
err = Spec(d, strfmt.Default)
require.Error(t, err)
assert.StringContainsT(t, err.Error(), `discriminator "petType" of "Pet"`)
}