Skip to content

Improve type validation of integrals #496

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 14, 2022
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ and this project adheres to [Semantic Versioning](http://semver.org/).

### Changed

- fixes #446 maxLength and minLength do not work when a number with a zero decimal is used

## 1.0.65 - 2022-01-07

### Changed
Expand Down
8 changes: 8 additions & 0 deletions doc/config.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,11 @@ When set to true, use Java-specific semantics rather than native JavaScript sema
For example, if the node type is `number` per JS semantics where the value can be losslesly interpreted as `java.lang.Long`, the validator would use `integer` as the node type instead of `number`. This is useful when schema type is `integer`, since validation would fail otherwise.

For more details, please refer to this [issue](https://github.com/networknt/json-schema-validator/issues/334).

* losslessNarrowing

When set to true, can interpret round doubles as integers.

Note that setting `javaSemantics = true` will achieve the same functionality at this time.

For more details, please refer to this [issue](https://github.com/networknt/json-schema-validator/issues/344).
4 changes: 2 additions & 2 deletions src/main/java/com/networknt/schema/TypeFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,9 @@ public static JsonType getValueNodeType(JsonNode node, SchemaValidatorsConfig co
if (node.isIntegralNumber())
return JsonType.INTEGER;
if (node.isNumber())
if (config != null && config.isJavaSemantics() && node.canConvertToLong() && (node.asText().indexOf('.') == -1))
if (config != null && config.isJavaSemantics() && node.canConvertToExactIntegral())
return JsonType.INTEGER;
else if (config!= null && config.isLosslessNarrowing() && node.asText().endsWith(".0"))
else if (config != null && config.isLosslessNarrowing() && node.canConvertToExactIntegral())
return JsonType.INTEGER;
else
return JsonType.NUMBER;
Expand Down
22 changes: 18 additions & 4 deletions src/test/java/com/networknt/schema/TypeFactoryTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,29 +28,43 @@ public class TypeFactoryTest {

private static final String[] validIntegralValues = {
"1", "-1", "0E+1", "0E1", "-0E+1", "-0E1", "10.1E+1", "10.1E1", "-10.1E+1", "-10.1E1", "1E+0", "1E-0",
"1E0", "1E18", "9223372036854775807", "-9223372036854775808"
"1E0", "1E18", "9223372036854775807", "-9223372036854775808", "1.0", "1.00", "-1.0", "-1.00"
};

private static final String[] validNonIntegralNumberValues = {
"1.1", "-1.1", "1.10"
};

private final SchemaValidatorsConfig schemaValidatorsConfig = new SchemaValidatorsConfig();

@Test
public void testValidIntegralValuesWithJavaSemantics() {
public void testIntegralValuesWithJavaSemantics() {
schemaValidatorsConfig.setJavaSemantics(true);
for (String validValue : validIntegralValues) {
assertSame(JsonType.INTEGER,
getValueNodeType(DecimalNode.valueOf(new BigDecimal(validValue)), schemaValidatorsConfig),
validValue);
}
for (String validValue : validNonIntegralNumberValues) {
assertSame(JsonType.NUMBER,
getValueNodeType(DecimalNode.valueOf(new BigDecimal(validValue)), schemaValidatorsConfig),
validValue);
}
}

@Test
public void testValidIntegralValuesWithoutJavaSemantics() {
public void testIntegralValuesWithoutJavaSemantics() {
schemaValidatorsConfig.setJavaSemantics(false);
for (String validValue : validIntegralValues) {
assertSame(JsonType.NUMBER,
getValueNodeType(DecimalNode.valueOf(new BigDecimal(validValue)), schemaValidatorsConfig),
validValue);
}
for (String validValue : validNonIntegralNumberValues) {
assertSame(JsonType.NUMBER,
getValueNodeType(DecimalNode.valueOf(new BigDecimal(validValue)), schemaValidatorsConfig),
validValue);
}
}


Expand Down Expand Up @@ -82,4 +96,4 @@ public void testWithoutLosslessNarrowing() {
}

}
}
}