|
2 | 2 |
|
3 | 3 | import jdk.sandbox.java.util.json.Json; |
4 | 4 | import jdk.sandbox.java.util.json.JsonValue; |
| 5 | +import jdk.sandbox.java.util.json.JsonNumber; |
5 | 6 | import org.junit.jupiter.api.Test; |
6 | 7 |
|
7 | 8 | import static org.assertj.core.api.Assertions.assertThat; |
@@ -440,4 +441,93 @@ public void testRefSchemaRecursiveBad() throws Exception { |
440 | 441 | .as("Recursive ref should reject heterogeneous nested data") |
441 | 442 | .isFalse(); |
442 | 443 | } |
| 444 | + |
| 445 | + /// Micro test to debug int32 validation with decimal values |
| 446 | + /// Should reject non-integer values like 3.14 for int32 type |
| 447 | + @Test |
| 448 | + public void testInt32RejectsDecimal() throws Exception { |
| 449 | + JsonValue schema = Json.parse("{\"type\": \"int32\"}"); |
| 450 | + JsonValue decimalValue = JsonNumber.of(new java.math.BigDecimal("3.14")); |
| 451 | + |
| 452 | + LOG.info(() -> "Testing int32 validation against decimal value 3.14"); |
| 453 | + LOG.fine(() -> "Schema: " + schema); |
| 454 | + LOG.fine(() -> "Instance: " + decimalValue); |
| 455 | + |
| 456 | + Jtd validator = new Jtd(); |
| 457 | + Jtd.Result result = validator.validate(schema, decimalValue); |
| 458 | + |
| 459 | + LOG.fine(() -> "Validation result: " + (result.isValid() ? "VALID" : "INVALID")); |
| 460 | + if (!result.isValid()) { |
| 461 | + LOG.fine(() -> "ERRORS: " + result.errors()); |
| 462 | + } |
| 463 | + |
| 464 | + // This should be invalid - int32 should reject decimal values |
| 465 | + assertThat(result.isValid()) |
| 466 | + .as("int32 should reject decimal value 3.14") |
| 467 | + .isFalse(); |
| 468 | + assertThat(result.errors()) |
| 469 | + .as("Should have validation errors for decimal value") |
| 470 | + .isNotEmpty(); |
| 471 | + } |
| 472 | + |
| 473 | + /// Test that integer types accept valid integer representations with trailing zeros |
| 474 | + /// RFC 8927 §2.2.3.1: "An integer value is a number without a fractional component" |
| 475 | + /// Values like 3.0, 3.000 are valid integers despite positive scale |
| 476 | + @Test |
| 477 | + public void testIntegerTypesAcceptTrailingZeros() throws Exception { |
| 478 | + JsonValue schema = Json.parse("{\"type\": \"int32\"}"); |
| 479 | + |
| 480 | + // Valid integer representations with trailing zeros |
| 481 | + JsonValue[] validIntegers = { |
| 482 | + JsonNumber.of(new java.math.BigDecimal("3.0")), |
| 483 | + JsonNumber.of(new java.math.BigDecimal("3.000")), |
| 484 | + JsonNumber.of(new java.math.BigDecimal("42.00")), |
| 485 | + JsonNumber.of(new java.math.BigDecimal("0.0")) |
| 486 | + }; |
| 487 | + |
| 488 | + Jtd validator = new Jtd(); |
| 489 | + |
| 490 | + for (JsonValue validValue : validIntegers) { |
| 491 | + Jtd.Result result = validator.validate(schema, validValue); |
| 492 | + |
| 493 | + LOG.fine(() -> "Testing int32 with valid integer representation: " + validValue); |
| 494 | + |
| 495 | + assertThat(result.isValid()) |
| 496 | + .as("int32 should accept integer representation %s", validValue) |
| 497 | + .isTrue(); |
| 498 | + assertThat(result.errors()) |
| 499 | + .as("Should have no validation errors for integer representation %s", validValue) |
| 500 | + .isEmpty(); |
| 501 | + } |
| 502 | + } |
| 503 | + |
| 504 | + /// Test that integer types reject values with actual fractional components |
| 505 | + /// RFC 8927 §2.2.3.1: "An integer value is a number without a fractional component" |
| 506 | + @Test |
| 507 | + public void testIntegerTypesRejectFractionalComponents() throws Exception { |
| 508 | + JsonValue schema = Json.parse("{\"type\": \"int32\"}"); |
| 509 | + |
| 510 | + // Invalid values with actual fractional components |
| 511 | + JsonValue[] invalidValues = { |
| 512 | + JsonNumber.of(new java.math.BigDecimal("3.1")), |
| 513 | + JsonNumber.of(new java.math.BigDecimal("3.0001")), |
| 514 | + JsonNumber.of(new java.math.BigDecimal("3.14")), |
| 515 | + JsonNumber.of(new java.math.BigDecimal("0.1")) |
| 516 | + }; |
| 517 | + |
| 518 | + Jtd validator = new Jtd(); |
| 519 | + |
| 520 | + for (JsonValue invalidValue : invalidValues) { |
| 521 | + Jtd.Result result = validator.validate(schema, invalidValue); |
| 522 | + |
| 523 | + LOG.fine(() -> "Testing int32 with fractional value: " + invalidValue); |
| 524 | + |
| 525 | + assertThat(result.isValid()) |
| 526 | + .as("int32 should reject fractional value %s", invalidValue) |
| 527 | + .isFalse(); |
| 528 | + assertThat(result.errors()) |
| 529 | + .as("Should have validation errors for fractional value %s", invalidValue) |
| 530 | + .isNotEmpty(); |
| 531 | + } |
| 532 | + } |
443 | 533 | } |
0 commit comments