Skip to content

Commit 46cc360

Browse files
authored
Add lossless narrowing convertion (#379)
1 parent ca1847a commit 46cc360

File tree

3 files changed

+35
-1
lines changed

3 files changed

+35
-1
lines changed

src/main/java/com/networknt/schema/SchemaValidatorsConfig.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,11 @@ public class SchemaValidatorsConfig {
4242
*/
4343
private boolean javaSemantics;
4444

45+
/**
46+
* When set to true, can interpret round doubles as integers
47+
*/
48+
private boolean losslessNarrowing;
49+
4550
/**
4651
* Map of public, normally internet accessible schema URLs to alternate locations; this allows for offline
4752
* validation of schemas that refer to public URLs. This is merged with any mappings the {@link JsonSchemaFactory}
@@ -192,5 +197,12 @@ public CollectorContext getCollectorContext() {
192197
public void setCollectorContext(CollectorContext collectorContext) {
193198
this.collectorContext = collectorContext;
194199
}
195-
200+
201+
public boolean isLosslessNarrowing() {
202+
return losslessNarrowing;
203+
}
204+
205+
public void setLosslessNarrowing(boolean losslessNarrowing) {
206+
this.losslessNarrowing = losslessNarrowing;
207+
}
196208
}

src/main/java/com/networknt/schema/TypeFactory.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,8 @@ public static JsonType getValueNodeType(JsonNode node, SchemaValidatorsConfig co
7474
if (node.isNumber())
7575
if (config.isJavaSemantics() && node.canConvertToLong() && (node.asText().indexOf('.') == -1))
7676
return JsonType.INTEGER;
77+
else if (config.isLosslessNarrowing() && node.asText().endsWith(".0"))
78+
return JsonType.INTEGER;
7779
else
7880
return JsonType.NUMBER;
7981
if (node.isBoolean())

src/test/java/com/networknt/schema/TypeFactoryTest.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,4 +50,24 @@ public void testValidIntegralValuesWithoutJavaSemantics() {
5050
getValueNodeType(DecimalNode.valueOf(new BigDecimal(validValue)), schemaValidatorsConfig));
5151
}
5252
}
53+
54+
@Test
55+
public void testWithLosslessNarrowing() {
56+
schemaValidatorsConfig.setLosslessNarrowing(true);
57+
assertSame(validValue, JsonType.INTEGER,
58+
getValueNodeType(DecimalNode.valueOf(new BigDecimal("1.0")), schemaValidatorsConfig));
59+
60+
assertSame(validValue, JsonType.NUMBER,
61+
getValueNodeType(DecimalNode.valueOf(new BigDecimal("1.5")), schemaValidatorsConfig));
62+
}
63+
64+
@Test
65+
public void testWithoutLosslessNarrowing() {
66+
schemaValidatorsConfig.setLosslessNarrowing(false);
67+
assertSame(validValue, JsonType.NUMBER,
68+
getValueNodeType(DecimalNode.valueOf(new BigDecimal("1.0")), schemaValidatorsConfig));
69+
70+
assertSame(validValue, JsonType.NUMBER,
71+
getValueNodeType(DecimalNode.valueOf(new BigDecimal("1.5")), schemaValidatorsConfig));
72+
}
5373
}

0 commit comments

Comments
 (0)