WEB-36973 JSON validation doesn't allow large integer values, although it should

This commit is contained in:
Anton Lobov
2019-01-28 13:52:56 +01:00
parent 7d735c871e
commit 458b78b214
3 changed files with 42 additions and 16 deletions
@@ -727,11 +727,9 @@ class JsonSchemaAnnotatorChecker {
}
private void checkString(PsiElement propValue, JsonSchemaObject schema) {
final JsonLikePsiWalker walker = JsonLikePsiWalker.getWalker(propValue, schema);
assert walker != null;
JsonValueAdapter adapter = walker.createValueAdapter(propValue);
if (adapter != null && !adapter.shouldCheckAsValue()) return;
final String value = StringUtil.unquoteString(walker.getNodeTextForValidation(propValue));
String v = getValue(propValue, schema);
if (v == null) return;
final String value = StringUtil.unquoteString(v);
if (schema.getMinLength() != null) {
if (value.length() < schema.getMinLength()) {
error("String is shorter than " + schema.getMinLength(), propValue, JsonErrorPriority.LOW_PRIORITY);
@@ -760,18 +758,22 @@ class JsonSchemaAnnotatorChecker {
}*/
}
private void checkNumber(PsiElement propValue, JsonSchemaObject schema, JsonSchemaType schemaType) {
Number value;
@Nullable
private static String getValue(PsiElement propValue, JsonSchemaObject schema) {
final JsonLikePsiWalker walker = JsonLikePsiWalker.getWalker(propValue, schema);
assert walker != null;
JsonValueAdapter adapter = walker.createValueAdapter(propValue);
if (adapter != null && !adapter.shouldCheckAsValue()) return;
String valueText = walker.getNodeTextForValidation(propValue);
if (adapter != null && !adapter.shouldCheckAsValue()) return null;
return walker.getNodeTextForValidation(propValue);
}
private void checkNumber(PsiElement propValue, JsonSchemaObject schema, JsonSchemaType schemaType) {
Number value;
String valueText = getValue(propValue, schema);
if (valueText == null) return;
if (JsonSchemaType._integer.equals(schemaType)) {
try {
value = Integer.valueOf(valueText);
}
catch (NumberFormatException e) {
value = JsonSchemaType.getIntegerValue(valueText);
if (value == null) {
error("Integer value expected", propValue,
JsonValidationError.FixableIssueKind.TypeMismatch,
new JsonValidationError.TypeMismatchIssueData(new JsonSchemaType[]{schemaType}), JsonErrorPriority.TYPE_MISMATCH);
@@ -4,6 +4,8 @@ import com.jetbrains.jsonSchema.extension.adapters.JsonValueAdapter;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import java.math.BigInteger;
/**
* @author Irina.Chernushina on 7/15/2015.
*/
@@ -68,12 +70,21 @@ public enum JsonSchemaType {
}
public static boolean isInteger(@NotNull String text) {
return getIntegerValue(text) != null;
}
@Nullable
public static Number getIntegerValue(@NotNull String text) {
try {
Integer.parseInt(text);
return true;
return Integer.parseInt(text);
}
catch (NumberFormatException e) {
return false;
try {
return BigInteger.valueOf(Long.parseLong(text));
}
catch (NumberFormatException e2) {
return null;
}
}
}
@@ -1050,4 +1050,17 @@ public class JsonSchemaHighlightingTest extends JsonSchemaHighlightingTestBase {
String inputText = FileUtil.loadFile(new File(getTestDataPath() + "/exoticProps.json"));
doTest(schemaText, inputText);
}
public void testLargeInt() throws Exception {
// currently we limit it by Java Long range, should be sufficient as per RFC 7159
doTest("{\n" +
" \"properties\": {\n" +
" \"x\": {\n" +
" \"type\": \"integer\"\n" +
" }\n" +
" }\n" +
"}", "{\n" +
" \"x\": 9223372036854775807\n" +
"}");
}
}