From 08ca9e499ce8379554fbe4a1cf0d3ce0eca18b81 Mon Sep 17 00:00:00 2001 From: Steve Hu Date: Sat, 16 Nov 2019 13:00:29 -0500 Subject: [PATCH] fixes #54 convert all numbers to decimal node for comparison in enum --- src/main/java/com/networknt/schema/EnumValidator.java | 10 ++++++++-- .../com/networknt/schema/V201909JsonSchemaTest.java | 1 - .../java/com/networknt/schema/V6JsonSchemaTest.java | 1 - .../java/com/networknt/schema/V7JsonSchemaTest.java | 1 - 4 files changed, 8 insertions(+), 5 deletions(-) diff --git a/src/main/java/com/networknt/schema/EnumValidator.java b/src/main/java/com/networknt/schema/EnumValidator.java index 70a28ef46..28bc58241 100644 --- a/src/main/java/com/networknt/schema/EnumValidator.java +++ b/src/main/java/com/networknt/schema/EnumValidator.java @@ -17,6 +17,7 @@ package com.networknt.schema; import com.fasterxml.jackson.databind.JsonNode; +import com.fasterxml.jackson.databind.node.DecimalNode; import com.fasterxml.jackson.databind.node.NullNode; import org.slf4j.Logger; @@ -44,7 +45,12 @@ public EnumValidator(String schemaPath, JsonNode schemaNode, JsonSchema parentSc String separator = ""; for (JsonNode n : schemaNode) { - nodes.add(n); + if(n.isNumber()) { + // convert to DecimalNode for number comparison + nodes.add(DecimalNode.valueOf(n.decimalValue())); + } else { + nodes.add(n); + } sb.append(separator); sb.append(n.asText()); @@ -77,7 +83,7 @@ public Set validate(JsonNode node, JsonNode rootNode, String debug(logger, node, rootNode, at); Set errors = new LinkedHashSet(); - + if(node.isNumber()) node = DecimalNode.valueOf(node.decimalValue()); if (!nodes.contains(node) && !(config.isTypeLoose() && isTypeLooseContainsInEnum(node))) { errors.add(buildValidationMessage(at, error)); } diff --git a/src/test/java/com/networknt/schema/V201909JsonSchemaTest.java b/src/test/java/com/networknt/schema/V201909JsonSchemaTest.java index 0652a5eb8..f5118e044 100644 --- a/src/test/java/com/networknt/schema/V201909JsonSchemaTest.java +++ b/src/test/java/com/networknt/schema/V201909JsonSchemaTest.java @@ -289,7 +289,6 @@ public void testDependenciesValidator() throws Exception { } @Test - @Ignore public void testEnumValidator() throws Exception { runTestFile("draft2019-09/enum.json"); } diff --git a/src/test/java/com/networknt/schema/V6JsonSchemaTest.java b/src/test/java/com/networknt/schema/V6JsonSchemaTest.java index 788f56976..49345022a 100644 --- a/src/test/java/com/networknt/schema/V6JsonSchemaTest.java +++ b/src/test/java/com/networknt/schema/V6JsonSchemaTest.java @@ -183,7 +183,6 @@ public void testDependenciesValidator() throws Exception { } @Test - @Ignore public void testEnumValidator() throws Exception { runTestFile("draft6/enum.json"); } diff --git a/src/test/java/com/networknt/schema/V7JsonSchemaTest.java b/src/test/java/com/networknt/schema/V7JsonSchemaTest.java index 27a10d483..99730c1a7 100644 --- a/src/test/java/com/networknt/schema/V7JsonSchemaTest.java +++ b/src/test/java/com/networknt/schema/V7JsonSchemaTest.java @@ -285,7 +285,6 @@ public void testDependenciesValidator() throws Exception { } @Test - @Ignore public void testEnumValidator() throws Exception { runTestFile("draft7/enum.json"); }