From 0282bc73315268f2923e10e5daad41dfb1770a83 Mon Sep 17 00:00:00 2001 From: irengrig Date: Tue, 31 Jan 2017 21:05:55 +0100 Subject: [PATCH] json schema, when reference can not be resolved, do not stop reading the schema --> fixes navigation by $ref in schemas with any misspelled references --- .../jsonSchema/impl/JsonSchemaReader.java | 6 ++-- .../jsonSchema/impl/JsonSchemaWalker.java | 4 +-- .../JsonSchemaCrossReferencesTest.java | 30 +++++++++++++++++++ .../withIncorrectReferenceSchema.json | 23 ++++++++++++++ 4 files changed, 58 insertions(+), 5 deletions(-) create mode 100644 json/tests/testData/jsonSchema/crossReferences/withIncorrectReferenceSchema.json diff --git a/json/src/com/jetbrains/jsonSchema/impl/JsonSchemaReader.java b/json/src/com/jetbrains/jsonSchema/impl/JsonSchemaReader.java index a6035ea691dc..08cc1925e22c 100644 --- a/json/src/com/jetbrains/jsonSchema/impl/JsonSchemaReader.java +++ b/json/src/com/jetbrains/jsonSchema/impl/JsonSchemaReader.java @@ -179,7 +179,7 @@ public class JsonSchemaReader { return !ref.startsWith("#/"); } - @NotNull + @Nullable public static JsonSchemaObject findRelativeDefinition(@NotNull String ref, @NotNull JsonSchemaObject root) { if ("#".equals(ref)) { return root; @@ -189,7 +189,7 @@ public class JsonSchemaReader { final String[] parts = ref.split("/"); JsonSchemaObject current = root; for (int i = 0; i < parts.length; i++) { - if (current == null) throw new RuntimeException("Incorrect reference: " + ref); + if (current == null) return null; final String part = parts[i]; if ("definitions".equals(part)) { if (i == (parts.length - 1)) throw new RuntimeException("Incorrect definition reference: " + ref); @@ -206,7 +206,7 @@ public class JsonSchemaReader { current = current.getDefinitions().get(part); } - if (current == null) throw new RuntimeException("Incorrect reference: " + ref); + if (current == null) return null; return current; } } \ No newline at end of file diff --git a/json/src/com/jetbrains/jsonSchema/impl/JsonSchemaWalker.java b/json/src/com/jetbrains/jsonSchema/impl/JsonSchemaWalker.java index 1b70ac9cb687..87b9db1dcbff 100644 --- a/json/src/com/jetbrains/jsonSchema/impl/JsonSchemaWalker.java +++ b/json/src/com/jetbrains/jsonSchema/impl/JsonSchemaWalker.java @@ -229,10 +229,10 @@ public class JsonSchemaWalker { private static List gatherSchemas(JsonSchemaObject schema) { List list = new ArrayList<>(); + list.add(schema); if (schema.getAllOf() != null) { - list = schema.getAllOf(); + list.addAll(schema.getAllOf()); } else { - list.add(schema); if (schema.getAnyOf() != null) list.addAll(schema.getAnyOf()); if (schema.getOneOf() != null) list.addAll(schema.getOneOf()); diff --git a/json/tests/test/com/jetbrains/jsonSchema/JsonSchemaCrossReferencesTest.java b/json/tests/test/com/jetbrains/jsonSchema/JsonSchemaCrossReferencesTest.java index 16471b16e212..df07215f2d7e 100644 --- a/json/tests/test/com/jetbrains/jsonSchema/JsonSchemaCrossReferencesTest.java +++ b/json/tests/test/com/jetbrains/jsonSchema/JsonSchemaCrossReferencesTest.java @@ -538,4 +538,34 @@ public class JsonSchemaCrossReferencesTest extends JsonSchemaHeavyAbstractTest { } }); } + + public void testNavigateToDefinitionByRefInFileWithIncorrectReference() throws Exception { + skeleton(new Callback() { + @Override + public void registerSchemes() { + final String moduleDir = getModuleDir(getProject()); + addSchema(new JsonSchemaMappingsConfigurationBase.SchemaInfo("one", moduleDir + "/withIncorrectReferenceSchema.json", false, Collections.emptyList())); + } + + @Override + public void configureFiles() throws Exception { + configureByFiles(null, "withIncorrectReferenceSchema.json"); + } + + @Override + public void doCheck() { + int offset = myEditor.getCaretModel().getPrimaryCaret().getOffset(); + final PsiReference referenceAt = myFile.findReferenceAt(offset); + Assert.assertNotNull(referenceAt); + final PsiElement resolve = referenceAt.resolve(); + Assert.assertNotNull(resolve); + Assert.assertEquals("\"midia\"", resolve.getText()); + final PsiElement parent = resolve.getParent(); + Assert.assertTrue(parent instanceof JsonProperty); + Assert.assertEquals("midia", ((JsonProperty) parent).getName()); + Assert.assertTrue(parent.getParent().getParent() instanceof JsonProperty); + Assert.assertEquals("definitions", ((JsonProperty) parent.getParent().getParent()).getName()); + } + }); + } } diff --git a/json/tests/testData/jsonSchema/crossReferences/withIncorrectReferenceSchema.json b/json/tests/testData/jsonSchema/crossReferences/withIncorrectReferenceSchema.json new file mode 100644 index 000000000000..814fdff48569 --- /dev/null +++ b/json/tests/testData/jsonSchema/crossReferences/withIncorrectReferenceSchema.json @@ -0,0 +1,23 @@ +{ + "definitions": { + "midia": { + "properties": { + "mittel" : { + "type": ["integer", "boolean"], + "description": "this is found!", + "enum": [1,2, false] + } + } + } + }, + "properties": { + "call": { + "$ref": "#/definitions/midia" + } + }, + "allOf": [ + { + "$ref": "#/definitions/middle" + } + ] +} \ No newline at end of file