From fb40864d295dcffb225b8cd9f13fc6d30f9c4866 Mon Sep 17 00:00:00 2001 From: Justin Tay <49700559+justin-tay@users.noreply.github.com> Date: Wed, 7 Feb 2024 01:56:57 +0800 Subject: [PATCH] Add test for type integer (#954) --- .../networknt/schema/TypeValidatorTest.java | 47 +++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/src/test/java/com/networknt/schema/TypeValidatorTest.java b/src/test/java/com/networknt/schema/TypeValidatorTest.java index 8a3d81d8b..8560b5652 100644 --- a/src/test/java/com/networknt/schema/TypeValidatorTest.java +++ b/src/test/java/com/networknt/schema/TypeValidatorTest.java @@ -92,4 +92,51 @@ void testTypeLoose() { assertEquals(1, messages.size()); } + + /** + * Issue 864. + */ + @Test + void integer() { + String schemaData = "{\r\n" + + " \"type\": \"integer\"\r\n" + + "}"; + JsonSchema schema = JsonSchemaFactory.getInstance(VersionFlag.V202012).getSchema(schemaData); + Set messages = schema.validate("1", InputFormat.JSON); + assertEquals(0, messages.size()); + messages = schema.validate("2.0", InputFormat.JSON); + assertEquals(0, messages.size()); + messages = schema.validate("2.000001", InputFormat.JSON); + assertEquals(1, messages.size()); + } + + /** + * Issue 864. + *

+ * In draft-04, "integer" is listed as a primitive type and defined as "a JSON + * number without a fraction or exponent part"; in draft-06, "integer" is not + * considered a primitive type and is only defined in the section for keyword + * "type" as "any number with a zero fractional part"; 1.0 is thus not a valid + * "integer" type in draft-04 and earlier, but is a valid "integer" type in + * draft-06 and later; note that both drafts say that integers SHOULD be encoded + * in JSON without fractional parts + * + * @see Draft-06 + * Release Notes + */ + @Test + void integerDraft4() { + String schemaData = "{\r\n" + + " \"type\": \"integer\"\r\n" + + "}"; + JsonSchema schema = JsonSchemaFactory.getInstance(VersionFlag.V4).getSchema(schemaData); + Set messages = schema.validate("1", InputFormat.JSON); + assertEquals(0, messages.size()); + // The logic in JsonNodeUtil specifically excludes V4 from this handling + messages = schema.validate("2.0", InputFormat.JSON); + assertEquals(1, messages.size()); + messages = schema.validate("2.000001", InputFormat.JSON); + assertEquals(1, messages.size()); + } }