From 458b78b2149c521135f37185d20a7f3bbecc6d56 Mon Sep 17 00:00:00 2001 From: Anton Lobov Date: Mon, 28 Jan 2019 12:55:25 +0100 Subject: [PATCH] WEB-36973 JSON validation doesn't allow large integer values, although it should --- .../impl/JsonSchemaAnnotatorChecker.java | 28 ++++++++++--------- .../jsonSchema/impl/JsonSchemaType.java | 17 +++++++++-- .../JsonSchemaHighlightingTest.java | 13 +++++++++ 3 files changed, 42 insertions(+), 16 deletions(-) diff --git a/json/src/com/jetbrains/jsonSchema/impl/JsonSchemaAnnotatorChecker.java b/json/src/com/jetbrains/jsonSchema/impl/JsonSchemaAnnotatorChecker.java index fce28ba0e996..8d5aa28fc7aa 100644 --- a/json/src/com/jetbrains/jsonSchema/impl/JsonSchemaAnnotatorChecker.java +++ b/json/src/com/jetbrains/jsonSchema/impl/JsonSchemaAnnotatorChecker.java @@ -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); diff --git a/json/src/com/jetbrains/jsonSchema/impl/JsonSchemaType.java b/json/src/com/jetbrains/jsonSchema/impl/JsonSchemaType.java index 98351d18d7e2..312e984473be 100644 --- a/json/src/com/jetbrains/jsonSchema/impl/JsonSchemaType.java +++ b/json/src/com/jetbrains/jsonSchema/impl/JsonSchemaType.java @@ -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; + } } } diff --git a/json/tests/test/com/jetbrains/jsonSchema/JsonSchemaHighlightingTest.java b/json/tests/test/com/jetbrains/jsonSchema/JsonSchemaHighlightingTest.java index c2d1fbebe711..0a70e70d3a24 100644 --- a/json/tests/test/com/jetbrains/jsonSchema/JsonSchemaHighlightingTest.java +++ b/json/tests/test/com/jetbrains/jsonSchema/JsonSchemaHighlightingTest.java @@ -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" + + "}"); + } }