| 1 |
// Copyright 2023 Princess B33f Heavy Industries / Dave Shanley |
| 2 |
// SPDX-License-Identifier: MIT |
| 3 |
|
| 4 |
package schema_validation |
| 5 |
|
| 6 |
import ( |
| 7 |
"encoding/json" |
| 8 |
"testing" |
| 9 |
|
| 10 |
"github.com/pb33f/libopenapi" |
| 11 |
"github.com/stretchr/testify/assert" |
| 12 |
"go.yaml.in/yaml/v4" |
| 13 |
) |
| 14 |
|
| 15 |
func TestLocateSchemaPropertyNodeByJSONPath(t *testing.T) { |
| 16 |
spec := `openapi: 3.1.0 |
| 17 |
paths: |
| 18 |
/burgers/createBurger: |
| 19 |
post: |
| 20 |
requestBody: |
| 21 |
content: |
| 22 |
application/json: |
| 23 |
schema: |
| 24 |
type: object |
| 25 |
properties: |
| 26 |
name: |
| 27 |
type: string |
| 28 |
patties: |
| 29 |
type: integer |
| 30 |
vegetarian: |
| 31 |
type: boolean` |
| 32 |
|
| 33 |
var node yaml.Node |
| 34 |
_ = yaml.Unmarshal([]byte(spec), &node) |
| 35 |
|
| 36 |
foundNode := LocateSchemaPropertyNodeByJSONPath(node.Content[0], |
| 37 |
"/paths/~1burgers~1createBurger/post/requestBody/content/application~1json/schema/properties/vegetarian") |
| 38 |
|
| 39 |
assert.Equal(t, "boolean", foundNode.Content[1].Value) |
| 40 |
|
| 41 |
foundNode = LocateSchemaPropertyNodeByJSONPath(node.Content[0], |
| 42 |
"/i/do/not/exist") |
| 43 |
|
| 44 |
assert.Nil(t, foundNode) |
| 45 |
} |
| 46 |
|
| 47 |
func TestValidateSchema_SimpleValid_String(t *testing.T) { |
| 48 |
spec := `openapi: 3.1.0 |
| 49 |
paths: |
| 50 |
/burgers/createBurger: |
| 51 |
post: |
| 52 |
requestBody: |
| 53 |
content: |
| 54 |
application/json: |
| 55 |
schema: |
| 56 |
type: object |
| 57 |
properties: |
| 58 |
name: |
| 59 |
type: string |
| 60 |
patties: |
| 61 |
type: integer |
| 62 |
vegetarian: |
| 63 |
type: boolean` |
| 64 |
|
| 65 |
doc, _ := libopenapi.NewDocument([]byte(spec)) |
| 66 |
|
| 67 |
m, _ := doc.BuildV3Model() |
| 68 |
|
| 69 |
body := map[string]interface{}{ |
| 70 |
"name": "Big Mac", |
| 71 |
"patties": 2, |
| 72 |
"vegetarian": true, |
| 73 |
} |
| 74 |
|
| 75 |
bodyBytes, _ := json.Marshal(body) |
| 76 |
sch := m.Model.Paths.PathItems.GetOrZero("/burgers/createBurger").Post.RequestBody.Content.GetOrZero("application/json").Schema |
| 77 |
|
| 78 |
// create a schema validator |
| 79 |
v := NewSchemaValidator() |
| 80 |
|
| 81 |
// validate! |
| 82 |
valid, errors := v.ValidateSchemaString(sch.Schema(), string(bodyBytes)) |
| 83 |
|
| 84 |
assert.True(t, valid) |
| 85 |
assert.Len(t, errors, 0) |
| 86 |
} |
| 87 |
|
| 88 |
func TestValidateSchema_SimpleValid(t *testing.T) { |
| 89 |
spec := `openapi: 3.1.0 |
| 90 |
paths: |
| 91 |
/burgers/createBurger: |
| 92 |
post: |
| 93 |
requestBody: |
| 94 |
content: |
| 95 |
application/json: |
| 96 |
schema: |
| 97 |
type: object |
| 98 |
properties: |
| 99 |
name: |
| 100 |
type: string |
| 101 |
patties: |
| 102 |
type: integer |
| 103 |
vegetarian: |
| 104 |
type: boolean` |
| 105 |
|
| 106 |
doc, _ := libopenapi.NewDocument([]byte(spec)) |
| 107 |
|
| 108 |
m, _ := doc.BuildV3Model() |
| 109 |
|
| 110 |
body := map[string]interface{}{ |
| 111 |
"name": "Big Mac", |
| 112 |
"patties": 2, |
| 113 |
"vegetarian": true, |
| 114 |
} |
| 115 |
|
| 116 |
// create a schema validator |
| 117 |
v := NewSchemaValidator() |
| 118 |
|
| 119 |
bodyBytes, _ := json.Marshal(body) |
| 120 |
sch := m.Model.Paths.PathItems.GetOrZero("/burgers/createBurger").Post.RequestBody.Content.GetOrZero("application/json").Schema |
| 121 |
|
| 122 |
// validate! |
| 123 |
valid, errors := v.ValidateSchemaBytes(sch.Schema(), bodyBytes) |
| 124 |
|
| 125 |
assert.True(t, valid) |
| 126 |
assert.Len(t, errors, 0) |
| 127 |
} |
| 128 |
|
| 129 |
func TestValidateSchema_SimpleInValid(t *testing.T) { |
| 130 |
spec := `openapi: 3.1.0 |
| 131 |
paths: |
| 132 |
/burgers/createBurger: |
| 133 |
post: |
| 134 |
requestBody: |
| 135 |
content: |
| 136 |
application/json: |
| 137 |
schema: |
| 138 |
type: object |
| 139 |
properties: |
| 140 |
name: |
| 141 |
type: string |
| 142 |
patties: |
| 143 |
type: integer |
| 144 |
vegetarian: |
| 145 |
type: boolean` |
| 146 |
|
| 147 |
doc, _ := libopenapi.NewDocument([]byte(spec)) |
| 148 |
|
| 149 |
m, _ := doc.BuildV3Model() |
| 150 |
|
| 151 |
body := map[string]interface{}{ |
| 152 |
"name": "Big Mac", |
| 153 |
"patties": "I am not a number", // will fail |
| 154 |
"vegetarian": 23, // will fail |
| 155 |
} |
| 156 |
|
| 157 |
// create a schema validator |
| 158 |
v := NewSchemaValidator() |
| 159 |
|
| 160 |
bodyBytes, _ := json.Marshal(body) |
| 161 |
sch := m.Model.Paths.PathItems.GetOrZero("/burgers/createBurger").Post.RequestBody.Content.GetOrZero("application/json").Schema |
| 162 |
|
| 163 |
// validate! |
| 164 |
valid, errors := v.ValidateSchemaBytes(sch.Schema(), bodyBytes) |
| 165 |
|
| 166 |
assert.False(t, valid) |
| 167 |
assert.Len(t, errors, 1) |
| 168 |
assert.Len(t, errors[0].SchemaValidationErrors, 2) |
| 169 |
} |
| 170 |
|
| 171 |
func TestValidateSchema_InvalidJSONType(t *testing.T) { |
| 172 |
spec := `openapi: 3.1.0 |
| 173 |
paths: |
| 174 |
/burgers/createBurger: |
| 175 |
post: |
| 176 |
requestBody: |
| 177 |
content: |
| 178 |
application/json: |
| 179 |
schema: |
| 180 |
type: object |
| 181 |
properties: |
| 182 |
name: |
| 183 |
type: string |
| 184 |
patties: |
| 185 |
type: integer |
| 186 |
vegetarian: |
| 187 |
type: boolean` |
| 188 |
|
| 189 |
doc, _ := libopenapi.NewDocument([]byte(spec)) |
| 190 |
|
| 191 |
m, _ := doc.BuildV3Model() |
| 192 |
|
| 193 |
body := struct{ name string }{"hello world"} |
| 194 |
|
| 195 |
// create a schema validator |
| 196 |
v := NewSchemaValidator() |
| 197 |
|
| 198 |
// bodyBytes, _ := json.Marshal(body) |
| 199 |
sch := m.Model.Paths.PathItems.GetOrZero("/burgers/createBurger").Post.RequestBody.Content.GetOrZero("application/json").Schema |
| 200 |
|
| 201 |
// validate! |
| 202 |
valid, errors := v.ValidateSchemaObject(sch.Schema(), body) |
| 203 |
|
| 204 |
assert.False(t, valid) |
| 205 |
assert.Len(t, errors, 1) |
| 206 |
assert.Len(t, errors[0].SchemaValidationErrors, 1) |
| 207 |
assert.Equal(t, "invalid jsonType struct { name string }", errors[0].SchemaValidationErrors[0].Reason) |
| 208 |
} |
| 209 |
|
| 210 |
func TestValidateSchema_ReffyComplex_Valid(t *testing.T) { |
| 211 |
spec := `openapi: 3.1.0 |
| 212 |
components: |
| 213 |
schemas: |
| 214 |
Death: |
| 215 |
type: object |
| 216 |
required: [cakeOrDeath] |
| 217 |
properties: |
| 218 |
cakeOrDeath: |
| 219 |
type: string |
| 220 |
enum: [death] |
| 221 |
Cake: |
| 222 |
type: object |
| 223 |
required: [cakeOrDeath] |
| 224 |
properties: |
| 225 |
cakeOrDeath: |
| 226 |
type: string |
| 227 |
enum: [cake please] |
| 228 |
Four: |
| 229 |
type: object |
| 230 |
oneOf: |
| 231 |
- $ref: '#/components/schemas/Cake' |
| 232 |
- $ref: '#/components/schemas/Death' |
| 233 |
Three: |
| 234 |
type: object |
| 235 |
properties: |
| 236 |
name: |
| 237 |
type: string |
| 238 |
four: |
| 239 |
$ref: '#/components/schemas/Four' |
| 240 |
Two: |
| 241 |
type: object |
| 242 |
properties: |
| 243 |
name: |
| 244 |
type: string |
| 245 |
three: |
| 246 |
$ref: '#/components/schemas/Three' |
| 247 |
One: |
| 248 |
type: object |
| 249 |
properties: |
| 250 |
name: |
| 251 |
type: string |
| 252 |
two: |
| 253 |
$ref: '#/components/schemas/Two' |
| 254 |
paths: |
| 255 |
/burgers/createBurger: |
| 256 |
post: |
| 257 |
requestBody: |
| 258 |
content: |
| 259 |
application/json: |
| 260 |
schema: |
| 261 |
$ref: '#/components/schemas/One'` |
| 262 |
|
| 263 |
doc, _ := libopenapi.NewDocument([]byte(spec)) |
| 264 |
|
| 265 |
m, _ := doc.BuildV3Model() |
| 266 |
|
| 267 |
cakePlease := map[string]interface{}{ |
| 268 |
"two": map[string]interface{}{ |
| 269 |
"three": map[string]interface{}{ |
| 270 |
"four": map[string]interface{}{ |
| 271 |
"cakeOrDeath": "cake please", |
| 272 |
}, |
| 273 |
}, |
| 274 |
}, |
| 275 |
} |
| 276 |
|
| 277 |
death := map[string]interface{}{ |
| 278 |
"two": map[string]interface{}{ |
| 279 |
"three": map[string]interface{}{ |
| 280 |
"four": map[string]interface{}{ |
| 281 |
"cakeOrDeath": "death", |
| 282 |
}, |
| 283 |
}, |
| 284 |
}, |
| 285 |
} |
| 286 |
|
| 287 |
// cake? (https://www.youtube.com/watch?v=PVH0gZO5lq0) |
| 288 |
bodyBytes, _ := json.Marshal(cakePlease) |
| 289 |
sch := m.Model.Paths.PathItems.GetOrZero("/burgers/createBurger").Post.RequestBody.Content.GetOrZero("application/json").Schema |
| 290 |
|
| 291 |
// create a schema validator |
| 292 |
v := NewSchemaValidator() |
| 293 |
|
| 294 |
// validate! |
| 295 |
valid, errors := v.ValidateSchemaBytes(sch.Schema(), bodyBytes) |
| 296 |
|
| 297 |
assert.True(t, valid) |
| 298 |
assert.Len(t, errors, 0) |
| 299 |
|
| 300 |
// or death! |
| 301 |
bodyBytes, _ = json.Marshal(death) |
| 302 |
sch = m.Model.Paths.PathItems.GetOrZero("/burgers/createBurger").Post.RequestBody.Content.GetOrZero("application/json").Schema |
| 303 |
|
| 304 |
// validate! |
| 305 |
valid, errors = v.ValidateSchemaBytes(sch.Schema(), bodyBytes) |
| 306 |
|
| 307 |
assert.True(t, valid) |
| 308 |
assert.Len(t, errors, 0) |
| 309 |
} |
| 310 |
|
| 311 |
func TestValidateSchema_ReffyComplex_Invalid(t *testing.T) { |
| 312 |
spec := `openapi: 3.1.0 |
| 313 |
components: |
| 314 |
schemas: |
| 315 |
Death: |
| 316 |
type: object |
| 317 |
required: [cakeOrDeath] |
| 318 |
properties: |
| 319 |
cakeOrDeath: |
| 320 |
type: string |
| 321 |
enum: [death] |
| 322 |
Cake: |
| 323 |
type: object |
| 324 |
required: [cakeOrDeath] |
| 325 |
properties: |
| 326 |
cakeOrDeath: |
| 327 |
type: string |
| 328 |
enum: [cake please] |
| 329 |
Four: |
| 330 |
type: object |
| 331 |
oneOf: |
| 332 |
- $ref: '#/components/schemas/Cake' |
| 333 |
- $ref: '#/components/schemas/Death' |
| 334 |
Three: |
| 335 |
type: object |
| 336 |
properties: |
| 337 |
name: |
| 338 |
type: string |
| 339 |
four: |
| 340 |
$ref: '#/components/schemas/Four' |
| 341 |
Two: |
| 342 |
type: object |
| 343 |
properties: |
| 344 |
name: |
| 345 |
type: string |
| 346 |
three: |
| 347 |
$ref: '#/components/schemas/Three' |
| 348 |
One: |
| 349 |
type: object |
| 350 |
properties: |
| 351 |
name: |
| 352 |
type: string |
| 353 |
two: |
| 354 |
$ref: '#/components/schemas/Two' |
| 355 |
paths: |
| 356 |
/burgers/createBurger: |
| 357 |
post: |
| 358 |
requestBody: |
| 359 |
content: |
| 360 |
application/json: |
| 361 |
schema: |
| 362 |
$ref: '#/components/schemas/One'` |
| 363 |
|
| 364 |
doc, _ := libopenapi.NewDocument([]byte(spec)) |
| 365 |
|
| 366 |
m, _ := doc.BuildV3Model() |
| 367 |
|
| 368 |
cakePlease := map[string]interface{}{ |
| 369 |
"two": map[string]interface{}{ |
| 370 |
"three": map[string]interface{}{ |
| 371 |
"four": map[string]interface{}{ |
| 372 |
"cakeOrDeath": "no more cake? so the choice is 'or death?'", |
| 373 |
}, |
| 374 |
}, |
| 375 |
}, |
| 376 |
} |
| 377 |
|
| 378 |
death := map[string]interface{}{ |
| 379 |
"two": map[string]interface{}{ |
| 380 |
"three": map[string]interface{}{ |
| 381 |
"four": map[string]interface{}{ |
| 382 |
"cakeOrDeath": "i'll have the chicken", |
| 383 |
}, |
| 384 |
}, |
| 385 |
}, |
| 386 |
} |
| 387 |
|
| 388 |
// cake? (https://www.youtube.com/watch?v=PVH0gZO5lq0) |
| 389 |
bodyBytes, _ := json.Marshal(cakePlease) |
| 390 |
sch := m.Model.Paths.PathItems.GetOrZero("/burgers/createBurger").Post.RequestBody.Content.GetOrZero("application/json").Schema |
| 391 |
|
| 392 |
// create a schema validator |
| 393 |
v := NewSchemaValidator() |
| 394 |
|
| 395 |
// validate! |
| 396 |
valid, errors := v.ValidateSchemaBytes(sch.Schema(), bodyBytes) |
| 397 |
|
| 398 |
assert.False(t, valid) |
| 399 |
assert.Len(t, errors, 1) |
| 400 |
assert.Len(t, errors[0].SchemaValidationErrors, 2) |
| 401 |
|
| 402 |
valid, errors = v.ValidateSchemaObject(sch.Schema(), cakePlease) |
| 403 |
|
| 404 |
assert.False(t, valid) |
| 405 |
assert.Len(t, errors, 1) |
| 406 |
assert.Len(t, errors[0].SchemaValidationErrors, 2) |
| 407 |
|
| 408 |
// or death! |
| 409 |
bodyBytes, _ = json.Marshal(death) |
| 410 |
sch = m.Model.Paths.PathItems.GetOrZero("/burgers/createBurger").Post.RequestBody.Content.GetOrZero("application/json").Schema |
| 411 |
|
| 412 |
// validate! |
| 413 |
valid, errors = v.ValidateSchemaBytes(sch.Schema(), bodyBytes) |
| 414 |
|
| 415 |
assert.False(t, valid) |
| 416 |
assert.Len(t, errors, 1) |
| 417 |
assert.Len(t, errors[0].SchemaValidationErrors, 2) |
| 418 |
|
| 419 |
valid, errors = v.ValidateSchemaObject(sch.Schema(), death) |
| 420 |
|
| 421 |
assert.False(t, valid) |
| 422 |
assert.Len(t, errors, 1) |
| 423 |
assert.Len(t, errors[0].SchemaValidationErrors, 2) |
| 424 |
} |
| 425 |
|
| 426 |
func TestValidateSchema_EmptySchema(t *testing.T) { |
| 427 |
// create a schema validator |
| 428 |
v := NewSchemaValidator() |
| 429 |
|
| 430 |
// validate! |
| 431 |
valid, errors := v.ValidateSchemaObject(nil, nil) |
| 432 |
|
| 433 |
assert.False(t, valid) |
| 434 |
assert.Len(t, errors, 0) |
| 435 |
} |
| 436 |
|
| 437 |
func TestValidateSchema_SimpleInvalid_Multiple(t *testing.T) { |
| 438 |
spec := `openapi: 3.1.0 |
| 439 |
paths: |
| 440 |
/burgers/createBurger: |
| 441 |
post: |
| 442 |
requestBody: |
| 443 |
content: |
| 444 |
application/json: |
| 445 |
schema: |
| 446 |
type: array |
| 447 |
items: |
| 448 |
type: object |
| 449 |
required: |
| 450 |
- name |
| 451 |
properties: |
| 452 |
name: |
| 453 |
type: string |
| 454 |
patties: |
| 455 |
type: integer |
| 456 |
vegetarian: |
| 457 |
type: boolean` |
| 458 |
|
| 459 |
doc, _ := libopenapi.NewDocument([]byte(spec)) |
| 460 |
|
| 461 |
m, _ := doc.BuildV3Model() |
| 462 |
|
| 463 |
var items []map[string]interface{} |
| 464 |
items = append(items, map[string]interface{}{ |
| 465 |
"patties": 1, |
| 466 |
"vegetarian": true, |
| 467 |
}) |
| 468 |
items = append(items, map[string]interface{}{ |
| 469 |
"name": "Quarter Pounder", |
| 470 |
"patties": true, |
| 471 |
"vegetarian": false, |
| 472 |
}) |
| 473 |
items = append(items, map[string]interface{}{ |
| 474 |
"name": "Big Mac", |
| 475 |
"patties": 2, |
| 476 |
"vegetarian": false, |
| 477 |
}) |
| 478 |
|
| 479 |
bodyBytes, _ := json.Marshal(items) |
| 480 |
|
| 481 |
// create a schema validator |
| 482 |
v := NewSchemaValidator() |
| 483 |
|
| 484 |
sch := m.Model.Paths.PathItems.GetOrZero("/burgers/createBurger").Post.RequestBody.Content.GetOrZero("application/json").Schema |
| 485 |
|
| 486 |
// validate! |
| 487 |
valid, errors := v.ValidateSchemaBytes(sch.Schema(), bodyBytes) |
| 488 |
|
| 489 |
assert.False(t, valid) |
| 490 |
assert.Len(t, errors, 1) |
| 491 |
assert.Len(t, errors[0].SchemaValidationErrors, 2) |
| 492 |
} |
| 493 |
|
| 494 |
func TestValidateSchema_BadJSON(t *testing.T) { |
| 495 |
spec := `openapi: 3.1.0 |
| 496 |
paths: |
| 497 |
/burgers/createBurger: |
| 498 |
post: |
| 499 |
requestBody: |
| 500 |
content: |
| 501 |
application/json: |
| 502 |
schema: |
| 503 |
type: object |
| 504 |
properties: |
| 505 |
name: |
| 506 |
type: string |
| 507 |
patties: |
| 508 |
type: integer |
| 509 |
vegetarian: |
| 510 |
type: boolean` |
| 511 |
|
| 512 |
doc, _ := libopenapi.NewDocument([]byte(spec)) |
| 513 |
|
| 514 |
m, _ := doc.BuildV3Model() |
| 515 |
|
| 516 |
bodyBytes := []byte("{\"bad\": \"json\",}") |
| 517 |
sch := m.Model.Paths.PathItems.GetOrZero("/burgers/createBurger").Post.RequestBody.Content.GetOrZero("application/json").Schema |
| 518 |
|
| 519 |
// create a schema validator |
| 520 |
v := NewSchemaValidator() |
| 521 |
|
| 522 |
// validate! |
| 523 |
valid, errors := v.ValidateSchemaString(sch.Schema(), string(bodyBytes)) |
| 524 |
|
| 525 |
assert.False(t, valid) |
| 526 |
assert.Len(t, errors, 1) |
| 527 |
assert.Contains(t, errors[0].Reason, "invalid character '}' looking for beginning of object key string") |
| 528 |
assert.Nil(t, errors[0].SchemaValidationErrors) |
| 529 |
} |
| 530 |
|
| 531 |
func TestValidateSchema_CompilationFailure(t *testing.T) { |
| 532 |
// Test that schema compilation failure doesn't create SchemaValidationErrors |
| 533 |
// This uses an extremely complex regex that might fail compilation in some regex engines |
| 534 |
spec := `openapi: 3.1.0 |
| 535 |
paths: |
| 536 |
/test: |
| 537 |
post: |
| 538 |
requestBody: |
| 539 |
content: |
| 540 |
application/json: |
| 541 |
schema: |
| 542 |
type: object |
| 543 |
properties: |
| 544 |
password: |
| 545 |
type: string |
| 546 |
pattern: '(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[@$!%*?&])[A-Za-z\d@$!%*?&]{8,}(?:(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[@$!%*?&])[A-Za-z\d@$!%*?&]{8,})*'` |
| 547 |
|
| 548 |
doc, _ := libopenapi.NewDocument([]byte(spec)) |
| 549 |
m, _ := doc.BuildV3Model() |
| 550 |
|
| 551 |
sch := m.Model.Paths.PathItems.GetOrZero("/test").Post.RequestBody.Content.GetOrZero("application/json").Schema |
| 552 |
|
| 553 |
// create a schema validator with strict regex engine that might fail on complex patterns |
| 554 |
v := NewSchemaValidator() |
| 555 |
|
| 556 |
// Try to validate - if compilation fails, we should get an error without SchemaValidationErrors |
| 557 |
validData := `{"password": "ValidPass123!"}` |
| 558 |
valid, errors := v.ValidateSchemaString(sch.Schema(), validData) |
| 559 |
|
| 560 |
// This test is environment-dependent - compilation might succeed or fail depending on regex engine |
| 561 |
// If it fails, we want to ensure SchemaValidationErrors is nil |
| 562 |
if !valid && len(errors) > 0 { |
| 563 |
for _, err := range errors { |
| 564 |
if err.Message == "schema compilation failed" { |
| 565 |
// Compilation failure should NOT have SchemaValidationErrors |
| 566 |
assert.Nil(t, err.SchemaValidationErrors, "Schema compilation errors should not have SchemaValidationErrors") |
| 567 |
t.Logf("Schema compilation failed as expected: %s", err.Reason) |
| 568 |
} |
| 569 |
} |
| 570 |
} else { |
| 571 |
t.Skip("Regex engine handled the complex pattern - skipping compilation failure test") |
| 572 |
} |
| 573 |
} |
| 574 |
|
| 575 |
//// https://github.com/pb33f/libopenapi-validator/issues/26 |
| 576 |
//func TestValidateSchema_v3_0_BooleanExclusiveMinimum(t *testing.T) { |
| 577 |
// |
| 578 |
// spec := `openapi: 3.0.0 |
| 579 |
//paths: |
| 580 |
// /burgers/createBurger: |
| 581 |
// post: |
| 582 |
// requestBody: |
| 583 |
// content: |
| 584 |
// application/json: |
| 585 |
// schema: |
| 586 |
// type: object |
| 587 |
// properties: |
| 588 |
// amount: |
| 589 |
// type: number |
| 590 |
// minimum: 0 |
| 591 |
// exclusiveMinimum: true` |
| 592 |
// |
| 593 |
// doc, _ := libopenapi.NewDocument([]byte(spec)) |
| 594 |
// |
| 595 |
// m, _ := doc.BuildV3Model() |
| 596 |
// |
| 597 |
// body := map[string]interface{}{"amount": 3} |
| 598 |
// |
| 599 |
// bodyBytes, _ := json.Marshal(body) |
| 600 |
// sch := m.Model.Paths.PathItems.GetOrZero("/burgers/createBurger"].Post.RequestBody.Content.GetOrZero("application/json"].Schema |
| 601 |
// |
| 602 |
// // create a schema validator |
| 603 |
// v := NewSchemaValidator() |
| 604 |
// |
| 605 |
// // validate! |
| 606 |
// valid, errors := v.ValidateSchemaString(sch.Schema(), string(bodyBytes)) |
| 607 |
// |
| 608 |
// assert.True(t, valid) |
| 609 |
// assert.Empty(t, errors) |
| 610 |
// |
| 611 |
//} |
| 612 |
|
| 613 |
// https://github.com/pb33f/libopenapi-validator/issues/26 |
| 614 |
func TestValidateSchema_v3_0_NumericExclusiveMinimum(t *testing.T) { |
| 615 |
spec := `openapi: 3.0.0 |
| 616 |
paths: |
| 617 |
/burgers/createBurger: |
| 618 |
post: |
| 619 |
requestBody: |
| 620 |
content: |
| 621 |
application/json: |
| 622 |
schema: |
| 623 |
type: object |
| 624 |
properties: |
| 625 |
amount: |
| 626 |
type: number |
| 627 |
exclusiveMinimum: 0` |
| 628 |
|
| 629 |
doc, _ := libopenapi.NewDocument([]byte(spec)) |
| 630 |
|
| 631 |
m, _ := doc.BuildV3Model() |
| 632 |
|
| 633 |
body := map[string]interface{}{"amount": 3} |
| 634 |
|
| 635 |
bodyBytes, _ := json.Marshal(body) |
| 636 |
sch := m.Model.Paths.PathItems.GetOrZero("/burgers/createBurger").Post.RequestBody.Content.GetOrZero("application/json").Schema |
| 637 |
|
| 638 |
// create a schema validator |
| 639 |
v := NewSchemaValidator() |
| 640 |
|
| 641 |
// validate! |
| 642 |
valid, errors := v.ValidateSchemaString(sch.Schema(), string(bodyBytes)) |
| 643 |
|
| 644 |
assert.False(t, valid) |
| 645 |
assert.NotEmpty(t, errors) |
| 646 |
} |
| 647 |
|
| 648 |
// https://github.com/pb33f/libopenapi-validator/issues/26 |
| 649 |
func TestValidateSchema_v3_1_NumericExclusiveMinimum(t *testing.T) { |
| 650 |
spec := `openapi: 3.1.0 |
| 651 |
paths: |
| 652 |
/burgers/createBurger: |
| 653 |
post: |
| 654 |
requestBody: |
| 655 |
content: |
| 656 |
application/json: |
| 657 |
schema: |
| 658 |
type: object |
| 659 |
properties: |
| 660 |
amount: |
| 661 |
type: number |
| 662 |
exclusiveMinimum: 0` |
| 663 |
|
| 664 |
doc, _ := libopenapi.NewDocument([]byte(spec)) |
| 665 |
|
| 666 |
m, _ := doc.BuildV3Model() |
| 667 |
|
| 668 |
body := map[string]interface{}{"amount": 3} |
| 669 |
|
| 670 |
bodyBytes, _ := json.Marshal(body) |
| 671 |
sch := m.Model.Paths.PathItems.GetOrZero("/burgers/createBurger").Post.RequestBody.Content.GetOrZero("application/json").Schema |
| 672 |
|
| 673 |
// create a schema validator |
| 674 |
v := NewSchemaValidator() |
| 675 |
|
| 676 |
// validate! |
| 677 |
valid, errors := v.ValidateSchemaString(sch.Schema(), string(bodyBytes)) |
| 678 |
|
| 679 |
assert.True(t, valid) |
| 680 |
assert.Empty(t, errors) |
| 681 |
} |
| 682 |
|
| 683 |
func TestValidateSchema_NullableEnum(t *testing.T) { |
| 684 |
spec := `openapi: 3.0.0 |
| 685 |
paths: |
| 686 |
/burgers/createBurger: |
| 687 |
post: |
| 688 |
requestBody: |
| 689 |
content: |
| 690 |
application/json: |
| 691 |
schema: |
| 692 |
type: object |
| 693 |
required: [name] |
| 694 |
properties: |
| 695 |
name: |
| 696 |
type: string |
| 697 |
enum: [mcbird, mcbeef, veggie, null] |
| 698 |
nullable: true |
| 699 |
patties: |
| 700 |
type: integer |
| 701 |
vegetarian: |
| 702 |
type: boolean` |
| 703 |
|
| 704 |
doc, _ := libopenapi.NewDocument([]byte(spec)) |
| 705 |
|
| 706 |
m, errs := doc.BuildV3Model() |
| 707 |
assert.Empty(t, errs) |
| 708 |
|
| 709 |
body := map[string]interface{}{ |
| 710 |
"name": nil, |
| 711 |
"patties": 2, |
| 712 |
"vegetarian": true, |
| 713 |
} |
| 714 |
|
| 715 |
bodyBytes, _ := json.Marshal(body) |
| 716 |
sch := m.Model.Paths.PathItems.GetOrZero("/burgers/createBurger").Post.RequestBody.Content.GetOrZero("application/json").Schema |
| 717 |
|
| 718 |
// create a schema validator |
| 719 |
v := NewSchemaValidator() |
| 720 |
|
| 721 |
// validate with OpenAPI 3.0 version - should pass with nullable support! |
| 722 |
valid, errors := v.ValidateSchemaStringWithVersion(sch.Schema(), string(bodyBytes), 3.0) |
| 723 |
|
| 724 |
assert.True(t, valid) |
| 725 |
assert.Len(t, errors, 0) |
| 726 |
|
| 727 |
// validate with default behavior (3.1+) - should fail due to nullable keyword |
| 728 |
valid, errors = v.ValidateSchemaString(sch.Schema(), string(bodyBytes)) |
| 729 |
assert.False(t, valid) |
| 730 |
assert.NotEmpty(t, errors) |
| 731 |
} |
| 732 |
|
| 733 |
// https://github.com/pb33f/libopenapi/issues/415 |
| 734 |
func TestValidateSchema_v3_1_DependentSchemas(t *testing.T) { |
| 735 |
spec := `openapi: 3.1.0 |
| 736 |
paths: |
| 737 |
/burgers/createBurger: |
| 738 |
post: |
| 739 |
requestBody: |
| 740 |
content: |
| 741 |
application/json: |
| 742 |
schema: |
| 743 |
type: object |
| 744 |
properties: |
| 745 |
fishCake: |
| 746 |
type: object |
| 747 |
properties: |
| 748 |
bones: |
| 749 |
type: boolean |
| 750 |
dependentSchemas: |
| 751 |
fishCake: |
| 752 |
type: object |
| 753 |
properties: |
| 754 |
cream: |
| 755 |
type: number |
| 756 |
format: double |
| 757 |
required: |
| 758 |
- cream` |
| 759 |
|
| 760 |
doc, _ := libopenapi.NewDocument([]byte(spec)) |
| 761 |
|
| 762 |
m, _ := doc.BuildV3Model() |
| 763 |
|
| 764 |
exp := `{ |
| 765 |
"fishCake": { |
| 766 |
"bones": true, |
| 767 |
"cream": 2.5 |
| 768 |
} |
| 769 |
}` |
| 770 |
|
| 771 |
sch := m.Model.Paths.PathItems.GetOrZero("/burgers/createBurger").Post.RequestBody.Content.GetOrZero("application/json").Schema |
| 772 |
|
| 773 |
// create a schema validator |
| 774 |
v := NewSchemaValidator() |
| 775 |
|
| 776 |
// validate! |
| 777 |
valid, errors := v.ValidateSchemaString(sch.Schema(), exp) |
| 778 |
|
| 779 |
assert.True(t, valid) |
| 780 |
assert.Empty(t, errors) |
| 781 |
} |
| 782 |
|
| 783 |
// https://github.com/pb33f/libopenapi/issues/415 |
| 784 |
func TestValidateSchema_v3_1_DependentSchemas_NotValid(t *testing.T) { |
| 785 |
spec := `openapi: 3.1.0 |
| 786 |
paths: |
| 787 |
/burgers/createBurger: |
| 788 |
post: |
| 789 |
requestBody: |
| 790 |
content: |
| 791 |
application/json: |
| 792 |
schema: |
| 793 |
type: object |
| 794 |
properties: |
| 795 |
fishCake: |
| 796 |
type: object |
| 797 |
properties: |
| 798 |
bones: |
| 799 |
type: boolean |
| 800 |
dependentSchemas: |
| 801 |
fishCake: |
| 802 |
type: object |
| 803 |
properties: |
| 804 |
cream: |
| 805 |
type: number |
| 806 |
format: double |
| 807 |
required: |
| 808 |
- cream` |
| 809 |
|
| 810 |
doc, _ := libopenapi.NewDocument([]byte(spec)) |
| 811 |
|
| 812 |
m, _ := doc.BuildV3Model() |
| 813 |
|
| 814 |
exp := `{ |
| 815 |
"fishCake": { |
| 816 |
"bones": true, |
| 817 |
} |
| 818 |
"cream": 2.5 |
| 819 |
}` |
| 820 |
|
| 821 |
sch := m.Model.Paths.PathItems.GetOrZero("/burgers/createBurger").Post.RequestBody.Content.GetOrZero("application/json").Schema |
| 822 |
|
| 823 |
// create a schema validator |
| 824 |
v := NewSchemaValidator() |
| 825 |
|
| 826 |
// validate! |
| 827 |
valid, errors := v.ValidateSchemaString(sch.Schema(), exp) |
| 828 |
|
| 829 |
assert.False(t, valid) |
| 830 |
assert.NotEmpty(t, errors) |
| 831 |
} |
| 832 |
|
| 833 |
// Additional comprehensive tests for version-aware validation |
| 834 |
|
| 835 |
func TestSchemaValidator_ValidateSchemaStringWithVersion_NilSchema(t *testing.T) { |
| 836 |
v := NewSchemaValidator() |
| 837 |
|
| 838 |
valid, errors := v.ValidateSchemaStringWithVersion(nil, `{"test": "value"}`, 3.0) |
| 839 |
|
| 840 |
assert.False(t, valid, "Should fail with nil schema") |
| 841 |
assert.Empty(t, errors, "Should not return errors for nil schema") |
| 842 |
} |
| 843 |
|
| 844 |
func TestSchemaValidator_ValidateSchemaObjectWithVersion_NilSchema(t *testing.T) { |
| 845 |
v := NewSchemaValidator() |
| 846 |
|
| 847 |
testObj := map[string]interface{}{ |
| 848 |
"test": "value", |
| 849 |
} |
| 850 |
|
| 851 |
valid, errors := v.ValidateSchemaObjectWithVersion(nil, testObj, 3.0) |
| 852 |
|
| 853 |
assert.False(t, valid, "Should fail with nil schema") |
| 854 |
assert.Empty(t, errors, "Should not return errors for nil schema") |
| 855 |
} |
| 856 |
|
| 857 |
func TestSchemaValidator_ValidateSchemaBytesWithVersion_NilSchema(t *testing.T) { |
| 858 |
v := NewSchemaValidator() |
| 859 |
|
| 860 |
payload := []byte(`{"test": "value"}`) |
| 861 |
|
| 862 |
valid, errors := v.ValidateSchemaBytesWithVersion(nil, payload, 3.0) |
| 863 |
|
| 864 |
assert.False(t, valid, "Should fail with nil schema") |
| 865 |
assert.Empty(t, errors, "Should not return errors for nil schema") |
| 866 |
} |
| 867 |
|
| 868 |
// https://github.com/daveshanley/vacuum/issues/520 |
| 869 |
func TestValidateSchema_OneOf_MultipleMatches_Issue520(t *testing.T) { |
| 870 |
// This test reproduces the issue from vacuum #520 |
| 871 |
// The example matches BOTH oneOf alternatives which should fail validation |
| 872 |
// but the error details are not being populated correctly |
| 873 |
|
| 874 |
spec := `openapi: 3.1.0 |
| 875 |
info: |
| 876 |
title: Test |
| 877 |
version: 1.0.0 |
| 878 |
components: |
| 879 |
schemas: |
| 880 |
Test: |
| 881 |
type: object |
| 882 |
oneOf: |
| 883 |
- properties: |
| 884 |
pim: |
| 885 |
type: string |
| 886 |
- properties: |
| 887 |
pam: |
| 888 |
type: string |
| 889 |
` |
| 890 |
|
| 891 |
doc, err := libopenapi.NewDocument([]byte(spec)) |
| 892 |
assert.NoError(t, err) |
| 893 |
|
| 894 |
model, errs := doc.BuildV3Model() |
| 895 |
assert.Empty(t, errs) |
| 896 |
|
| 897 |
testSchema := model.Model.Components.Schemas.GetOrZero("Test").Schema() |
| 898 |
|
| 899 |
testData := map[string]interface{}{ |
| 900 |
"pam": "nop", |
| 901 |
} |
| 902 |
|
| 903 |
validator := NewSchemaValidator() |
| 904 |
valid, validationErrors := validator.ValidateSchemaObject(testSchema, testData) |
| 905 |
|
| 906 |
assert.False(t, valid, "validation should fail because example matches both oneOf alternatives") |
| 907 |
assert.NotEmpty(t, validationErrors, "validation errors should be present") |
| 908 |
|
| 909 |
if len(validationErrors) > 0 { |
| 910 |
|
| 911 |
assert.NotEmpty(t, validationErrors[0].SchemaValidationErrors, |
| 912 |
"SchemaValidationErrors should contain detailed error information about the oneOf violation") |
| 913 |
|
| 914 |
if len(validationErrors[0].SchemaValidationErrors) > 0 { |
| 915 |
firstError := validationErrors[0].SchemaValidationErrors[0] |
| 916 |
|
| 917 |
assert.Contains(t, firstError.Reason, "oneOf", |
| 918 |
"error should mention oneOf constraint violation") |
| 919 |
} |
| 920 |
} |
| 921 |
} |
| 922 |
|
| 923 |
// https://github.com/daveshanley/vacuum/issues/520 |
| 924 |
func TestValidateSchema_OneOf_Discriminant_Valid(t *testing.T) { |
| 925 |
spec := `openapi: 3.1.0 |
| 926 |
info: |
| 927 |
title: Test |
| 928 |
version: 1.0.0 |
| 929 |
components: |
| 930 |
schemas: |
| 931 |
Test: |
| 932 |
type: object |
| 933 |
oneOf: |
| 934 |
- properties: |
| 935 |
type: |
| 936 |
const: pim |
| 937 |
pim: |
| 938 |
type: string |
| 939 |
required: [type, pim] |
| 940 |
- properties: |
| 941 |
type: |
| 942 |
const: pam |
| 943 |
pam: |
| 944 |
type: string |
| 945 |
required: [type, pam]` |
| 946 |
|
| 947 |
doc, err := libopenapi.NewDocument([]byte(spec)) |
| 948 |
assert.NoError(t, err) |
| 949 |
|
| 950 |
model, errs := doc.BuildV3Model() |
| 951 |
assert.Empty(t, errs) |
| 952 |
|
| 953 |
testSchema := model.Model.Components.Schemas.GetOrZero("Test").Schema() |
| 954 |
|
| 955 |
testData := map[string]interface{}{ |
| 956 |
"type": "pam", |
| 957 |
"pam": "nop", |
| 958 |
} |
| 959 |
|
| 960 |
validator := NewSchemaValidator() |
| 961 |
valid, validationErrors := validator.ValidateSchemaObject(testSchema, testData) |
| 962 |
|
| 963 |
assert.True(t, valid, "validation should pass for discriminant oneOf") |
| 964 |
assert.Empty(t, validationErrors, "no validation errors should be present") |
| 965 |
} |
| 966 |
|
| 967 |
// https://github.com/daveshanley/vacuum/issues/520 |
| 968 |
func TestValidateSchema_OneOf_NoMatches(t *testing.T) { |
| 969 |
spec := `openapi: 3.1.0 |
| 970 |
info: |
| 971 |
title: Test |
| 972 |
version: 1.0.0 |
| 973 |
components: |
| 974 |
schemas: |
| 975 |
Test: |
| 976 |
type: object |
| 977 |
oneOf: |
| 978 |
- properties: |
| 979 |
foo: |
| 980 |
type: string |
| 981 |
required: [foo] |
| 982 |
- properties: |
| 983 |
bar: |
| 984 |
type: integer |
| 985 |
required: [bar]` |
| 986 |
|
| 987 |
doc, err := libopenapi.NewDocument([]byte(spec)) |
| 988 |
assert.NoError(t, err) |
| 989 |
|
| 990 |
model, errs := doc.BuildV3Model() |
| 991 |
assert.Empty(t, errs) |
| 992 |
|
| 993 |
testSchema := model.Model.Components.Schemas.GetOrZero("Test").Schema() |
| 994 |
|
| 995 |
testData := map[string]interface{}{ |
| 996 |
"baz": "invalid", |
| 997 |
} |
| 998 |
|
| 999 |
validator := NewSchemaValidator() |
| 1000 |
valid, validationErrors := validator.ValidateSchemaObject(testSchema, testData) |
| 1001 |
|
| 1002 |
assert.False(t, valid, "validation should fail because example matches no oneOf alternatives") |
| 1003 |
assert.NotEmpty(t, validationErrors, "validation errors should be present") |
| 1004 |
|
| 1005 |
if len(validationErrors) > 0 { |
| 1006 |
assert.NotEmpty(t, validationErrors[0].SchemaValidationErrors, |
| 1007 |
"SchemaValidationErrors should contain detailed error information") |
| 1008 |
} |
| 1009 |
} |
| 1010 |
|
| 1011 |
// https://github.com/daveshanley/vacuum/issues/520 |
| 1012 |
func TestValidateSchema_OneOf_SimpleTypes(t *testing.T) { |
| 1013 |
testCases := []struct { |
| 1014 |
name string |
| 1015 |
spec string |
| 1016 |
value interface{} |
| 1017 |
shouldPass bool |
| 1018 |
errorDetail string |
| 1019 |
}{ |
| 1020 |
{ |
| 1021 |
name: "valid string", |
| 1022 |
spec: `openapi: 3.1.0 |
| 1023 |
info: |
| 1024 |
title: Test |
| 1025 |
version: 1.0.0 |
| 1026 |
components: |
| 1027 |
schemas: |
| 1028 |
Test: |
| 1029 |
oneOf: |
| 1030 |
- type: string |
| 1031 |
- type: integer`, |
| 1032 |
value: "hello", |
| 1033 |
shouldPass: true, |
| 1034 |
}, |
| 1035 |
{ |
| 1036 |
name: "valid integer", |
| 1037 |
spec: `openapi: 3.1.0 |
| 1038 |
info: |
| 1039 |
title: Test |
| 1040 |
version: 1.0.0 |
| 1041 |
components: |
| 1042 |
schemas: |
| 1043 |
Test: |
| 1044 |
oneOf: |
| 1045 |
- type: string |
| 1046 |
- type: integer`, |
| 1047 |
value: 42, |
| 1048 |
shouldPass: true, |
| 1049 |
}, |
| 1050 |
{ |
| 1051 |
name: "invalid - matches both (ambiguous pattern)", |
| 1052 |
spec: `openapi: 3.1.0 |
| 1053 |
info: |
| 1054 |
title: Test |
| 1055 |
version: 1.0.0 |
| 1056 |
components: |
| 1057 |
schemas: |
| 1058 |
Test: |
| 1059 |
oneOf: |
| 1060 |
- type: string |
| 1061 |
- type: string |
| 1062 |
pattern: '^[0-9]+$'`, |
| 1063 |
value: "123", |
| 1064 |
shouldPass: false, |
| 1065 |
errorDetail: "oneOf", |
| 1066 |
}, |
| 1067 |
} |
| 1068 |
|
| 1069 |
for _, tc := range testCases { |
| 1070 |
t.Run(tc.name, func(t *testing.T) { |
| 1071 |
doc, err := libopenapi.NewDocument([]byte(tc.spec)) |
| 1072 |
assert.NoError(t, err) |
| 1073 |
|
| 1074 |
model, errs := doc.BuildV3Model() |
| 1075 |
assert.Empty(t, errs) |
| 1076 |
|
| 1077 |
testSchema := model.Model.Components.Schemas.GetOrZero("Test").Schema() |
| 1078 |
|
| 1079 |
validator := NewSchemaValidator() |
| 1080 |
valid, validationErrors := validator.ValidateSchemaObject(testSchema, tc.value) |
| 1081 |
|
| 1082 |
if tc.shouldPass { |
| 1083 |
assert.True(t, valid, "validation should pass") |
| 1084 |
assert.Empty(t, validationErrors, "no validation errors expected") |
| 1085 |
} else { |
| 1086 |
assert.False(t, valid, "validation should fail") |
| 1087 |
assert.NotEmpty(t, validationErrors, "validation errors should be present") |
| 1088 |
|
| 1089 |
if len(validationErrors) > 0 && tc.errorDetail != "" { |
| 1090 |
if len(validationErrors[0].SchemaValidationErrors) > 0 { |
| 1091 |
firstError := validationErrors[0].SchemaValidationErrors[0] |
| 1092 |
assert.Contains(t, firstError.Reason, tc.errorDetail, |
| 1093 |
"error should contain expected detail") |
| 1094 |
} |
| 1095 |
} |
| 1096 |
} |
| 1097 |
}) |
| 1098 |
} |
| 1099 |
} |
| 1100 |
|
| 1101 |
// TestValidateSchema_Discriminator_OneOf_WithRefs_Issue788 tests that validation works correctly |
| 1102 |
// when a schema has discriminator + oneOf with $ref to component schemas. |
| 1103 |
// This was a regression in vacuum v0.21.2+ where the validator would fail with |
| 1104 |
// "JSON schema compile failed: json-pointer not found" because discriminator refs |
| 1105 |
// were being preserved instead of inlined. |
| 1106 |
// https://github.com/daveshanley/vacuum/issues/788 |
| 1107 |
func TestValidateSchema_Discriminator_OneOf_WithRefs_Issue788(t *testing.T) { |
| 1108 |
spec := `openapi: 3.1.0 |
| 1109 |
info: |
| 1110 |
title: Test Discriminator OneOf With Refs |
| 1111 |
version: 1.0.0 |
| 1112 |
components: |
| 1113 |
schemas: |
| 1114 |
ProductWidget: |
| 1115 |
type: object |
| 1116 |
required: |
| 1117 |
- productName |
| 1118 |
- quantity |
| 1119 |
- color |
| 1120 |
properties: |
| 1121 |
productName: |
| 1122 |
type: string |
| 1123 |
enum: |
| 1124 |
- Widget |
| 1125 |
quantity: |
| 1126 |
type: integer |
| 1127 |
minimum: 1 |
| 1128 |
color: |
| 1129 |
type: string |
| 1130 |
enum: |
| 1131 |
- Red |
| 1132 |
- Blue |
| 1133 |
- Green |
| 1134 |
ProductGadget: |
| 1135 |
type: object |
| 1136 |
required: |
| 1137 |
- productName |
| 1138 |
- quantity |
| 1139 |
- size |
| 1140 |
properties: |
| 1141 |
productName: |
| 1142 |
type: string |
| 1143 |
enum: |
| 1144 |
- Gadget |
| 1145 |
quantity: |
| 1146 |
type: integer |
| 1147 |
minimum: 1 |
| 1148 |
size: |
| 1149 |
type: string |
| 1150 |
enum: |
| 1151 |
- Small |
| 1152 |
- Medium |
| 1153 |
- Large |
| 1154 |
Product: |
| 1155 |
oneOf: |
| 1156 |
- $ref: '#/components/schemas/ProductWidget' |
| 1157 |
- $ref: '#/components/schemas/ProductGadget' |
| 1158 |
discriminator: |
| 1159 |
propertyName: productName` |
| 1160 |
|
| 1161 |
doc, err := libopenapi.NewDocument([]byte(spec)) |
| 1162 |
assert.NoError(t, err) |
| 1163 |
|
| 1164 |
model, errs := doc.BuildV3Model() |
| 1165 |
assert.Empty(t, errs) |
| 1166 |
|
| 1167 |
productSchema := model.Model.Components.Schemas.GetOrZero("Product").Schema() |
| 1168 |
|
| 1169 |
// Valid Widget product |
| 1170 |
validWidget := map[string]interface{}{ |
| 1171 |
"productName": "Widget", |
| 1172 |
"quantity": 1, |
| 1173 |
"color": "Green", |
| 1174 |
} |
| 1175 |
|
| 1176 |
validator := NewSchemaValidator() |
| 1177 |
valid, validationErrors := validator.ValidateSchemaObject(productSchema, validWidget) |
| 1178 |
|
| 1179 |
// This should pass without "json-pointer not found" error |
| 1180 |
assert.True(t, valid, "validation should pass for valid product with discriminator oneOf refs") |
| 1181 |
assert.Empty(t, validationErrors, "no validation errors should be present") |
| 1182 |
} |
| 1183 |
|
| 1184 |
// TestValidateSchema_Discriminator_AnyOf_WithRefs tests anyOf with discriminator and $refs |
| 1185 |
func TestValidateSchema_Discriminator_AnyOf_WithRefs(t *testing.T) { |
| 1186 |
spec := `openapi: 3.1.0 |
| 1187 |
info: |
| 1188 |
title: Test Discriminator AnyOf With Refs |
| 1189 |
version: 1.0.0 |
| 1190 |
components: |
| 1191 |
schemas: |
| 1192 |
Cat: |
| 1193 |
type: object |
| 1194 |
required: |
| 1195 |
- petType |
| 1196 |
- meow |
| 1197 |
properties: |
| 1198 |
petType: |
| 1199 |
type: string |
| 1200 |
const: cat |
| 1201 |
meow: |
| 1202 |
type: boolean |
| 1203 |
Dog: |
| 1204 |
type: object |
| 1205 |
required: |
| 1206 |
- petType |
| 1207 |
- bark |
| 1208 |
properties: |
| 1209 |
petType: |
| 1210 |
type: string |
| 1211 |
const: dog |
| 1212 |
bark: |
| 1213 |
type: boolean |
| 1214 |
Pet: |
| 1215 |
anyOf: |
| 1216 |
- $ref: '#/components/schemas/Cat' |
| 1217 |
- $ref: '#/components/schemas/Dog' |
| 1218 |
discriminator: |
| 1219 |
propertyName: petType` |
| 1220 |
|
| 1221 |
doc, err := libopenapi.NewDocument([]byte(spec)) |
| 1222 |
assert.NoError(t, err) |
| 1223 |
|
| 1224 |
model, errs := doc.BuildV3Model() |
| 1225 |
assert.Empty(t, errs) |
| 1226 |
|
| 1227 |
petSchema := model.Model.Components.Schemas.GetOrZero("Pet").Schema() |
| 1228 |
|
| 1229 |
// Valid cat |
| 1230 |
validCat := map[string]interface{}{ |
| 1231 |
"petType": "cat", |
| 1232 |
"meow": true, |
| 1233 |
} |
| 1234 |
|
| 1235 |
validator := NewSchemaValidator() |
| 1236 |
valid, validationErrors := validator.ValidateSchemaObject(petSchema, validCat) |
| 1237 |
|
| 1238 |
assert.True(t, valid, "validation should pass for valid cat with discriminator anyOf refs") |
| 1239 |
assert.Empty(t, validationErrors, "no validation errors should be present") |
| 1240 |
} |
| 1241 |
|
| 1242 |
// TestValidateSchema_Discriminator_OneOf_WithRefs_InvalidData tests that invalid data |
| 1243 |
// still fails validation correctly (not a false negative) |
| 1244 |
func TestValidateSchema_Discriminator_OneOf_WithRefs_InvalidData(t *testing.T) { |
| 1245 |
spec := `openapi: 3.1.0 |
| 1246 |
info: |
| 1247 |
title: Test Discriminator OneOf Invalid |
| 1248 |
version: 1.0.0 |
| 1249 |
components: |
| 1250 |
schemas: |
| 1251 |
ProductWidget: |
| 1252 |
type: object |
| 1253 |
required: |
| 1254 |
- productName |
| 1255 |
- quantity |
| 1256 |
- color |
| 1257 |
properties: |
| 1258 |
productName: |
| 1259 |
type: string |
| 1260 |
enum: |
| 1261 |
- Widget |
| 1262 |
quantity: |
| 1263 |
type: integer |
| 1264 |
minimum: 1 |
| 1265 |
color: |
| 1266 |
type: string |
| 1267 |
enum: |
| 1268 |
- Red |
| 1269 |
- Blue |
| 1270 |
ProductGadget: |
| 1271 |
type: object |
| 1272 |
required: |
| 1273 |
- productName |
| 1274 |
- quantity |
| 1275 |
- size |
| 1276 |
properties: |
| 1277 |
productName: |
| 1278 |
type: string |
| 1279 |
enum: |
| 1280 |
- Gadget |
| 1281 |
quantity: |
| 1282 |
type: integer |
| 1283 |
minimum: 1 |
| 1284 |
size: |
| 1285 |
type: string |
| 1286 |
Product: |
| 1287 |
oneOf: |
| 1288 |
- $ref: '#/components/schemas/ProductWidget' |
| 1289 |
- $ref: '#/components/schemas/ProductGadget' |
| 1290 |
discriminator: |
| 1291 |
propertyName: productName` |
| 1292 |
|
| 1293 |
doc, err := libopenapi.NewDocument([]byte(spec)) |
| 1294 |
assert.NoError(t, err) |
| 1295 |
|
| 1296 |
model, errs := doc.BuildV3Model() |
| 1297 |
assert.Empty(t, errs) |
| 1298 |
|
| 1299 |
productSchema := model.Model.Components.Schemas.GetOrZero("Product").Schema() |
| 1300 |
|
| 1301 |
// Invalid product - missing required field 'color' for Widget |
| 1302 |
invalidProduct := map[string]interface{}{ |
| 1303 |
"productName": "Widget", |
| 1304 |
"quantity": 1, |
| 1305 |
// missing required 'color' field |
| 1306 |
} |
| 1307 |
|
| 1308 |
validator := NewSchemaValidator() |
| 1309 |
valid, validationErrors := validator.ValidateSchemaObject(productSchema, invalidProduct) |
| 1310 |
|
| 1311 |
// This should fail because 'color' is required for Widget |
| 1312 |
assert.False(t, valid, "validation should fail for invalid product") |
| 1313 |
assert.NotEmpty(t, validationErrors, "validation errors should be present") |
| 1314 |
} |