json schema, when reference can not be resolved, do not stop reading the schema --> fixes navigation by $ref in schemas with any misspelled references

This commit is contained in:
irengrig
2017-01-31 21:07:13 +01:00
parent e72bbca2bf
commit 0282bc7331
4 changed files with 58 additions and 5 deletions
@@ -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;
}
}
@@ -229,10 +229,10 @@ public class JsonSchemaWalker {
private static List<JsonSchemaObject> gatherSchemas(JsonSchemaObject schema) {
List<JsonSchemaObject> 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());
@@ -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());
}
});
}
}
@@ -0,0 +1,23 @@
{
"definitions": {
"midia": {
"properties": {
"mittel" : {
"type": ["integer", "boolean"],
"description": "this is found!",
"enum": [1,2, false]
}
}
}
},
"properties": {
"call": {
"$ref": "#/definitions/<caret>midia"
}
},
"allOf": [
{
"$ref": "#/definitions/middle"
}
]
}