Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion src/main/java/com/networknt/schema/SchemaValidatorsConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ public class SchemaValidatorsConfig {
*/
private boolean javaSemantics;

/**
* When set to true, can interpret round doubles as integers
*/
private boolean losslessNarrowing;

/**
* Map of public, normally internet accessible schema URLs to alternate locations; this allows for offline
* validation of schemas that refer to public URLs. This is merged with any mappings the {@link JsonSchemaFactory}
Expand Down Expand Up @@ -192,5 +197,12 @@ public CollectorContext getCollectorContext() {
public void setCollectorContext(CollectorContext collectorContext) {
this.collectorContext = collectorContext;
}


public boolean isLosslessNarrowing() {
return losslessNarrowing;
}

public void setLosslessNarrowing(boolean losslessNarrowing) {
this.losslessNarrowing = losslessNarrowing;
}
}
2 changes: 2 additions & 0 deletions src/main/java/com/networknt/schema/TypeFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ public static JsonType getValueNodeType(JsonNode node, SchemaValidatorsConfig co
if (node.isNumber())
if (config.isJavaSemantics() && node.canConvertToLong() && (node.asText().indexOf('.') == -1))
return JsonType.INTEGER;
else if (config.isLosslessNarrowing() && node.asText().endsWith(".0"))
return JsonType.INTEGER;
else
return JsonType.NUMBER;
if (node.isBoolean())
Expand Down
20 changes: 20 additions & 0 deletions src/test/java/com/networknt/schema/TypeFactoryTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,24 @@ public void testValidIntegralValuesWithoutJavaSemantics() {
getValueNodeType(DecimalNode.valueOf(new BigDecimal(validValue)), schemaValidatorsConfig));
}
}

@Test
public void testWithLosslessNarrowing() {
schemaValidatorsConfig.setLosslessNarrowing(true);
assertSame(validValue, JsonType.INTEGER,
getValueNodeType(DecimalNode.valueOf(new BigDecimal("1.0")), schemaValidatorsConfig));

assertSame(validValue, JsonType.NUMBER,
getValueNodeType(DecimalNode.valueOf(new BigDecimal("1.5")), schemaValidatorsConfig));
}

@Test
public void testWithoutLosslessNarrowing() {
schemaValidatorsConfig.setLosslessNarrowing(false);
assertSame(validValue, JsonType.NUMBER,
getValueNodeType(DecimalNode.valueOf(new BigDecimal("1.0")), schemaValidatorsConfig));

assertSame(validValue, JsonType.NUMBER,
getValueNodeType(DecimalNode.valueOf(new BigDecimal("1.5")), schemaValidatorsConfig));
}
}