| 1 |
// Copyright 2025 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 |
|
| 13 |
"github.com/pb33f/libopenapi-validator/config" |
| 14 |
) |
| 15 |
|
| 16 |
func TestSchemaValidator_NullableKeyword_OpenAPI30_Success(t *testing.T) { |
| 17 |
spec := `openapi: 3.0.0 |
| 18 |
paths: |
| 19 |
/test: |
| 20 |
post: |
| 21 |
requestBody: |
| 22 |
content: |
| 23 |
application/json: |
| 24 |
schema: |
| 25 |
type: object |
| 26 |
required: [name] |
| 27 |
properties: |
| 28 |
name: |
| 29 |
type: string |
| 30 |
nullable: true` |
| 31 |
|
| 32 |
doc, _ := libopenapi.NewDocument([]byte(spec)) |
| 33 |
m, errs := doc.BuildV3Model() |
| 34 |
assert.Empty(t, errs) |
| 35 |
|
| 36 |
schema := m.Model.Paths.PathItems.GetOrZero("/test").Post.RequestBody.Content.GetOrZero("application/json").Schema |
| 37 |
|
| 38 |
body := map[string]interface{}{ |
| 39 |
"name": nil, // This should be valid with nullable: true |
| 40 |
} |
| 41 |
bodyBytes, _ := json.Marshal(body) |
| 42 |
|
| 43 |
// Test with version 3.0 - should pass |
| 44 |
v := NewSchemaValidator() |
| 45 |
valid, errors := v.ValidateSchemaStringWithVersion(schema.Schema(), string(bodyBytes), 3.0) |
| 46 |
assert.True(t, valid, "Validation should pass with nullable: true in OpenAPI 3.0") |
| 47 |
assert.Empty(t, errors, "Should have no validation errors") |
| 48 |
} |
| 49 |
|
| 50 |
func TestSchemaValidator_NullableKeyword_OpenAPI31_Fails(t *testing.T) { |
| 51 |
spec := `openapi: 3.0.0 |
| 52 |
paths: |
| 53 |
/test: |
| 54 |
post: |
| 55 |
requestBody: |
| 56 |
content: |
| 57 |
application/json: |
| 58 |
schema: |
| 59 |
type: object |
| 60 |
required: [name] |
| 61 |
properties: |
| 62 |
name: |
| 63 |
type: string |
| 64 |
nullable: true` |
| 65 |
|
| 66 |
doc, _ := libopenapi.NewDocument([]byte(spec)) |
| 67 |
m, errs := doc.BuildV3Model() |
| 68 |
assert.Empty(t, errs) |
| 69 |
|
| 70 |
schema := m.Model.Paths.PathItems.GetOrZero("/test").Post.RequestBody.Content.GetOrZero("application/json").Schema |
| 71 |
|
| 72 |
body := map[string]interface{}{ |
| 73 |
"name": nil, |
| 74 |
} |
| 75 |
bodyBytes, _ := json.Marshal(body) |
| 76 |
|
| 77 |
// Test with version 3.1 - should fail due to nullable keyword |
| 78 |
v := NewSchemaValidator() |
| 79 |
valid, errors := v.ValidateSchemaStringWithVersion(schema.Schema(), string(bodyBytes), 3.1) |
| 80 |
assert.False(t, valid, "Validation should fail with nullable keyword in OpenAPI 3.1") |
| 81 |
assert.NotEmpty(t, errors, "Should have validation errors") |
| 82 |
|
| 83 |
// Check that error mentions nullable keyword not allowed |
| 84 |
found := false |
| 85 |
for _, err := range errors { |
| 86 |
if err.Reason != "" && contains(err.Reason, "nullable") { |
| 87 |
found = true |
| 88 |
break |
| 89 |
} |
| 90 |
for _, schErr := range err.SchemaValidationErrors { |
| 91 |
if schErr.Reason != "" && contains(schErr.Reason, "nullable") { |
| 92 |
found = true |
| 93 |
break |
| 94 |
} |
| 95 |
} |
| 96 |
} |
| 97 |
assert.True(t, found, "Error should mention nullable keyword is not allowed") |
| 98 |
} |
| 99 |
|
| 100 |
func TestSchemaValidator_DefaultBehavior_RejectsNullable(t *testing.T) { |
| 101 |
spec := `openapi: 3.0.0 |
| 102 |
paths: |
| 103 |
/test: |
| 104 |
post: |
| 105 |
requestBody: |
| 106 |
content: |
| 107 |
application/json: |
| 108 |
schema: |
| 109 |
type: object |
| 110 |
properties: |
| 111 |
name: |
| 112 |
type: string |
| 113 |
nullable: true` |
| 114 |
|
| 115 |
doc, _ := libopenapi.NewDocument([]byte(spec)) |
| 116 |
m, errs := doc.BuildV3Model() |
| 117 |
assert.Empty(t, errs) |
| 118 |
|
| 119 |
schema := m.Model.Paths.PathItems.GetOrZero("/test").Post.RequestBody.Content.GetOrZero("application/json").Schema |
| 120 |
|
| 121 |
body := map[string]interface{}{ |
| 122 |
"name": nil, |
| 123 |
} |
| 124 |
bodyBytes, _ := json.Marshal(body) |
| 125 |
|
| 126 |
// Test default behavior (should be 3.1+ strict mode) - should fail |
| 127 |
v := NewSchemaValidator() |
| 128 |
valid, errors := v.ValidateSchemaString(schema.Schema(), string(bodyBytes)) |
| 129 |
assert.False(t, valid, "Default validation should fail with nullable keyword") |
| 130 |
assert.NotEmpty(t, errors, "Should have validation errors") |
| 131 |
} |
| 132 |
|
| 133 |
func TestSchemaValidator_OpenAPIModeDisabled(t *testing.T) { |
| 134 |
spec := `openapi: 3.0.0 |
| 135 |
paths: |
| 136 |
/test: |
| 137 |
post: |
| 138 |
requestBody: |
| 139 |
content: |
| 140 |
application/json: |
| 141 |
schema: |
| 142 |
type: object |
| 143 |
properties: |
| 144 |
name: |
| 145 |
type: string |
| 146 |
nullable: true` |
| 147 |
|
| 148 |
doc, _ := libopenapi.NewDocument([]byte(spec)) |
| 149 |
m, errs := doc.BuildV3Model() |
| 150 |
assert.Empty(t, errs) |
| 151 |
|
| 152 |
schema := m.Model.Paths.PathItems.GetOrZero("/test").Post.RequestBody.Content.GetOrZero("application/json").Schema |
| 153 |
|
| 154 |
body := map[string]interface{}{ |
| 155 |
"name": nil, |
| 156 |
} |
| 157 |
bodyBytes, _ := json.Marshal(body) |
| 158 |
|
| 159 |
// Test with OpenAPI mode disabled - should ignore nullable keyword entirely |
| 160 |
v := NewSchemaValidator(config.WithoutOpenAPIMode()) |
| 161 |
valid, errors := v.ValidateSchemaStringWithVersion(schema.Schema(), string(bodyBytes), 3.0) |
| 162 |
assert.False(t, valid, "Should fail without OpenAPI vocabulary (nullable ignored)") |
| 163 |
assert.NotEmpty(t, errors, "Should have validation errors (null vs string type)") |
| 164 |
} |
| 165 |
|
| 166 |
func TestSchemaValidator_DiscriminatorKeyword_Valid(t *testing.T) { |
| 167 |
spec := `openapi: 3.0.0 |
| 168 |
paths: |
| 169 |
/test: |
| 170 |
post: |
| 171 |
requestBody: |
| 172 |
content: |
| 173 |
application/json: |
| 174 |
schema: |
| 175 |
type: object |
| 176 |
discriminator: |
| 177 |
propertyName: type |
| 178 |
mapping: |
| 179 |
dog: "#/components/schemas/Dog" |
| 180 |
cat: "#/components/schemas/Cat"` |
| 181 |
|
| 182 |
doc, _ := libopenapi.NewDocument([]byte(spec)) |
| 183 |
m, errs := doc.BuildV3Model() |
| 184 |
assert.Empty(t, errs) |
| 185 |
|
| 186 |
schema := m.Model.Paths.PathItems.GetOrZero("/test").Post.RequestBody.Content.GetOrZero("application/json").Schema |
| 187 |
|
| 188 |
body := map[string]interface{}{ |
| 189 |
"type": "dog", |
| 190 |
"name": "Buddy", |
| 191 |
} |
| 192 |
bodyBytes, _ := json.Marshal(body) |
| 193 |
|
| 194 |
// Test with discriminator in OpenAPI 3.0 - should pass |
| 195 |
v := NewSchemaValidator() |
| 196 |
valid, errors := v.ValidateSchemaStringWithVersion(schema.Schema(), string(bodyBytes), 3.0) |
| 197 |
assert.True(t, valid, "Validation should pass with valid discriminator") |
| 198 |
assert.Empty(t, errors, "Should have no validation errors") |
| 199 |
} |
| 200 |
|
| 201 |
func TestSchemaValidator_MultipleOpenAPIKeywords(t *testing.T) { |
| 202 |
spec := `openapi: 3.0.0 |
| 203 |
paths: |
| 204 |
/test: |
| 205 |
post: |
| 206 |
requestBody: |
| 207 |
content: |
| 208 |
application/json: |
| 209 |
schema: |
| 210 |
type: object |
| 211 |
properties: |
| 212 |
name: |
| 213 |
type: string |
| 214 |
nullable: true |
| 215 |
example: "John Doe" |
| 216 |
deprecated: true` |
| 217 |
|
| 218 |
doc, _ := libopenapi.NewDocument([]byte(spec)) |
| 219 |
m, errs := doc.BuildV3Model() |
| 220 |
assert.Empty(t, errs) |
| 221 |
|
| 222 |
schema := m.Model.Paths.PathItems.GetOrZero("/test").Post.RequestBody.Content.GetOrZero("application/json").Schema |
| 223 |
|
| 224 |
body := map[string]interface{}{ |
| 225 |
"name": nil, |
| 226 |
} |
| 227 |
bodyBytes, _ := json.Marshal(body) |
| 228 |
|
| 229 |
// Test with multiple OpenAPI keywords in OpenAPI 3.0 - should pass |
| 230 |
v := NewSchemaValidator() |
| 231 |
valid, errors := v.ValidateSchemaStringWithVersion(schema.Schema(), string(bodyBytes), 3.0) |
| 232 |
assert.True(t, valid, "Validation should pass with multiple OpenAPI keywords") |
| 233 |
assert.Empty(t, errors, "Should have no validation errors") |
| 234 |
} |
| 235 |
|
| 236 |
func TestSchemaValidator_NullableEnum_OriginalCommentedTest(t *testing.T) { |
| 237 |
// This is the original test case that was commented out |
| 238 |
spec := `openapi: 3.0.0 |
| 239 |
paths: |
| 240 |
/burgers/createBurger: |
| 241 |
post: |
| 242 |
requestBody: |
| 243 |
content: |
| 244 |
application/json: |
| 245 |
schema: |
| 246 |
type: object |
| 247 |
required: [name] |
| 248 |
properties: |
| 249 |
name: |
| 250 |
type: string |
| 251 |
enum: [mcbird, mcbeef, veggie, null] |
| 252 |
nullable: true |
| 253 |
patties: |
| 254 |
type: integer |
| 255 |
vegetarian: |
| 256 |
type: boolean` |
| 257 |
|
| 258 |
doc, _ := libopenapi.NewDocument([]byte(spec)) |
| 259 |
m, errs := doc.BuildV3Model() |
| 260 |
assert.Empty(t, errs) |
| 261 |
|
| 262 |
body := map[string]interface{}{ |
| 263 |
"name": nil, |
| 264 |
"patties": 2, |
| 265 |
"vegetarian": true, |
| 266 |
} |
| 267 |
|
| 268 |
bodyBytes, _ := json.Marshal(body) |
| 269 |
sch := m.Model.Paths.PathItems.GetOrZero("/burgers/createBurger").Post.RequestBody.Content.GetOrZero("application/json").Schema |
| 270 |
|
| 271 |
// create a schema validator |
| 272 |
v := NewSchemaValidator() |
| 273 |
|
| 274 |
// validate with OpenAPI 3.0 version - should now pass! |
| 275 |
valid, errors := v.ValidateSchemaStringWithVersion(sch.Schema(), string(bodyBytes), 3.0) |
| 276 |
|
| 277 |
assert.True(t, valid, "Should pass with nullable enum in OpenAPI 3.0") |
| 278 |
assert.Empty(t, errors, "Should have no validation errors") |
| 279 |
} |
| 280 |
|
| 281 |
func TestValidateSchema_CircularReference(t *testing.T) { |
| 282 |
spec := `openapi: "3.1.0" |
| 283 |
info: |
| 284 |
title: Test |
| 285 |
version: "1" |
| 286 |
paths: |
| 287 |
/: |
| 288 |
post: |
| 289 |
operationId: op |
| 290 |
requestBody: |
| 291 |
content: |
| 292 |
application/json: |
| 293 |
schema: |
| 294 |
$ref: '#/components/schemas/c' |
| 295 |
components: |
| 296 |
schemas: |
| 297 |
a: |
| 298 |
type: "string" |
| 299 |
examples: |
| 300 |
- '' |
| 301 |
b: |
| 302 |
type: "object" |
| 303 |
examples: |
| 304 |
- { "z": "" } |
| 305 |
properties: |
| 306 |
z: |
| 307 |
"$ref": '#/components/schemas/a' |
| 308 |
b: |
| 309 |
"$ref": '#/components/schemas/b' |
| 310 |
c: |
| 311 |
type: "object" |
| 312 |
examples: |
| 313 |
- { "b": { "z": "" } } |
| 314 |
properties: |
| 315 |
"b": |
| 316 |
"$ref": '#/components/schemas/b'` |
| 317 |
|
| 318 |
doc, err := libopenapi.NewDocument([]byte(spec)) |
| 319 |
assert.NoError(t, err) |
| 320 |
|
| 321 |
model, errs := doc.BuildV3Model() |
| 322 |
assert.Nil(t, errs) |
| 323 |
|
| 324 |
schema := model.Model.Paths.PathItems.GetOrZero("/").Post.RequestBody.Content.GetOrZero("application/json").Schema |
| 325 |
assert.NotNil(t, schema) |
| 326 |
assert.NotNil(t, schema.Schema()) |
| 327 |
|
| 328 |
t.Run("should fail rendering", func(t *testing.T) { |
| 329 |
_, err := schema.Schema().RenderInline() |
| 330 |
assert.Error(t, err, "RenderInline should not error on circular refs") |
| 331 |
}) |
| 332 |
|
| 333 |
t.Run("should fail validating", func(t *testing.T) { |
| 334 |
sv := NewSchemaValidator() |
| 335 |
|
| 336 |
schemaB := model.Model.Components.Schemas.GetOrZero("b").Schema() |
| 337 |
|
| 338 |
assert.NotNil(t, schemaB) |
| 339 |
assert.NotNil(t, schemaB.Examples) |
| 340 |
|
| 341 |
exampleJSON := `{"z": "", "b": {"z": ""}}` |
| 342 |
valid, errors := sv.ValidateSchemaString(schemaB, exampleJSON) |
| 343 |
|
| 344 |
assert.False(t, valid, "Schema with circular refs should currently fail validation") |
| 345 |
assert.NotNil(t, errors, "Should have validation errors") |
| 346 |
|
| 347 |
foundCompilationError := false |
| 348 |
for _, err := range errors { |
| 349 |
if err.Message == "schema does not pass validation" && |
| 350 |
err.Reason != "" && |
| 351 |
(err.Reason == "The schema cannot be decoded: schema render failure, circular reference: `#/components/schemas/b`" || |
| 352 |
err.Reason == "The schema cannot be decoded: schema render failure, circular reference: `#/components/schemas/Node`") { |
| 353 |
foundCompilationError = true |
| 354 |
} |
| 355 |
assert.Nil(t, err.SchemaValidationErrors, "Rendering errors should not have SchemaValidationErrors") |
| 356 |
} |
| 357 |
assert.True(t, foundCompilationError, "Should have schema compilation error for circular references") |
| 358 |
}) |
| 359 |
} |
| 360 |
|
| 361 |
func TestValidateSchema_SimpleCircularReference(t *testing.T) { |
| 362 |
// Even simpler test case |
| 363 |
spec := `openapi: "3.1.0" |
| 364 |
info: |
| 365 |
title: Test |
| 366 |
version: "1" |
| 367 |
paths: |
| 368 |
/test: |
| 369 |
get: |
| 370 |
responses: |
| 371 |
'200': |
| 372 |
description: OK |
| 373 |
content: |
| 374 |
application/json: |
| 375 |
schema: |
| 376 |
$ref: '#/components/schemas/Node' |
| 377 |
components: |
| 378 |
schemas: |
| 379 |
Node: |
| 380 |
type: object |
| 381 |
properties: |
| 382 |
value: |
| 383 |
type: string |
| 384 |
next: |
| 385 |
$ref: '#/components/schemas/Node' |
| 386 |
examples: |
| 387 |
- value: "test" |
| 388 |
next: |
| 389 |
value: "nested"` |
| 390 |
|
| 391 |
doc, err := libopenapi.NewDocument([]byte(spec)) |
| 392 |
assert.NoError(t, err) |
| 393 |
|
| 394 |
model, errs := doc.BuildV3Model() |
| 395 |
assert.Nil(t, errs) |
| 396 |
|
| 397 |
schema := model.Model.Paths.PathItems.GetOrZero("/test").Get.Responses.Codes.GetOrZero("200").Content.GetOrZero("application/json").Schema |
| 398 |
assert.NotNil(t, schema) |
| 399 |
assert.NotNil(t, schema.Schema()) |
| 400 |
|
| 401 |
// Try to render inline |
| 402 |
rendered, err := schema.Schema().RenderInline() |
| 403 |
if err != nil { |
| 404 |
t.Logf("RenderInline error on simple circular ref: %v", err) |
| 405 |
} else { |
| 406 |
t.Logf("RenderInline succeeded for simple circular ref, rendered %d bytes", len(rendered)) |
| 407 |
} |
| 408 |
|
| 409 |
// Validate using schema validator |
| 410 |
sv := NewSchemaValidator() |
| 411 |
nodeSchema := model.Model.Components.Schemas.GetOrZero("Node").Schema() |
| 412 |
|
| 413 |
// Try to validate an example against the schema |
| 414 |
exampleJSON := `{"value": "test", "next": {"value": "nested"}}` |
| 415 |
valid, errors := sv.ValidateSchemaString(nodeSchema, exampleJSON) |
| 416 |
|
| 417 |
t.Logf("Simple circular ref - Schema validation valid: %v", valid) |
| 418 |
for _, err := range errors { |
| 419 |
t.Logf("Error: %s", err.Error()) |
| 420 |
} |
| 421 |
} |
| 422 |
|
| 423 |
// Helper function to check if a string contains a substring (case-insensitive) |
| 424 |
func contains(s, substr string) bool { |
| 425 |
return len(s) >= len(substr) && (s == substr || len(substr) == 0 || |
| 426 |
(len(s) > len(substr) && (s[:len(substr)] == substr || s[len(s)-len(substr):] == substr || |
| 427 |
func() bool { |
| 428 |
for i := 1; i <= len(s)-len(substr); i++ { |
| 429 |
if s[i:i+len(substr)] == substr { |
| 430 |
return true |
| 431 |
} |
| 432 |
} |
| 433 |
return false |
| 434 |
}()))) |
| 435 |
} |