From c074f3fb830803dfeb81c20b30bdc4535ad0abbb Mon Sep 17 00:00:00 2001 From: irengrig Date: Thu, 25 Feb 2016 15:55:04 +0100 Subject: [PATCH] WEB-20519 JSON schema: non-string element in `required` section hangs IDE --- .../jsonSchema/impl/JsonSchemaReader.java | 4 +- .../jsonSchema/impl/JsonSchemaReadTest.java | 42 +++++++++++++++++++ .../testData/jsonSchema/WithWrongItems.json | 9 ++++ .../jsonSchema/WithWrongRequired.json | 3 ++ 4 files changed, 57 insertions(+), 1 deletion(-) create mode 100644 json/tests/testData/jsonSchema/WithWrongItems.json create mode 100644 json/tests/testData/jsonSchema/WithWrongRequired.json diff --git a/json/src/com/jetbrains/jsonSchema/impl/JsonSchemaReader.java b/json/src/com/jetbrains/jsonSchema/impl/JsonSchemaReader.java index d99ed1c6754c..43d06da54d44 100644 --- a/json/src/com/jetbrains/jsonSchema/impl/JsonSchemaReader.java +++ b/json/src/com/jetbrains/jsonSchema/impl/JsonSchemaReader.java @@ -411,6 +411,8 @@ public class JsonSchemaReader { while (in.peek() != JsonToken.END_ARRAY) { if (in.peek() == JsonToken.STRING) { required.add(in.nextString()); + } else { + in.skipValue(); } } in.endArray(); @@ -479,7 +481,7 @@ public class JsonSchemaReader { while (in.peek() != JsonToken.END_ARRAY) { if (in.peek() == JsonToken.BEGIN_OBJECT) { list.add(readInnerObject(in)); - } + } else in.skipValue(); } in.endArray(); object.setItemsSchemaList(list); diff --git a/json/tests/test/com/jetbrains/jsonSchema/impl/JsonSchemaReadTest.java b/json/tests/test/com/jetbrains/jsonSchema/impl/JsonSchemaReadTest.java index 0de77d8d0258..6c069a932090 100644 --- a/json/tests/test/com/jetbrains/jsonSchema/impl/JsonSchemaReadTest.java +++ b/json/tests/test/com/jetbrains/jsonSchema/impl/JsonSchemaReadTest.java @@ -1,12 +1,17 @@ package com.jetbrains.jsonSchema.impl; import com.intellij.testFramework.PlatformTestUtil; +import com.intellij.util.concurrency.Semaphore; import org.junit.Assert; import org.junit.Test; import java.io.File; import java.io.FileReader; +import java.io.IOException; import java.util.List; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.atomic.AtomicBoolean; +import java.util.concurrent.atomic.AtomicReference; /** * @author Irina.Chernushina on 8/29/2015. @@ -60,4 +65,41 @@ public class JsonSchemaReadTest { final JsonSchemaObject read = reader.read(new FileReader(file)); Assert.assertTrue(read.getDefinitions().get("common").getProperties().containsKey("id")); } + + @Test + public void testReadSchemaWithWrongRequired() throws Exception { + testSchemaReadNotHung(new File(PlatformTestUtil.getCommunityPath(), "json/tests/testData/jsonSchema/withWrongRequired.json")); + } + + @Test + public void testReadSchemaWithWrongItems() throws Exception { + testSchemaReadNotHung(new File(PlatformTestUtil.getCommunityPath(), "json/tests/testData/jsonSchema/withWrongItems.json")); + } + + private static void testSchemaReadNotHung(final File file) throws IOException { + Assert.assertTrue(file.exists()); + + final AtomicBoolean done = new AtomicBoolean(); + final AtomicReference error = new AtomicReference<>(); + final Semaphore semaphore = new Semaphore(); + semaphore.down(); + new Thread(new Runnable() { + @Override + public void run() { + final JsonSchemaReader reader = new JsonSchemaReader(); + try { + reader.read(new FileReader(file)); + done.set(true); + } + catch (IOException e) { + error.set(e); + } finally { + semaphore.up(); + } + } + }, "read test json schema " + file.getName()).start(); + semaphore.waitFor(TimeUnit.SECONDS.toMillis(60)); + if (error.get() != null) throw error.get(); + Assert.assertTrue("Reading test schema hung!", done.get()); + } } diff --git a/json/tests/testData/jsonSchema/WithWrongItems.json b/json/tests/testData/jsonSchema/WithWrongItems.json new file mode 100644 index 000000000000..11905c6e427e --- /dev/null +++ b/json/tests/testData/jsonSchema/WithWrongItems.json @@ -0,0 +1,9 @@ +{ + "properties": { + "a": { + "items": [ + 1 + ] + } + } +} \ No newline at end of file diff --git a/json/tests/testData/jsonSchema/WithWrongRequired.json b/json/tests/testData/jsonSchema/WithWrongRequired.json new file mode 100644 index 000000000000..fd2933d2af4c --- /dev/null +++ b/json/tests/testData/jsonSchema/WithWrongRequired.json @@ -0,0 +1,3 @@ +{ + "required": [{}] +} \ No newline at end of file