| 1 |
// Copyright 2025 Princess B33f Heavy Industries / Dave Shanley |
| 2 |
// SPDX-License-Identifier: MIT |
| 3 |
|
| 4 |
package openapi_vocabulary |
| 5 |
|
| 6 |
import ( |
| 7 |
"fmt" |
| 8 |
"strings" |
| 9 |
"testing" |
| 10 |
|
| 11 |
"github.com/santhosh-tekuri/jsonschema/v6" |
| 12 |
"github.com/stretchr/testify/assert" |
| 13 |
) |
| 14 |
|
| 15 |
func TestCoercion_Vocabulary_CompilationSuccess(t *testing.T) { |
| 16 |
// Test that coercion vocabulary compiles successfully for all scalar types |
| 17 |
testCases := []string{ |
| 18 |
`{"type": "boolean"}`, |
| 19 |
`{"type": "number"}`, |
| 20 |
`{"type": "integer"}`, |
| 21 |
`{"type": ["boolean", "null"]}`, |
| 22 |
`{"type": "string"}`, // Should not get coercion extension |
| 23 |
} |
| 24 |
|
| 25 |
for i, schemaJSON := range testCases { |
| 26 |
t.Run(fmt.Sprintf("Schema_%d", i), func(t *testing.T) { |
| 27 |
schema, err := jsonschema.UnmarshalJSON(strings.NewReader(schemaJSON)) |
| 28 |
assert.NoError(t, err) |
| 29 |
|
| 30 |
compiler := jsonschema.NewCompiler() |
| 31 |
compiler.RegisterVocabulary(NewOpenAPIVocabularyWithCoercion(Version30, true)) |
| 32 |
compiler.AssertVocabs() |
| 33 |
|
| 34 |
err = compiler.AddResource("test.json", schema) |
| 35 |
assert.NoError(t, err) |
| 36 |
|
| 37 |
// Should compile successfully |
| 38 |
compiledSchema, err := compiler.Compile("test.json") |
| 39 |
assert.NoError(t, err) |
| 40 |
assert.NotNil(t, compiledSchema) |
| 41 |
}) |
| 42 |
} |
| 43 |
} |
| 44 |
|
| 45 |
func TestCoercion_Vocabulary_DisabledCompilation(t *testing.T) { |
| 46 |
// Test that vocabulary compiles successfully when coercion is disabled |
| 47 |
schemaJSON := `{"type": "boolean"}` |
| 48 |
|
| 49 |
schema, err := jsonschema.UnmarshalJSON(strings.NewReader(schemaJSON)) |
| 50 |
assert.NoError(t, err) |
| 51 |
|
| 52 |
compiler := jsonschema.NewCompiler() |
| 53 |
compiler.RegisterVocabulary(NewOpenAPIVocabularyWithCoercion(Version30, false)) // Disabled |
| 54 |
compiler.AssertVocabs() |
| 55 |
|
| 56 |
err = compiler.AddResource("test.json", schema) |
| 57 |
assert.NoError(t, err) |
| 58 |
|
| 59 |
// Should compile successfully even with coercion disabled |
| 60 |
compiledSchema, err := compiler.Compile("test.json") |
| 61 |
assert.NoError(t, err) |
| 62 |
assert.NotNil(t, compiledSchema) |
| 63 |
} |
| 64 |
|
| 65 |
// Additional comprehensive coercion tests |
| 66 |
|
| 67 |
func TestCoercionExtension_Validate_CoercionDisabled(t *testing.T) { |
| 68 |
// Test with coercion disabled via vocabulary |
| 69 |
schemaJSON := `{"type": "boolean"}` |
| 70 |
schema, err := jsonschema.UnmarshalJSON(strings.NewReader(schemaJSON)) |
| 71 |
assert.NoError(t, err) |
| 72 |
|
| 73 |
compiler := jsonschema.NewCompiler() |
| 74 |
compiler.RegisterVocabulary(NewOpenAPIVocabularyWithCoercion(Version30, false)) |
| 75 |
compiler.AssertVocabs() |
| 76 |
|
| 77 |
err = compiler.AddResource("test.json", schema) |
| 78 |
assert.NoError(t, err) |
| 79 |
|
| 80 |
compiledSchema, err := compiler.Compile("test.json") |
| 81 |
assert.NoError(t, err) |
| 82 |
|
| 83 |
// With coercion disabled, string "true" should fail normal validation |
| 84 |
err = compiledSchema.Validate("true") |
| 85 |
assert.Error(t, err, "Should fail without coercion") |
| 86 |
|
| 87 |
// But actual boolean should pass |
| 88 |
err = compiledSchema.Validate(true) |
| 89 |
assert.NoError(t, err, "Should pass with actual boolean") |
| 90 |
} |
| 91 |
|
| 92 |
func TestCoercionExtension_Validate_NonStringValue(t *testing.T) { |
| 93 |
// Test that non-string values don't trigger coercion |
| 94 |
schemaJSON := `{"type": "boolean"}` |
| 95 |
schema, err := jsonschema.UnmarshalJSON(strings.NewReader(schemaJSON)) |
| 96 |
assert.NoError(t, err) |
| 97 |
|
| 98 |
compiler := jsonschema.NewCompiler() |
| 99 |
compiler.RegisterVocabulary(NewOpenAPIVocabularyWithCoercion(Version30, true)) |
| 100 |
compiler.AssertVocabs() |
| 101 |
|
| 102 |
err = compiler.AddResource("test.json", schema) |
| 103 |
assert.NoError(t, err) |
| 104 |
|
| 105 |
compiledSchema, err := compiler.Compile("test.json") |
| 106 |
assert.NoError(t, err) |
| 107 |
|
| 108 |
// Non-string values should be handled by normal JSON Schema validation |
| 109 |
err = compiledSchema.Validate(true) |
| 110 |
assert.NoError(t, err, "Boolean should pass") |
| 111 |
|
| 112 |
err = compiledSchema.Validate(123) |
| 113 |
assert.Error(t, err, "Number should fail for boolean type") |
| 114 |
|
| 115 |
err = compiledSchema.Validate(nil) |
| 116 |
assert.Error(t, err, "Null should fail for boolean type") |
| 117 |
} |
| 118 |
|
| 119 |
func TestCoercionExtension_Validate_BooleanCoercion_Valid(t *testing.T) { |
| 120 |
// Create a schema that allows both boolean and string types for coercion to work |
| 121 |
schemaJSON := `{"type": ["boolean", "string"]}` |
| 122 |
schema, err := jsonschema.UnmarshalJSON(strings.NewReader(schemaJSON)) |
| 123 |
assert.NoError(t, err) |
| 124 |
|
| 125 |
compiler := jsonschema.NewCompiler() |
| 126 |
compiler.RegisterVocabulary(NewOpenAPIVocabularyWithCoercion(Version30, true)) |
| 127 |
compiler.AssertVocabs() |
| 128 |
|
| 129 |
err = compiler.AddResource("test.json", schema) |
| 130 |
assert.NoError(t, err) |
| 131 |
|
| 132 |
compiledSchema, err := compiler.Compile("test.json") |
| 133 |
assert.NoError(t, err) |
| 134 |
|
| 135 |
testCases := []string{"true", "false"} |
| 136 |
|
| 137 |
for _, testCase := range testCases { |
| 138 |
err = compiledSchema.Validate(testCase) |
| 139 |
assert.NoError(t, err, "Should pass for valid boolean string: %s", testCase) |
| 140 |
} |
| 141 |
} |
| 142 |
|
| 143 |
func TestCoercionExtension_Validate_BooleanCoercion_Invalid(t *testing.T) { |
| 144 |
schemaJSON := `{"type": ["boolean", "string"]}` |
| 145 |
schema, err := jsonschema.UnmarshalJSON(strings.NewReader(schemaJSON)) |
| 146 |
assert.NoError(t, err) |
| 147 |
|
| 148 |
compiler := jsonschema.NewCompiler() |
| 149 |
compiler.RegisterVocabulary(NewOpenAPIVocabularyWithCoercion(Version30, true)) |
| 150 |
compiler.AssertVocabs() |
| 151 |
|
| 152 |
err = compiler.AddResource("test.json", schema) |
| 153 |
assert.NoError(t, err) |
| 154 |
|
| 155 |
compiledSchema, err := compiler.Compile("test.json") |
| 156 |
assert.NoError(t, err) |
| 157 |
|
| 158 |
testCases := []string{"yes", "no", "1", "0", "True", "FALSE", ""} |
| 159 |
|
| 160 |
for _, testCase := range testCases { |
| 161 |
err = compiledSchema.Validate(testCase) |
| 162 |
assert.Error(t, err, "Should fail for invalid boolean string: %s", testCase) |
| 163 |
assert.Contains(t, err.Error(), "cannot coerce", "Should contain coercion error message") |
| 164 |
} |
| 165 |
} |
| 166 |
|
| 167 |
func TestCoercionExtension_HasType_Methods(t *testing.T) { |
| 168 |
// Test hasType method with string type |
| 169 |
ext := &coercionExtension{ |
| 170 |
schemaType: "boolean", |
| 171 |
} |
| 172 |
|
| 173 |
assert.True(t, ext.hasType("boolean")) |
| 174 |
assert.False(t, ext.hasType("number")) |
| 175 |
assert.False(t, ext.hasType("integer")) |
| 176 |
|
| 177 |
// Test hasType method with array type |
| 178 |
ext = &coercionExtension{ |
| 179 |
schemaType: []any{"boolean", "null"}, |
| 180 |
} |
| 181 |
|
| 182 |
assert.True(t, ext.hasType("boolean")) |
| 183 |
assert.True(t, ext.hasType("null")) |
| 184 |
assert.False(t, ext.hasType("number")) |
| 185 |
|
| 186 |
// Test hasType method with invalid array items |
| 187 |
ext = &coercionExtension{ |
| 188 |
schemaType: []any{123, "boolean"}, |
| 189 |
} |
| 190 |
|
| 191 |
assert.True(t, ext.hasType("boolean")) |
| 192 |
assert.False(t, ext.hasType("number")) |
| 193 |
|
| 194 |
// Test hasType method with non-string, non-array |
| 195 |
ext = &coercionExtension{ |
| 196 |
schemaType: 123, |
| 197 |
} |
| 198 |
|
| 199 |
assert.False(t, ext.hasType("boolean")) |
| 200 |
assert.False(t, ext.hasType("number")) |
| 201 |
} |
| 202 |
|
| 203 |
func TestCoercionExtension_ValidationMethods(t *testing.T) { |
| 204 |
ext := &coercionExtension{} |
| 205 |
|
| 206 |
// Test boolean validation |
| 207 |
assert.True(t, ext.isValidBooleanString("true")) |
| 208 |
assert.True(t, ext.isValidBooleanString("false")) |
| 209 |
assert.False(t, ext.isValidBooleanString("True")) |
| 210 |
assert.False(t, ext.isValidBooleanString("FALSE")) |
| 211 |
assert.False(t, ext.isValidBooleanString("yes")) |
| 212 |
assert.False(t, ext.isValidBooleanString("no")) |
| 213 |
assert.False(t, ext.isValidBooleanString("1")) |
| 214 |
assert.False(t, ext.isValidBooleanString("0")) |
| 215 |
assert.False(t, ext.isValidBooleanString("")) |
| 216 |
|
| 217 |
// Test number validation |
| 218 |
validNumbers := []string{ |
| 219 |
"123", "-123", "0", "-0", |
| 220 |
"123.45", "-123.45", "0.0", |
| 221 |
"1e5", "1E5", "1e+5", "1e-5", |
| 222 |
"1.23e10", "1.23E-10", |
| 223 |
} |
| 224 |
|
| 225 |
for _, num := range validNumbers { |
| 226 |
assert.True(t, ext.isValidNumberString(num), "Should be valid number: %s", num) |
| 227 |
} |
| 228 |
|
| 229 |
invalidNumbers := []string{ |
| 230 |
"abc", "12.34.56", "1e", "e5", |
| 231 |
"1.23.45e10", "", "null", "true", |
| 232 |
"Infinity", "NaN", "+123", |
| 233 |
} |
| 234 |
|
| 235 |
for _, num := range invalidNumbers { |
| 236 |
assert.False(t, ext.isValidNumberString(num), "Should be invalid number: %s", num) |
| 237 |
} |
| 238 |
|
| 239 |
// Test integer validation |
| 240 |
validIntegers := []string{"123", "-123", "0"} |
| 241 |
|
| 242 |
for _, num := range validIntegers { |
| 243 |
assert.True(t, ext.isValidIntegerString(num), "Should be valid integer: %s", num) |
| 244 |
} |
| 245 |
|
| 246 |
invalidIntegers := []string{ |
| 247 |
"123.45", "abc", "007", "1e5", |
| 248 |
"", "null", "true", "+123", |
| 249 |
} |
| 250 |
|
| 251 |
for _, num := range invalidIntegers { |
| 252 |
assert.False(t, ext.isValidIntegerString(num), "Should be invalid integer: %s", num) |
| 253 |
} |
| 254 |
} |
| 255 |
|
| 256 |
func TestCompileCoercion_CoercionDisabled(t *testing.T) { |
| 257 |
obj := map[string]any{ |
| 258 |
"type": "boolean", |
| 259 |
} |
| 260 |
|
| 261 |
ext, err := CompileCoercion(nil, obj, false) |
| 262 |
assert.NoError(t, err) |
| 263 |
assert.Nil(t, ext) |
| 264 |
} |
| 265 |
|
| 266 |
func TestCompileCoercion_NoType(t *testing.T) { |
| 267 |
obj := map[string]any{ |
| 268 |
"minLength": 1, |
| 269 |
} |
| 270 |
|
| 271 |
ext, err := CompileCoercion(nil, obj, true) |
| 272 |
assert.NoError(t, err) |
| 273 |
assert.Nil(t, ext) |
| 274 |
} |
| 275 |
|
| 276 |
func TestCompileCoercion_NonCoercibleType(t *testing.T) { |
| 277 |
obj := map[string]any{ |
| 278 |
"type": "string", |
| 279 |
} |
| 280 |
|
| 281 |
ext, err := CompileCoercion(nil, obj, true) |
| 282 |
assert.NoError(t, err) |
| 283 |
assert.Nil(t, ext) |
| 284 |
} |
| 285 |
|
| 286 |
func TestCompileCoercion_CoercibleTypes(t *testing.T) { |
| 287 |
coercibleTypes := []string{"boolean", "number", "integer"} |
| 288 |
|
| 289 |
for _, schemaType := range coercibleTypes { |
| 290 |
obj := map[string]any{ |
| 291 |
"type": schemaType, |
| 292 |
} |
| 293 |
|
| 294 |
ext, err := CompileCoercion(nil, obj, true) |
| 295 |
assert.NoError(t, err, "Should compile for type: %s", schemaType) |
| 296 |
assert.NotNil(t, ext, "Should return extension for type: %s", schemaType) |
| 297 |
|
| 298 |
coercionExt, ok := ext.(*coercionExtension) |
| 299 |
assert.True(t, ok, "Should be coercionExtension") |
| 300 |
assert.Equal(t, schemaType, coercionExt.schemaType) |
| 301 |
assert.True(t, coercionExt.allowCoercion) |
| 302 |
} |
| 303 |
} |
| 304 |
|
| 305 |
func TestIsCoercibleType_String(t *testing.T) { |
| 306 |
assert.True(t, IsCoercibleType("boolean")) |
| 307 |
assert.True(t, IsCoercibleType("number")) |
| 308 |
assert.True(t, IsCoercibleType("integer")) |
| 309 |
assert.False(t, IsCoercibleType("string")) |
| 310 |
assert.False(t, IsCoercibleType("object")) |
| 311 |
assert.False(t, IsCoercibleType("array")) |
| 312 |
} |
| 313 |
|
| 314 |
func TestIsCoercibleType_Array(t *testing.T) { |
| 315 |
// Array containing coercible type - should return true |
| 316 |
assert.True(t, IsCoercibleType([]any{"string", "boolean"})) |
| 317 |
assert.True(t, IsCoercibleType([]any{"number", "null"})) |
| 318 |
assert.True(t, IsCoercibleType([]any{"integer"})) |
| 319 |
|
| 320 |
// Array containing only non-coercible types - should return false |
| 321 |
assert.False(t, IsCoercibleType([]any{"string", "null"})) |
| 322 |
assert.False(t, IsCoercibleType([]any{"object", "array"})) |
| 323 |
assert.False(t, IsCoercibleType([]any{"string"})) |
| 324 |
|
| 325 |
// Empty array - should return false |
| 326 |
assert.False(t, IsCoercibleType([]any{})) |
| 327 |
} |
| 328 |
|
| 329 |
func TestCoercionExtension_ShouldCoerceToMethods(t *testing.T) { |
| 330 |
// Test shouldCoerceToNumber method |
| 331 |
ext := &coercionExtension{ |
| 332 |
schemaType: "number", |
| 333 |
} |
| 334 |
assert.True(t, ext.shouldCoerceToNumber()) |
| 335 |
assert.False(t, ext.shouldCoerceToBoolean()) |
| 336 |
assert.False(t, ext.shouldCoerceToInteger()) |
| 337 |
|
| 338 |
// Test shouldCoerceToInteger method |
| 339 |
ext = &coercionExtension{ |
| 340 |
schemaType: "integer", |
| 341 |
} |
| 342 |
assert.True(t, ext.shouldCoerceToInteger()) |
| 343 |
assert.False(t, ext.shouldCoerceToBoolean()) |
| 344 |
assert.False(t, ext.shouldCoerceToNumber()) |
| 345 |
|
| 346 |
// Test shouldCoerceToBoolean method |
| 347 |
ext = &coercionExtension{ |
| 348 |
schemaType: "boolean", |
| 349 |
} |
| 350 |
assert.True(t, ext.shouldCoerceToBoolean()) |
| 351 |
assert.False(t, ext.shouldCoerceToNumber()) |
| 352 |
assert.False(t, ext.shouldCoerceToInteger()) |
| 353 |
} |
| 354 |
|
| 355 |
func TestCoercionExtension_Validate_NumberCoercion(t *testing.T) { |
| 356 |
// Test number coercion path in Validate method |
| 357 |
schemaJSON := `{"type": ["number", "string"]}` |
| 358 |
schema, err := jsonschema.UnmarshalJSON(strings.NewReader(schemaJSON)) |
| 359 |
assert.NoError(t, err) |
| 360 |
|
| 361 |
compiler := jsonschema.NewCompiler() |
| 362 |
compiler.RegisterVocabulary(NewOpenAPIVocabularyWithCoercion(Version30, true)) |
| 363 |
compiler.AssertVocabs() |
| 364 |
|
| 365 |
err = compiler.AddResource("test.json", schema) |
| 366 |
assert.NoError(t, err) |
| 367 |
|
| 368 |
compiledSchema, err := compiler.Compile("test.json") |
| 369 |
assert.NoError(t, err) |
| 370 |
|
| 371 |
// Test valid number strings |
| 372 |
err = compiledSchema.Validate("123.45") |
| 373 |
assert.NoError(t, err) |
| 374 |
|
| 375 |
err = compiledSchema.Validate("-123") |
| 376 |
assert.NoError(t, err) |
| 377 |
|
| 378 |
// Test invalid number string |
| 379 |
err = compiledSchema.Validate("abc") |
| 380 |
assert.Error(t, err) |
| 381 |
assert.Contains(t, err.Error(), "cannot coerce") |
| 382 |
} |
| 383 |
|
| 384 |
func TestCoercionExtension_Validate_IntegerCoercion(t *testing.T) { |
| 385 |
// Test integer coercion path in Validate method |
| 386 |
schemaJSON := `{"type": ["integer", "string"]}` |
| 387 |
schema, err := jsonschema.UnmarshalJSON(strings.NewReader(schemaJSON)) |
| 388 |
assert.NoError(t, err) |
| 389 |
|
| 390 |
compiler := jsonschema.NewCompiler() |
| 391 |
compiler.RegisterVocabulary(NewOpenAPIVocabularyWithCoercion(Version30, true)) |
| 392 |
compiler.AssertVocabs() |
| 393 |
|
| 394 |
err = compiler.AddResource("test.json", schema) |
| 395 |
assert.NoError(t, err) |
| 396 |
|
| 397 |
compiledSchema, err := compiler.Compile("test.json") |
| 398 |
assert.NoError(t, err) |
| 399 |
|
| 400 |
// Test valid integer strings |
| 401 |
err = compiledSchema.Validate("123") |
| 402 |
assert.NoError(t, err) |
| 403 |
|
| 404 |
err = compiledSchema.Validate("-123") |
| 405 |
assert.NoError(t, err) |
| 406 |
|
| 407 |
// Test invalid integer string |
| 408 |
err = compiledSchema.Validate("123.45") |
| 409 |
assert.Error(t, err) |
| 410 |
assert.Contains(t, err.Error(), "cannot coerce") |
| 411 |
} |
| 412 |
|
| 413 |
func TestMetadataExtensions_Validate_Coverage(t *testing.T) { |
| 414 |
// Test example extension validate method (just for coverage) |
| 415 |
exampleExt := &exampleExtension{example: "test"} |
| 416 |
exampleExt.Validate(nil, "any-value") // No-op, just for coverage |
| 417 |
|
| 418 |
// Test deprecated extension validate method (just for coverage) |
| 419 |
deprecatedExt := &deprecatedExtension{deprecated: true} |
| 420 |
deprecatedExt.Validate(nil, "any-value") // No-op, just for coverage |
| 421 |
} |