| 1 |
// Copyright 2025 Princess B33f Heavy Industries / Dave Shanley |
| 2 |
// SPDX-License-Identifier: MIT |
| 3 |
|
| 4 |
package openapi_vocabulary |
| 5 |
|
| 6 |
import ( |
| 7 |
"strings" |
| 8 |
"testing" |
| 9 |
|
| 10 |
"github.com/santhosh-tekuri/jsonschema/v6" |
| 11 |
"github.com/stretchr/testify/assert" |
| 12 |
"golang.org/x/text/message" |
| 13 |
) |
| 14 |
|
| 15 |
func TestNullableKeyword_Version30_Compilation(t *testing.T) { |
| 16 |
// Test that nullable: true compiles successfully in OpenAPI 3.0 |
| 17 |
schemaJSON := `{ |
| 18 |
"type": "string", |
| 19 |
"nullable": true |
| 20 |
}` |
| 21 |
|
| 22 |
schema, err := jsonschema.UnmarshalJSON(strings.NewReader(schemaJSON)) |
| 23 |
assert.NoError(t, err) |
| 24 |
|
| 25 |
compiler := jsonschema.NewCompiler() |
| 26 |
compiler.RegisterVocabulary(NewOpenAPIVocabulary(Version30)) |
| 27 |
compiler.AssertVocabs() |
| 28 |
|
| 29 |
err = compiler.AddResource("test.json", schema) |
| 30 |
assert.NoError(t, err) |
| 31 |
|
| 32 |
// Should compile successfully in 3.0 (validation behavior handled by transformation) |
| 33 |
compiledSchema, err := compiler.Compile("test.json") |
| 34 |
assert.NoError(t, err) |
| 35 |
assert.NotNil(t, compiledSchema) |
| 36 |
} |
| 37 |
|
| 38 |
func TestNullableKeyword_Version30_WithoutNullable(t *testing.T) { |
| 39 |
// Test that without nullable: true, null values are rejected |
| 40 |
schemaJSON := `{ |
| 41 |
"type": "string" |
| 42 |
}` |
| 43 |
|
| 44 |
schema, err := jsonschema.UnmarshalJSON(strings.NewReader(schemaJSON)) |
| 45 |
assert.NoError(t, err) |
| 46 |
|
| 47 |
compiler := jsonschema.NewCompiler() |
| 48 |
compiler.RegisterVocabulary(NewOpenAPIVocabulary(Version30)) |
| 49 |
compiler.AssertVocabs() |
| 50 |
|
| 51 |
err = compiler.AddResource("test.json", schema) |
| 52 |
assert.NoError(t, err) |
| 53 |
|
| 54 |
compiledSchema, err := compiler.Compile("test.json") |
| 55 |
assert.NoError(t, err) |
| 56 |
|
| 57 |
// Test null value - should fail without nullable: true |
| 58 |
err = compiledSchema.Validate(nil) |
| 59 |
assert.Error(t, err) |
| 60 |
assert.Contains(t, err.Error(), "got null, want string") |
| 61 |
|
| 62 |
// Test string value - should pass |
| 63 |
err = compiledSchema.Validate("hello") |
| 64 |
assert.NoError(t, err) |
| 65 |
} |
| 66 |
|
| 67 |
func TestNullableKeyword_Version31_Rejected(t *testing.T) { |
| 68 |
// Test that nullable keyword is rejected in OpenAPI 3.1 |
| 69 |
schemaJSON := `{ |
| 70 |
"type": "string", |
| 71 |
"nullable": true |
| 72 |
}` |
| 73 |
|
| 74 |
schema, err := jsonschema.UnmarshalJSON(strings.NewReader(schemaJSON)) |
| 75 |
assert.NoError(t, err) |
| 76 |
|
| 77 |
compiler := jsonschema.NewCompiler() |
| 78 |
compiler.RegisterVocabulary(NewOpenAPIVocabulary(Version31)) |
| 79 |
compiler.AssertVocabs() |
| 80 |
|
| 81 |
err = compiler.AddResource("test.json", schema) |
| 82 |
assert.NoError(t, err) |
| 83 |
|
| 84 |
// Compilation should fail due to nullable in 3.1 |
| 85 |
_, err = compiler.Compile("test.json") |
| 86 |
assert.Error(t, err) |
| 87 |
assert.Contains(t, err.Error(), "The `nullable` keyword is not supported in OpenAPI 3.1+") |
| 88 |
} |
| 89 |
|
| 90 |
func TestNullableKeyword_InvalidType(t *testing.T) { |
| 91 |
// Test that nullable must be a boolean |
| 92 |
schemaJSON := `{ |
| 93 |
"type": "string", |
| 94 |
"nullable": "yes" |
| 95 |
}` |
| 96 |
|
| 97 |
schema, err := jsonschema.UnmarshalJSON(strings.NewReader(schemaJSON)) |
| 98 |
assert.NoError(t, err) |
| 99 |
|
| 100 |
compiler := jsonschema.NewCompiler() |
| 101 |
compiler.RegisterVocabulary(NewOpenAPIVocabulary(Version30)) |
| 102 |
compiler.AssertVocabs() |
| 103 |
|
| 104 |
err = compiler.AddResource("test.json", schema) |
| 105 |
assert.NoError(t, err) |
| 106 |
|
| 107 |
// Compilation should fail due to invalid nullable type |
| 108 |
_, err = compiler.Compile("test.json") |
| 109 |
assert.Error(t, err) |
| 110 |
assert.Contains(t, err.Error(), "nullable must be a boolean value") |
| 111 |
} |
| 112 |
|
| 113 |
func TestDiscriminatorKeyword_ValidStructure(t *testing.T) { |
| 114 |
// Test discriminator with valid structure |
| 115 |
schemaJSON := `{ |
| 116 |
"type": "object", |
| 117 |
"discriminator": { |
| 118 |
"propertyName": "type", |
| 119 |
"mapping": { |
| 120 |
"dog": "#/components/schemas/Dog", |
| 121 |
"cat": "#/components/schemas/Cat" |
| 122 |
} |
| 123 |
} |
| 124 |
}` |
| 125 |
|
| 126 |
schema, err := jsonschema.UnmarshalJSON(strings.NewReader(schemaJSON)) |
| 127 |
assert.NoError(t, err) |
| 128 |
|
| 129 |
compiler := jsonschema.NewCompiler() |
| 130 |
compiler.RegisterVocabulary(NewOpenAPIVocabulary(Version30)) |
| 131 |
compiler.AssertVocabs() |
| 132 |
|
| 133 |
err = compiler.AddResource("test.json", schema) |
| 134 |
assert.NoError(t, err) |
| 135 |
|
| 136 |
compiledSchema, err := compiler.Compile("test.json") |
| 137 |
assert.NoError(t, err) |
| 138 |
|
| 139 |
// Test object with discriminator property |
| 140 |
testData := map[string]any{ |
| 141 |
"type": "dog", |
| 142 |
"name": "Buddy", |
| 143 |
} |
| 144 |
err = compiledSchema.Validate(testData) |
| 145 |
assert.NoError(t, err) |
| 146 |
} |
| 147 |
|
| 148 |
func TestDiscriminatorKeyword_MissingPropertyName(t *testing.T) { |
| 149 |
// Test discriminator without propertyName |
| 150 |
schemaJSON := `{ |
| 151 |
"type": "object", |
| 152 |
"discriminator": { |
| 153 |
"mapping": { |
| 154 |
"dog": "#/components/schemas/Dog" |
| 155 |
} |
| 156 |
} |
| 157 |
}` |
| 158 |
|
| 159 |
schema, err := jsonschema.UnmarshalJSON(strings.NewReader(schemaJSON)) |
| 160 |
assert.NoError(t, err) |
| 161 |
|
| 162 |
compiler := jsonschema.NewCompiler() |
| 163 |
compiler.RegisterVocabulary(NewOpenAPIVocabulary(Version30)) |
| 164 |
compiler.AssertVocabs() |
| 165 |
|
| 166 |
err = compiler.AddResource("test.json", schema) |
| 167 |
assert.NoError(t, err) |
| 168 |
|
| 169 |
// Compilation should fail due to missing propertyName |
| 170 |
_, err = compiler.Compile("test.json") |
| 171 |
assert.Error(t, err) |
| 172 |
assert.Contains(t, err.Error(), "discriminator must have a propertyName field") |
| 173 |
} |
| 174 |
|
| 175 |
func TestExampleKeyword_Valid(t *testing.T) { |
| 176 |
// Test example keyword with any value |
| 177 |
schemaJSON := `{ |
| 178 |
"type": "string", |
| 179 |
"example": "hello world" |
| 180 |
}` |
| 181 |
|
| 182 |
schema, err := jsonschema.UnmarshalJSON(strings.NewReader(schemaJSON)) |
| 183 |
assert.NoError(t, err) |
| 184 |
|
| 185 |
compiler := jsonschema.NewCompiler() |
| 186 |
compiler.RegisterVocabulary(NewOpenAPIVocabulary(Version30)) |
| 187 |
compiler.AssertVocabs() |
| 188 |
|
| 189 |
err = compiler.AddResource("test.json", schema) |
| 190 |
assert.NoError(t, err) |
| 191 |
|
| 192 |
compiledSchema, err := compiler.Compile("test.json") |
| 193 |
assert.NoError(t, err) |
| 194 |
assert.NotNil(t, compiledSchema) |
| 195 |
} |
| 196 |
|
| 197 |
func TestDeprecatedKeyword_Valid(t *testing.T) { |
| 198 |
// Test deprecated keyword with boolean value |
| 199 |
schemaJSON := `{ |
| 200 |
"type": "string", |
| 201 |
"deprecated": true |
| 202 |
}` |
| 203 |
|
| 204 |
schema, err := jsonschema.UnmarshalJSON(strings.NewReader(schemaJSON)) |
| 205 |
assert.NoError(t, err) |
| 206 |
|
| 207 |
compiler := jsonschema.NewCompiler() |
| 208 |
compiler.RegisterVocabulary(NewOpenAPIVocabulary(Version30)) |
| 209 |
compiler.AssertVocabs() |
| 210 |
|
| 211 |
err = compiler.AddResource("test.json", schema) |
| 212 |
assert.NoError(t, err) |
| 213 |
|
| 214 |
compiledSchema, err := compiler.Compile("test.json") |
| 215 |
assert.NoError(t, err) |
| 216 |
assert.NotNil(t, compiledSchema) |
| 217 |
} |
| 218 |
|
| 219 |
func TestDeprecatedKeyword_InvalidType(t *testing.T) { |
| 220 |
// Test deprecated keyword with non-boolean value |
| 221 |
schemaJSON := `{ |
| 222 |
"type": "string", |
| 223 |
"deprecated": "yes" |
| 224 |
}` |
| 225 |
|
| 226 |
schema, err := jsonschema.UnmarshalJSON(strings.NewReader(schemaJSON)) |
| 227 |
assert.NoError(t, err) |
| 228 |
|
| 229 |
compiler := jsonschema.NewCompiler() |
| 230 |
compiler.RegisterVocabulary(NewOpenAPIVocabulary(Version30)) |
| 231 |
compiler.AssertVocabs() |
| 232 |
|
| 233 |
err = compiler.AddResource("test.json", schema) |
| 234 |
assert.NoError(t, err) |
| 235 |
|
| 236 |
// Compilation should fail due to invalid deprecated type |
| 237 |
_, err = compiler.Compile("test.json") |
| 238 |
assert.Error(t, err) |
| 239 |
assert.Contains(t, err.Error(), "deprecated must be a boolean value") |
| 240 |
} |
| 241 |
|
| 242 |
func TestMultipleKeywords_Combined(t *testing.T) { |
| 243 |
// Test multiple OpenAPI keywords compile successfully in the same schema |
| 244 |
schemaJSON := `{ |
| 245 |
"type": "string", |
| 246 |
"nullable": true, |
| 247 |
"example": "test value", |
| 248 |
"deprecated": false |
| 249 |
}` |
| 250 |
|
| 251 |
schema, err := jsonschema.UnmarshalJSON(strings.NewReader(schemaJSON)) |
| 252 |
assert.NoError(t, err) |
| 253 |
|
| 254 |
compiler := jsonschema.NewCompiler() |
| 255 |
compiler.RegisterVocabulary(NewOpenAPIVocabulary(Version30)) |
| 256 |
compiler.AssertVocabs() |
| 257 |
|
| 258 |
err = compiler.AddResource("test.json", schema) |
| 259 |
assert.NoError(t, err) |
| 260 |
|
| 261 |
// Should compile successfully in 3.0 (actual nullable behavior handled by transformation) |
| 262 |
compiledSchema, err := compiler.Compile("test.json") |
| 263 |
assert.NoError(t, err) |
| 264 |
assert.NotNil(t, compiledSchema) |
| 265 |
} |
| 266 |
|
| 267 |
func TestNoOpenAPIKeywords_NoExtension(t *testing.T) { |
| 268 |
// Test that schemas without OpenAPI keywords don't get extensions |
| 269 |
schemaJSON := `{ |
| 270 |
"type": "string", |
| 271 |
"minLength": 1 |
| 272 |
}` |
| 273 |
|
| 274 |
schema, err := jsonschema.UnmarshalJSON(strings.NewReader(schemaJSON)) |
| 275 |
assert.NoError(t, err) |
| 276 |
|
| 277 |
compiler := jsonschema.NewCompiler() |
| 278 |
compiler.RegisterVocabulary(NewOpenAPIVocabulary(Version30)) |
| 279 |
compiler.AssertVocabs() |
| 280 |
|
| 281 |
err = compiler.AddResource("test.json", schema) |
| 282 |
assert.NoError(t, err) |
| 283 |
|
| 284 |
compiledSchema, err := compiler.Compile("test.json") |
| 285 |
assert.NoError(t, err) |
| 286 |
|
| 287 |
// Should compile successfully even without OpenAPI keywords |
| 288 |
err = compiledSchema.Validate("hello") |
| 289 |
assert.NoError(t, err) |
| 290 |
|
| 291 |
err = compiledSchema.Validate("") |
| 292 |
assert.Error(t, err) // Should fail minLength validation |
| 293 |
} |
| 294 |
|
| 295 |
// Additional comprehensive tests |
| 296 |
|
| 297 |
func TestNewOpenAPIVocabulary_Version30(t *testing.T) { |
| 298 |
vocab := NewOpenAPIVocabulary(Version30) |
| 299 |
assert.NotNil(t, vocab) |
| 300 |
assert.Equal(t, OpenAPIVocabularyURL, vocab.URL) |
| 301 |
assert.Nil(t, vocab.Schema) |
| 302 |
assert.NotNil(t, vocab.Compile) |
| 303 |
} |
| 304 |
|
| 305 |
func TestNewOpenAPIVocabulary_Version31(t *testing.T) { |
| 306 |
vocab := NewOpenAPIVocabulary(Version31) |
| 307 |
assert.NotNil(t, vocab) |
| 308 |
assert.Equal(t, OpenAPIVocabularyURL, vocab.URL) |
| 309 |
assert.Nil(t, vocab.Schema) |
| 310 |
assert.NotNil(t, vocab.Compile) |
| 311 |
} |
| 312 |
|
| 313 |
func TestNewOpenAPIVocabularyWithCoercion_Enabled(t *testing.T) { |
| 314 |
vocab := NewOpenAPIVocabularyWithCoercion(Version30, true) |
| 315 |
assert.NotNil(t, vocab) |
| 316 |
assert.Equal(t, OpenAPIVocabularyURL, vocab.URL) |
| 317 |
assert.Nil(t, vocab.Schema) |
| 318 |
assert.NotNil(t, vocab.Compile) |
| 319 |
} |
| 320 |
|
| 321 |
func TestNewOpenAPIVocabularyWithCoercion_Disabled(t *testing.T) { |
| 322 |
vocab := NewOpenAPIVocabularyWithCoercion(Version30, false) |
| 323 |
assert.NotNil(t, vocab) |
| 324 |
assert.Equal(t, OpenAPIVocabularyURL, vocab.URL) |
| 325 |
assert.Nil(t, vocab.Schema) |
| 326 |
assert.NotNil(t, vocab.Compile) |
| 327 |
} |
| 328 |
|
| 329 |
func TestCompileOpenAPIKeywords_EmptySchema(t *testing.T) { |
| 330 |
obj := map[string]any{} |
| 331 |
|
| 332 |
ext, err := compileOpenAPIKeywords(nil, obj, Version30, false) |
| 333 |
assert.NoError(t, err) |
| 334 |
assert.Nil(t, ext) |
| 335 |
} |
| 336 |
|
| 337 |
func TestCompileOpenAPIKeywords_NoOpenAPIKeywords(t *testing.T) { |
| 338 |
obj := map[string]any{ |
| 339 |
"type": "string", |
| 340 |
"minLength": 1, |
| 341 |
} |
| 342 |
|
| 343 |
ext, err := compileOpenAPIKeywords(nil, obj, Version30, false) |
| 344 |
assert.NoError(t, err) |
| 345 |
assert.Nil(t, ext) |
| 346 |
} |
| 347 |
|
| 348 |
func TestCompileOpenAPIKeywords_NullableOnly(t *testing.T) { |
| 349 |
obj := map[string]any{ |
| 350 |
"type": "string", |
| 351 |
"nullable": true, |
| 352 |
} |
| 353 |
|
| 354 |
ext, err := compileOpenAPIKeywords(nil, obj, Version30, false) |
| 355 |
assert.NoError(t, err) |
| 356 |
// Nullable compilation returns nil because the actual transformation is handled elsewhere |
| 357 |
// The vocabulary just validates that the keyword is not used in 3.1+ |
| 358 |
assert.Nil(t, ext) |
| 359 |
} |
| 360 |
|
| 361 |
func TestCompileOpenAPIKeywords_DiscriminatorOnly(t *testing.T) { |
| 362 |
obj := map[string]any{ |
| 363 |
"type": "object", |
| 364 |
"discriminator": map[string]any{ |
| 365 |
"propertyName": "type", |
| 366 |
}, |
| 367 |
} |
| 368 |
|
| 369 |
ext, err := compileOpenAPIKeywords(nil, obj, Version30, false) |
| 370 |
assert.NoError(t, err) |
| 371 |
assert.NotNil(t, ext) |
| 372 |
} |
| 373 |
|
| 374 |
func TestCompileOpenAPIKeywords_ExampleOnly(t *testing.T) { |
| 375 |
obj := map[string]any{ |
| 376 |
"type": "string", |
| 377 |
"example": "test", |
| 378 |
} |
| 379 |
|
| 380 |
ext, err := compileOpenAPIKeywords(nil, obj, Version30, false) |
| 381 |
assert.NoError(t, err) |
| 382 |
assert.NotNil(t, ext) |
| 383 |
} |
| 384 |
|
| 385 |
func TestCompileOpenAPIKeywords_DeprecatedOnly(t *testing.T) { |
| 386 |
obj := map[string]any{ |
| 387 |
"type": "string", |
| 388 |
"deprecated": true, |
| 389 |
} |
| 390 |
|
| 391 |
ext, err := compileOpenAPIKeywords(nil, obj, Version30, false) |
| 392 |
assert.NoError(t, err) |
| 393 |
assert.NotNil(t, ext) |
| 394 |
} |
| 395 |
|
| 396 |
func TestCompileOpenAPIKeywords_CoercionOnly(t *testing.T) { |
| 397 |
obj := map[string]any{ |
| 398 |
"type": "boolean", |
| 399 |
} |
| 400 |
|
| 401 |
ext, err := compileOpenAPIKeywords(nil, obj, Version30, true) |
| 402 |
assert.NoError(t, err) |
| 403 |
assert.NotNil(t, ext) |
| 404 |
} |
| 405 |
|
| 406 |
func TestCompileOpenAPIKeywords_AllKeywordsCombined(t *testing.T) { |
| 407 |
obj := map[string]any{ |
| 408 |
"type": "string", |
| 409 |
"nullable": true, |
| 410 |
"discriminator": map[string]any{ |
| 411 |
"propertyName": "type", |
| 412 |
}, |
| 413 |
"example": "test", |
| 414 |
"deprecated": false, |
| 415 |
} |
| 416 |
|
| 417 |
ext, err := compileOpenAPIKeywords(nil, obj, Version30, true) |
| 418 |
assert.NoError(t, err) |
| 419 |
assert.NotNil(t, ext) |
| 420 |
} |
| 421 |
|
| 422 |
// Error type tests |
| 423 |
func TestOpenAPIKeywordError_Error(t *testing.T) { |
| 424 |
err := &OpenAPIKeywordError{ |
| 425 |
Keyword: "nullable", |
| 426 |
Message: "test message", |
| 427 |
} |
| 428 |
|
| 429 |
expected := "OpenAPI keyword 'nullable': test message" |
| 430 |
assert.Equal(t, expected, err.Error()) |
| 431 |
} |
| 432 |
|
| 433 |
func TestDiscriminatorPropertyMissingError_KeywordPath(t *testing.T) { |
| 434 |
err := &DiscriminatorPropertyMissingError{ |
| 435 |
PropertyName: "type", |
| 436 |
} |
| 437 |
|
| 438 |
path := err.KeywordPath() |
| 439 |
assert.Equal(t, []string{"discriminator"}, path) |
| 440 |
} |
| 441 |
|
| 442 |
func TestDiscriminatorPropertyMissingError_LocalizedString(t *testing.T) { |
| 443 |
err := &DiscriminatorPropertyMissingError{ |
| 444 |
PropertyName: "type", |
| 445 |
} |
| 446 |
|
| 447 |
printer := message.NewPrinter(message.MatchLanguage("en")) |
| 448 |
result := err.LocalizedString(printer) |
| 449 |
expected := "discriminator property 'type' is missing" |
| 450 |
assert.Equal(t, expected, result) |
| 451 |
} |
| 452 |
|
| 453 |
func TestDiscriminatorPropertyMissingError_Error(t *testing.T) { |
| 454 |
err := &DiscriminatorPropertyMissingError{ |
| 455 |
PropertyName: "type", |
| 456 |
} |
| 457 |
|
| 458 |
expected := "discriminator property 'type' is missing" |
| 459 |
assert.Equal(t, expected, err.Error()) |
| 460 |
} |
| 461 |
|
| 462 |
func TestCoercionError_KeywordPath(t *testing.T) { |
| 463 |
err := &CoercionError{ |
| 464 |
SourceType: "string", |
| 465 |
TargetType: "boolean", |
| 466 |
Value: "yes", |
| 467 |
Message: "invalid value", |
| 468 |
} |
| 469 |
|
| 470 |
path := err.KeywordPath() |
| 471 |
assert.Equal(t, []string{"type"}, path) |
| 472 |
} |
| 473 |
|
| 474 |
func TestCoercionError_LocalizedString(t *testing.T) { |
| 475 |
err := &CoercionError{ |
| 476 |
SourceType: "string", |
| 477 |
TargetType: "boolean", |
| 478 |
Value: "yes", |
| 479 |
Message: "invalid value", |
| 480 |
} |
| 481 |
|
| 482 |
printer := message.NewPrinter(message.MatchLanguage("en")) |
| 483 |
result := err.LocalizedString(printer) |
| 484 |
expected := "cannot coerce string 'yes' to boolean: invalid value" |
| 485 |
assert.Equal(t, expected, result) |
| 486 |
} |
| 487 |
|
| 488 |
func TestCoercionError_Error(t *testing.T) { |
| 489 |
err := &CoercionError{ |
| 490 |
SourceType: "string", |
| 491 |
TargetType: "boolean", |
| 492 |
Value: "yes", |
| 493 |
Message: "invalid value", |
| 494 |
} |
| 495 |
|
| 496 |
expected := "cannot coerce string 'yes' to boolean: invalid value" |
| 497 |
assert.Equal(t, expected, err.Error()) |
| 498 |
} |
| 499 |
|
| 500 |
// Test metadata keywords compilation |
| 501 |
func TestMetadataKeywords_ExampleCompilation(t *testing.T) { |
| 502 |
obj := map[string]any{ |
| 503 |
"type": "string", |
| 504 |
"example": map[string]any{ |
| 505 |
"nested": "value", |
| 506 |
}, |
| 507 |
} |
| 508 |
|
| 509 |
ext, err := CompileExample(nil, obj, Version30) |
| 510 |
assert.NoError(t, err) |
| 511 |
assert.NotNil(t, ext) |
| 512 |
} |
| 513 |
|
| 514 |
func TestMetadataKeywords_DeprecatedTrueCompilation(t *testing.T) { |
| 515 |
obj := map[string]any{ |
| 516 |
"type": "string", |
| 517 |
"deprecated": true, |
| 518 |
} |
| 519 |
|
| 520 |
ext, err := CompileDeprecated(nil, obj, Version30) |
| 521 |
assert.NoError(t, err) |
| 522 |
assert.NotNil(t, ext) |
| 523 |
} |
| 524 |
|
| 525 |
func TestMetadataKeywords_DeprecatedFalseCompilation(t *testing.T) { |
| 526 |
obj := map[string]any{ |
| 527 |
"type": "string", |
| 528 |
"deprecated": false, |
| 529 |
} |
| 530 |
|
| 531 |
ext, err := CompileDeprecated(nil, obj, Version30) |
| 532 |
assert.NoError(t, err) |
| 533 |
assert.NotNil(t, ext) |
| 534 |
} |
| 535 |
|
| 536 |
func TestMetadataKeywords_DeprecatedInvalidType(t *testing.T) { |
| 537 |
obj := map[string]any{ |
| 538 |
"type": "string", |
| 539 |
"deprecated": "invalid", |
| 540 |
} |
| 541 |
|
| 542 |
ext, err := CompileDeprecated(nil, obj, Version30) |
| 543 |
assert.Error(t, err) |
| 544 |
assert.Nil(t, ext) |
| 545 |
assert.Contains(t, err.Error(), "deprecated must be a boolean value") |
| 546 |
} |
| 547 |
|
| 548 |
func TestMetadataKeywords_NoKeywords(t *testing.T) { |
| 549 |
obj := map[string]any{ |
| 550 |
"type": "string", |
| 551 |
} |
| 552 |
|
| 553 |
ext, err := CompileExample(nil, obj, Version30) |
| 554 |
assert.NoError(t, err) |
| 555 |
assert.Nil(t, ext) |
| 556 |
|
| 557 |
ext, err = CompileDeprecated(nil, obj, Version30) |
| 558 |
assert.NoError(t, err) |
| 559 |
assert.Nil(t, ext) |
| 560 |
} |
| 561 |
|
| 562 |
// Test discriminator compilation |
| 563 |
func TestDiscriminatorKeyword_ValidCompilation(t *testing.T) { |
| 564 |
obj := map[string]any{ |
| 565 |
"discriminator": map[string]any{ |
| 566 |
"propertyName": "type", |
| 567 |
"mapping": map[string]any{ |
| 568 |
"dog": "#/components/schemas/Dog", |
| 569 |
}, |
| 570 |
}, |
| 571 |
} |
| 572 |
|
| 573 |
ext, err := CompileDiscriminator(nil, obj, Version30) |
| 574 |
assert.NoError(t, err) |
| 575 |
assert.NotNil(t, ext) |
| 576 |
} |
| 577 |
|
| 578 |
func TestDiscriminatorKeyword_MissingPropertyName_Comprehensive(t *testing.T) { |
| 579 |
obj := map[string]any{ |
| 580 |
"discriminator": map[string]any{ |
| 581 |
"mapping": map[string]any{ |
| 582 |
"dog": "#/components/schemas/Dog", |
| 583 |
}, |
| 584 |
}, |
| 585 |
} |
| 586 |
|
| 587 |
ext, err := CompileDiscriminator(nil, obj, Version30) |
| 588 |
assert.Error(t, err) |
| 589 |
assert.Nil(t, ext) |
| 590 |
assert.Contains(t, err.Error(), "discriminator must have a propertyName field") |
| 591 |
} |
| 592 |
|
| 593 |
func TestDiscriminatorKeyword_PropertyNameNotString(t *testing.T) { |
| 594 |
obj := map[string]any{ |
| 595 |
"discriminator": map[string]any{ |
| 596 |
"propertyName": 123, |
| 597 |
}, |
| 598 |
} |
| 599 |
|
| 600 |
ext, err := CompileDiscriminator(nil, obj, Version30) |
| 601 |
assert.Error(t, err) |
| 602 |
assert.Nil(t, ext) |
| 603 |
assert.Contains(t, err.Error(), "discriminator propertyName must be a string") |
| 604 |
} |
| 605 |
|
| 606 |
func TestDiscriminatorKeyword_NotObject(t *testing.T) { |
| 607 |
obj := map[string]any{ |
| 608 |
"discriminator": "invalid", |
| 609 |
} |
| 610 |
|
| 611 |
ext, err := CompileDiscriminator(nil, obj, Version30) |
| 612 |
assert.Error(t, err) |
| 613 |
assert.Nil(t, ext) |
| 614 |
assert.Contains(t, err.Error(), "discriminator must be an object") |
| 615 |
} |
| 616 |
|
| 617 |
func TestDiscriminatorKeyword_NoDiscriminator(t *testing.T) { |
| 618 |
obj := map[string]any{ |
| 619 |
"type": "object", |
| 620 |
} |
| 621 |
|
| 622 |
ext, err := CompileDiscriminator(nil, obj, Version30) |
| 623 |
assert.NoError(t, err) |
| 624 |
assert.Nil(t, ext) |
| 625 |
} |
| 626 |
|
| 627 |
// Test end-to-end discriminator validation |
| 628 |
func TestDiscriminatorValidation_PropertyMissing(t *testing.T) { |
| 629 |
schemaJSON := `{ |
| 630 |
"type": "object", |
| 631 |
"discriminator": { |
| 632 |
"propertyName": "type" |
| 633 |
}, |
| 634 |
"properties": { |
| 635 |
"name": { |
| 636 |
"type": "string" |
| 637 |
} |
| 638 |
} |
| 639 |
}` |
| 640 |
|
| 641 |
schema, err := jsonschema.UnmarshalJSON(strings.NewReader(schemaJSON)) |
| 642 |
assert.NoError(t, err) |
| 643 |
|
| 644 |
compiler := jsonschema.NewCompiler() |
| 645 |
compiler.RegisterVocabulary(NewOpenAPIVocabulary(Version30)) |
| 646 |
compiler.AssertVocabs() |
| 647 |
|
| 648 |
err = compiler.AddResource("test.json", schema) |
| 649 |
assert.NoError(t, err) |
| 650 |
|
| 651 |
compiledSchema, err := compiler.Compile("test.json") |
| 652 |
assert.NoError(t, err) |
| 653 |
|
| 654 |
// Test object missing discriminator property |
| 655 |
testData := map[string]any{ |
| 656 |
"name": "test", |
| 657 |
// Missing "type" property |
| 658 |
} |
| 659 |
err = compiledSchema.Validate(testData) |
| 660 |
assert.Error(t, err) |
| 661 |
assert.Contains(t, err.Error(), "discriminator property 'type' is missing") |
| 662 |
} |
| 663 |
|
| 664 |
func TestDiscriminatorValidation_PropertyPresent(t *testing.T) { |
| 665 |
schemaJSON := `{ |
| 666 |
"type": "object", |
| 667 |
"discriminator": { |
| 668 |
"propertyName": "type" |
| 669 |
}, |
| 670 |
"properties": { |
| 671 |
"type": { |
| 672 |
"type": "string" |
| 673 |
}, |
| 674 |
"name": { |
| 675 |
"type": "string" |
| 676 |
} |
| 677 |
} |
| 678 |
}` |
| 679 |
|
| 680 |
schema, err := jsonschema.UnmarshalJSON(strings.NewReader(schemaJSON)) |
| 681 |
assert.NoError(t, err) |
| 682 |
|
| 683 |
compiler := jsonschema.NewCompiler() |
| 684 |
compiler.RegisterVocabulary(NewOpenAPIVocabulary(Version30)) |
| 685 |
compiler.AssertVocabs() |
| 686 |
|
| 687 |
err = compiler.AddResource("test.json", schema) |
| 688 |
assert.NoError(t, err) |
| 689 |
|
| 690 |
compiledSchema, err := compiler.Compile("test.json") |
| 691 |
assert.NoError(t, err) |
| 692 |
|
| 693 |
// Test object with discriminator property |
| 694 |
testData := map[string]any{ |
| 695 |
"type": "dog", |
| 696 |
"name": "Buddy", |
| 697 |
} |
| 698 |
err = compiledSchema.Validate(testData) |
| 699 |
assert.NoError(t, err) |
| 700 |
} |
| 701 |
|
| 702 |
func TestDiscriminatorValidation_NonObjectValue(t *testing.T) { |
| 703 |
schemaJSON := `{ |
| 704 |
"type": "object", |
| 705 |
"discriminator": { |
| 706 |
"propertyName": "type", |
| 707 |
"mapping": { |
| 708 |
"dog": "#/components/schemas/Dog" |
| 709 |
} |
| 710 |
} |
| 711 |
}` |
| 712 |
|
| 713 |
schema, err := jsonschema.UnmarshalJSON(strings.NewReader(schemaJSON)) |
| 714 |
assert.NoError(t, err) |
| 715 |
|
| 716 |
compiler := jsonschema.NewCompiler() |
| 717 |
compiler.RegisterVocabulary(NewOpenAPIVocabulary(Version30)) |
| 718 |
compiler.AssertVocabs() |
| 719 |
|
| 720 |
err = compiler.AddResource("test.json", schema) |
| 721 |
assert.NoError(t, err) |
| 722 |
|
| 723 |
compiledSchema, err := compiler.Compile("test.json") |
| 724 |
assert.NoError(t, err) |
| 725 |
|
| 726 |
// Test with non-object (should pass discriminator validation but fail type validation) |
| 727 |
err = compiledSchema.Validate("not an object") |
| 728 |
assert.Error(t, err) |
| 729 |
// Should get type validation error, not discriminator error |
| 730 |
assert.NotContains(t, err.Error(), "discriminator property") |
| 731 |
} |
| 732 |
|
| 733 |
func TestDiscriminatorValidation_DiscriminatorBadMapping(t *testing.T) { |
| 734 |
schemaJSON := `{ |
| 735 |
"type": "object", |
| 736 |
"discriminator": { |
| 737 |
"propertyName": "type", |
| 738 |
"mapping": "not an object" |
| 739 |
} |
| 740 |
}` |
| 741 |
|
| 742 |
schema, err := jsonschema.UnmarshalJSON(strings.NewReader(schemaJSON)) |
| 743 |
assert.NoError(t, err) |
| 744 |
|
| 745 |
compiler := jsonschema.NewCompiler() |
| 746 |
compiler.RegisterVocabulary(NewOpenAPIVocabulary(Version30)) |
| 747 |
compiler.AssertVocabs() |
| 748 |
|
| 749 |
err = compiler.AddResource("test.json", schema) |
| 750 |
assert.NoError(t, err) |
| 751 |
|
| 752 |
_, err = compiler.Compile("test.json") |
| 753 |
assert.Error(t, err) |
| 754 |
assert.Equal(t, "OpenAPI keyword 'discriminator': discriminator mapping must be an object", err.Error()) |
| 755 |
} |
| 756 |
|
| 757 |
func TestDiscriminatorValidation_BadMappingType(t *testing.T) { |
| 758 |
schemaJSON := `{ |
| 759 |
"type": "object", |
| 760 |
"discriminator": { |
| 761 |
"propertyName": "type", |
| 762 |
"mapping": { |
| 763 |
"fish": 12345 |
| 764 |
} |
| 765 |
} |
| 766 |
}` |
| 767 |
|
| 768 |
schema, err := jsonschema.UnmarshalJSON(strings.NewReader(schemaJSON)) |
| 769 |
assert.NoError(t, err) |
| 770 |
|
| 771 |
compiler := jsonschema.NewCompiler() |
| 772 |
compiler.RegisterVocabulary(NewOpenAPIVocabulary(Version30)) |
| 773 |
compiler.AssertVocabs() |
| 774 |
|
| 775 |
err = compiler.AddResource("test.json", schema) |
| 776 |
assert.NoError(t, err) |
| 777 |
|
| 778 |
_, err = compiler.Compile("test.json") |
| 779 |
assert.Error(t, err) |
| 780 |
assert.Equal(t, "OpenAPI keyword 'discriminator': discriminator mapping values must be strings", err.Error()) |
| 781 |
} |