Skip to content

Commit 52ec5e7

Browse files
committed
Issue #89 Fix int32 type validation to reject decimal values like 3.14
- Added BigDecimal fractional part checking in validateInteger method - Added test case testInt32RejectsDecimal() to verify the fix - Ensures all integer types (int8, uint8, int16, uint16, int32, uint32) reject decimal values - Maintains RFC 8927 compliance for integer type validation
1 parent 8dec57f commit 52ec5e7

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

json-java21-jtd/src/main/java/json/java21/jtd/JtdSchema.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,11 @@ Jtd.Result validateInteger(JsonValue instance, String type, boolean verboseError
244244
return Jtd.Result.failure(Jtd.Error.EXPECTED_INTEGER.message());
245245
}
246246

247+
// Handle BigDecimal - check if it has fractional part
248+
if (value instanceof java.math.BigDecimal bd && bd.scale() > 0) {
249+
return Jtd.Result.failure(Jtd.Error.EXPECTED_INTEGER.message());
250+
}
251+
247252
// Convert to long for range checking
248253
long longValue = value.longValue();
249254

json-java21-jtd/src/test/java/json/java21/jtd/TestRfc8927.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import jdk.sandbox.java.util.json.Json;
44
import jdk.sandbox.java.util.json.JsonValue;
5+
import jdk.sandbox.java.util.json.JsonNumber;
56
import org.junit.jupiter.api.Test;
67

78
import static org.assertj.core.api.Assertions.assertThat;
@@ -440,4 +441,32 @@ public void testRefSchemaRecursiveBad() throws Exception {
440441
.as("Recursive ref should reject heterogeneous nested data")
441442
.isFalse();
442443
}
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+
}
443472
}

0 commit comments

Comments
 (0)