From 48d7dbff9c666ffef33ca38ee88666318d3d5902 Mon Sep 17 00:00:00 2001 From: Anton Lobov Date: Thu, 14 Mar 2019 15:10:45 +0100 Subject: [PATCH] WEB-37789 JSON Schema documentation for additionalProperties not working don't know why this flag for skipping additionalProperties was needed, it only breaks some features but adds no value --- .../json/JsonSpellcheckerStrategy.java | 2 +- .../impl/JsonSchemaAnnotatorChecker.java | 2 +- .../impl/JsonSchemaBasedLanguageInjector.java | 2 +- .../impl/JsonSchemaCompletionContributor.java | 2 +- .../impl/JsonSchemaComplianceChecker.java | 2 +- .../impl/JsonSchemaDocumentationProvider.java | 2 +- .../JsonSchemaGotoDeclarationHandler.java | 4 +-- .../jsonSchema/impl/JsonSchemaResolver.java | 11 +++----- .../impl/JsonSchemaVariantsTreeBuilder.java | 26 ++++++++---------- .../JsonSchemaDeprecationInspection.java | 2 +- .../JsonSchemaDocumentationTest.java | 4 +++ .../documentation/additionalPropertyDocs.html | 1 + .../documentation/additionalPropertyDocs.json | 8 ++++++ .../additionalPropertyDocsSchema.json | 27 +++++++++++++++++++ .../additionalPropertyDocs_short.html | 1 + .../YamlJsonSchemaDeprecationInspection.java | 2 +- .../YamlJsonSchemaGotoDeclarationHandler.java | 4 +-- 17 files changed, 65 insertions(+), 37 deletions(-) create mode 100644 json/tests/testData/jsonSchema/documentation/additionalPropertyDocs.html create mode 100644 json/tests/testData/jsonSchema/documentation/additionalPropertyDocs.json create mode 100644 json/tests/testData/jsonSchema/documentation/additionalPropertyDocsSchema.json create mode 100644 json/tests/testData/jsonSchema/documentation/additionalPropertyDocs_short.html diff --git a/json/src/com/intellij/json/JsonSpellcheckerStrategy.java b/json/src/com/intellij/json/JsonSpellcheckerStrategy.java index aaf2efae7bcf..c690a31ab9a0 100644 --- a/json/src/com/intellij/json/JsonSpellcheckerStrategy.java +++ b/json/src/com/intellij/json/JsonSpellcheckerStrategy.java @@ -76,7 +76,7 @@ public class JsonSpellcheckerStrategy extends SpellcheckingStrategy { final JsonPointerPosition position = walker.findPosition(checkable, isName == ThreeState.NO); if (position == null || position.isEmpty() && isName == ThreeState.NO) return false; - final Collection schemas = new JsonSchemaResolver(project, rootSchema, false, position).resolve(); + final Collection schemas = new JsonSchemaResolver(project, rootSchema, position).resolve(); if (schemas.isEmpty()) return false; return schemas.stream().anyMatch(s -> s.getProperties().keySet().contains(value) diff --git a/json/src/com/jetbrains/jsonSchema/impl/JsonSchemaAnnotatorChecker.java b/json/src/com/jetbrains/jsonSchema/impl/JsonSchemaAnnotatorChecker.java index 6dc8cd86cbf5..cf984280f1b3 100644 --- a/json/src/com/jetbrains/jsonSchema/impl/JsonSchemaAnnotatorChecker.java +++ b/json/src/com/jetbrains/jsonSchema/impl/JsonSchemaAnnotatorChecker.java @@ -281,7 +281,7 @@ class JsonSchemaAnnotatorChecker { } final JsonPointerPosition step = JsonPointerPosition.createSingleProperty(name); - final Pair pair = doSingleStep(step, schema, true, false); + final Pair pair = doSingleStep(step, schema, false); if (ThreeState.NO.equals(pair.getFirst()) && !set.contains(name)) { error(JsonBundle.message("json.schema.annotation.not.allowed.property", name), property.getDelegate(), JsonValidationError.FixableIssueKind.ProhibitedProperty, diff --git a/json/src/com/jetbrains/jsonSchema/impl/JsonSchemaBasedLanguageInjector.java b/json/src/com/jetbrains/jsonSchema/impl/JsonSchemaBasedLanguageInjector.java index 0682171cf942..82071a01d0e3 100644 --- a/json/src/com/jetbrains/jsonSchema/impl/JsonSchemaBasedLanguageInjector.java +++ b/json/src/com/jetbrains/jsonSchema/impl/JsonSchemaBasedLanguageInjector.java @@ -36,7 +36,7 @@ public class JsonSchemaBasedLanguageInjector extends JsonSchemaInjectorBase { if (walker == null || walker.isName(context) != ThreeState.NO) return null; final JsonPointerPosition position = walker.findPosition(context, true); if (position == null || position.isEmpty()) return null; - final Collection schemas = new JsonSchemaResolver(project, schemaObject, false, position).resolve(); + final Collection schemas = new JsonSchemaResolver(project, schemaObject, position).resolve(); if (schemas.size() != 1) return null; JsonSchemaObject object = schemas.iterator().next(); String injection = object.getLanguageInjection(); diff --git a/json/src/com/jetbrains/jsonSchema/impl/JsonSchemaCompletionContributor.java b/json/src/com/jetbrains/jsonSchema/impl/JsonSchemaCompletionContributor.java index 7a84a4e80a78..2e74fc52cabb 100644 --- a/json/src/com/jetbrains/jsonSchema/impl/JsonSchemaCompletionContributor.java +++ b/json/src/com/jetbrains/jsonSchema/impl/JsonSchemaCompletionContributor.java @@ -172,7 +172,7 @@ public class JsonSchemaCompletionContributor extends CompletionContributor { final JsonPointerPosition position = myWalker.findPosition(checkable, isName == ThreeState.NO); if (position == null || position.isEmpty() && isName == ThreeState.NO) return; - final Collection schemas = new JsonSchemaResolver(myProject, myRootSchema, false, position).resolve(); + final Collection schemas = new JsonSchemaResolver(myProject, myRootSchema, position).resolve(); final Set knownNames = ContainerUtil.newHashSet(); // too long here, refactor further schemas.forEach(schema -> { diff --git a/json/src/com/jetbrains/jsonSchema/impl/JsonSchemaComplianceChecker.java b/json/src/com/jetbrains/jsonSchema/impl/JsonSchemaComplianceChecker.java index 4b239844ae17..8bca7942cd93 100644 --- a/json/src/com/jetbrains/jsonSchema/impl/JsonSchemaComplianceChecker.java +++ b/json/src/com/jetbrains/jsonSchema/impl/JsonSchemaComplianceChecker.java @@ -61,7 +61,7 @@ public class JsonSchemaComplianceChecker { if (firstProp != null) { final JsonPointerPosition position = myWalker.findPosition(firstProp.getDelegate(), true); if (position == null || position.isEmpty()) return; - final MatchResult result = new JsonSchemaResolver(project, myRootSchema, false, position).detailedResolve(); + final MatchResult result = new JsonSchemaResolver(project, myRootSchema, position).detailedResolve(); for (JsonValueAdapter value : firstProp.getValues()) { createWarnings(JsonSchemaAnnotatorChecker.checkByMatchResult(project, value, result, myOptions)); } diff --git a/json/src/com/jetbrains/jsonSchema/impl/JsonSchemaDocumentationProvider.java b/json/src/com/jetbrains/jsonSchema/impl/JsonSchemaDocumentationProvider.java index d7d3a3fbade9..c2723ecc2fe6 100644 --- a/json/src/com/jetbrains/jsonSchema/impl/JsonSchemaDocumentationProvider.java +++ b/json/src/com/jetbrains/jsonSchema/impl/JsonSchemaDocumentationProvider.java @@ -93,7 +93,7 @@ public class JsonSchemaDocumentationProvider implements DocumentationProvider { position.replaceStep(position.size() - 1, forcedPropName); } } - final Collection schemas = new JsonSchemaResolver(element.getProject(), rootSchema, true, position).resolve(); + final Collection schemas = new JsonSchemaResolver(element.getProject(), rootSchema, position).resolve(); String htmlDescription = null; boolean deprecated = false; diff --git a/json/src/com/jetbrains/jsonSchema/impl/JsonSchemaGotoDeclarationHandler.java b/json/src/com/jetbrains/jsonSchema/impl/JsonSchemaGotoDeclarationHandler.java index 69b93f02fdb4..49da821fd817 100644 --- a/json/src/com/jetbrains/jsonSchema/impl/JsonSchemaGotoDeclarationHandler.java +++ b/json/src/com/jetbrains/jsonSchema/impl/JsonSchemaGotoDeclarationHandler.java @@ -34,9 +34,7 @@ public class JsonSchemaGotoDeclarationHandler implements GotoDeclarationHandler if (steps == null) return null; final JsonSchemaObject schemaObject = service.getSchemaObject(file); if (schemaObject != null) { - final PsiElement target = new JsonSchemaResolver(sourceElement.getProject(), schemaObject, false, steps) - .findNavigationTarget(((JsonProperty)parent).getValue(), - JsonSchemaService.isSchemaFile(containingFile)); + final PsiElement target = new JsonSchemaResolver(sourceElement.getProject(), schemaObject, steps).findNavigationTarget(((JsonProperty)parent).getValue()); if (target != null) { return new PsiElement[] {target}; } diff --git a/json/src/com/jetbrains/jsonSchema/impl/JsonSchemaResolver.java b/json/src/com/jetbrains/jsonSchema/impl/JsonSchemaResolver.java index fe3296190818..762789c04004 100644 --- a/json/src/com/jetbrains/jsonSchema/impl/JsonSchemaResolver.java +++ b/json/src/com/jetbrains/jsonSchema/impl/JsonSchemaResolver.java @@ -45,28 +45,24 @@ import static com.jetbrains.jsonSchema.impl.JsonSchemaAnnotatorChecker.areSchema public class JsonSchemaResolver { @NotNull private final Project myProject; @NotNull private final JsonSchemaObject mySchema; - private final boolean myIsName; @NotNull private final JsonPointerPosition myPosition; public JsonSchemaResolver(@NotNull Project project, @NotNull JsonSchemaObject schema, - boolean isName, @NotNull JsonPointerPosition position) { myProject = project; mySchema = schema; - myIsName = isName; myPosition = position; } public JsonSchemaResolver(@NotNull Project project, @NotNull JsonSchemaObject schema) { myProject = project; mySchema = schema; - myIsName = true; myPosition = new JsonPointerPosition(); } public MatchResult detailedResolve() { - final JsonSchemaTreeNode node = JsonSchemaVariantsTreeBuilder.buildTree(myProject, mySchema, myPosition, false, !myIsName); + final JsonSchemaTreeNode node = JsonSchemaVariantsTreeBuilder.buildTree(myProject, mySchema, myPosition, false); return MatchResult.create(node); } @@ -82,10 +78,9 @@ public class JsonSchemaResolver { } @Nullable - public PsiElement findNavigationTarget(@Nullable final PsiElement element, - boolean acceptAdditionalPropertiesSchema) { + public PsiElement findNavigationTarget(@Nullable final PsiElement element) { final JsonSchemaTreeNode node = JsonSchemaVariantsTreeBuilder - .buildTree(myProject, mySchema, myPosition, true, acceptAdditionalPropertiesSchema || !myIsName); + .buildTree(myProject, mySchema, myPosition, true); final JsonSchemaObject schema = selectSchema(node, element, myPosition.isEmpty()); if (schema == null) return null; VirtualFile file = JsonSchemaService.Impl.get(myProject).resolveSchemaFile(schema); diff --git a/json/src/com/jetbrains/jsonSchema/impl/JsonSchemaVariantsTreeBuilder.java b/json/src/com/jetbrains/jsonSchema/impl/JsonSchemaVariantsTreeBuilder.java index 4628d0741b04..abe556a7f190 100644 --- a/json/src/com/jetbrains/jsonSchema/impl/JsonSchemaVariantsTreeBuilder.java +++ b/json/src/com/jetbrains/jsonSchema/impl/JsonSchemaVariantsTreeBuilder.java @@ -39,8 +39,7 @@ public class JsonSchemaVariantsTreeBuilder { public static JsonSchemaTreeNode buildTree(@NotNull Project project, @NotNull final JsonSchemaObject schema, @NotNull final JsonPointerPosition position, - final boolean skipLastExpand, - final boolean acceptAdditional) { + final boolean skipLastExpand) { final JsonSchemaTreeNode root = new JsonSchemaTreeNode(null, schema); JsonSchemaService service = JsonSchemaService.Impl.get(project); expandChildSchema(root, schema, service); @@ -59,7 +58,7 @@ public class JsonSchemaVariantsTreeBuilder { node.nothingChild(); continue; } - final Pair pair = doSingleStep(step, node.getSchema(), acceptAdditional, true); + final Pair pair = doSingleStep(step, node.getSchema(), true); if (ThreeState.NO.equals(pair.getFirst())) node.nothingChild(); else if (ThreeState.YES.equals(pair.getFirst())) node.anyChild(); else { @@ -112,15 +111,14 @@ public class JsonSchemaVariantsTreeBuilder { @NotNull public static Pair doSingleStep(@NotNull JsonPointerPosition step, @NotNull JsonSchemaObject parent, - boolean acceptAdditionalPropertiesSchemas, boolean processAllBranches) { final String name = step.getFirstName(); if (name != null) { - return propertyStep(name, parent, acceptAdditionalPropertiesSchemas, processAllBranches); + return propertyStep(name, parent, processAllBranches); } else { final int index = step.getFirstIndex(); assert index >= 0; - return arrayOrNumericPropertyElementStep(index, parent, acceptAdditionalPropertiesSchemas); + return arrayOrNumericPropertyElementStep(index, parent); } } @@ -384,7 +382,6 @@ public class JsonSchemaVariantsTreeBuilder { @NotNull private static Pair propertyStep(@NotNull String name, @NotNull JsonSchemaObject parent, - boolean acceptAdditionalPropertiesSchemas, boolean processAllBranches) { final JsonSchemaObject child = parent.getProperties().get(name); if (child != null) { @@ -394,13 +391,13 @@ public class JsonSchemaVariantsTreeBuilder { if (schema != null) { return Pair.create(ThreeState.UNSURE, schema); } - if (acceptAdditionalPropertiesSchemas) { - if (parent.getAdditionalPropertiesSchema() != null) { - return Pair.create(ThreeState.UNSURE, parent.getAdditionalPropertiesSchema()); - } + if (parent.getAdditionalPropertiesSchema() != null) { + return Pair.create(ThreeState.UNSURE, parent.getAdditionalPropertiesSchema()); + } + if (processAllBranches) { List ifThenElseList = parent.getIfThenElse(); - if (ifThenElseList != null && processAllBranches) { + if (ifThenElseList != null) { for (IfThenElse ifThenElse : ifThenElseList) { // resolve inside V7 if-then-else conditionals JsonSchemaObject childObject; @@ -435,8 +432,7 @@ public class JsonSchemaVariantsTreeBuilder { } @NotNull - private static Pair arrayOrNumericPropertyElementStep(int idx, @NotNull JsonSchemaObject parent, - boolean acceptAdditionalPropertiesSchemas) { + private static Pair arrayOrNumericPropertyElementStep(int idx, @NotNull JsonSchemaObject parent) { if (parent.getItemsSchema() != null) { return Pair.create(ThreeState.UNSURE, parent.getItemsSchema()); } @@ -454,7 +450,7 @@ public class JsonSchemaVariantsTreeBuilder { if (matchingPatternPropertySchema != null) { return Pair.create(ThreeState.UNSURE, matchingPatternPropertySchema); } - if (parent.getAdditionalItemsSchema() != null && acceptAdditionalPropertiesSchemas) { + if (parent.getAdditionalItemsSchema() != null) { return Pair.create(ThreeState.UNSURE, parent.getAdditionalItemsSchema()); } if (Boolean.FALSE.equals(parent.getAdditionalItemsAllowed())) { diff --git a/json/src/com/jetbrains/jsonSchema/impl/inspections/JsonSchemaDeprecationInspection.java b/json/src/com/jetbrains/jsonSchema/impl/inspections/JsonSchemaDeprecationInspection.java index 453893a23fe3..ce7c1b371112 100644 --- a/json/src/com/jetbrains/jsonSchema/impl/inspections/JsonSchemaDeprecationInspection.java +++ b/json/src/com/jetbrains/jsonSchema/impl/inspections/JsonSchemaDeprecationInspection.java @@ -37,7 +37,7 @@ public class JsonSchemaDeprecationInspection extends JsonSchemaBasedInspectionBa JsonPointerPosition position = walker.findPosition(o, true); if (position == null) return; - final MatchResult result = new JsonSchemaResolver(project, schema, false, position).detailedResolve(); + final MatchResult result = new JsonSchemaResolver(project, schema, position).detailedResolve(); for (JsonSchemaObject object : result.mySchemas) { String message = object.getDeprecationMessage(); if (message != null) { diff --git a/json/tests/test/com/jetbrains/jsonSchema/JsonSchemaDocumentationTest.java b/json/tests/test/com/jetbrains/jsonSchema/JsonSchemaDocumentationTest.java index 227d8e6651f9..4ee6db8fb9f6 100644 --- a/json/tests/test/com/jetbrains/jsonSchema/JsonSchemaDocumentationTest.java +++ b/json/tests/test/com/jetbrains/jsonSchema/JsonSchemaDocumentationTest.java @@ -30,4 +30,8 @@ public class JsonSchemaDocumentationTest extends JsonBySchemaDocumentationBaseTe public void testHtmlDescription() throws Exception { doTest(true, "json"); } + + public void testAdditionalPropertyDocs() throws Exception { + doTest(true, "json"); + } } diff --git a/json/tests/testData/jsonSchema/documentation/additionalPropertyDocs.html b/json/tests/testData/jsonSchema/documentation/additionalPropertyDocs.html new file mode 100644 index 000000000000..d9427ee8e1a4 --- /dev/null +++ b/json/tests/testData/jsonSchema/documentation/additionalPropertyDocs.html @@ -0,0 +1 @@ +
enabled: boolean
some text
\ No newline at end of file diff --git a/json/tests/testData/jsonSchema/documentation/additionalPropertyDocs.json b/json/tests/testData/jsonSchema/documentation/additionalPropertyDocs.json new file mode 100644 index 000000000000..462975cc963c --- /dev/null +++ b/json/tests/testData/jsonSchema/documentation/additionalPropertyDocs.json @@ -0,0 +1,8 @@ + { + "additionalProp": { + "enabled": true + }, + "knownProp": { + "enabled": false + } + } diff --git a/json/tests/testData/jsonSchema/documentation/additionalPropertyDocsSchema.json b/json/tests/testData/jsonSchema/documentation/additionalPropertyDocsSchema.json new file mode 100644 index 000000000000..81ec64232782 --- /dev/null +++ b/json/tests/testData/jsonSchema/documentation/additionalPropertyDocsSchema.json @@ -0,0 +1,27 @@ + { + "$schema": "http://json-schema.org/draft-07/schema#", + "type": "object", + "definitions": { + "option": { + "type": "object", + "properties": { + "enabled": { + "type": "boolean", + "description": "some text" + } + } + } + }, + "additionalProperties": { + "type": "object", + "$ref": "#/definitions/option" + }, + "properties": { + "knownProp": { + "type": "object", + "$ref": "#/definitions/option", + "title": "title", + "description": "text" + } + } + } diff --git a/json/tests/testData/jsonSchema/documentation/additionalPropertyDocs_short.html b/json/tests/testData/jsonSchema/documentation/additionalPropertyDocs_short.html new file mode 100644 index 000000000000..6a22e024842e --- /dev/null +++ b/json/tests/testData/jsonSchema/documentation/additionalPropertyDocs_short.html @@ -0,0 +1 @@ +enabled: boolean
some text \ No newline at end of file diff --git a/plugins/yaml/src/org/jetbrains/yaml/schema/YamlJsonSchemaDeprecationInspection.java b/plugins/yaml/src/org/jetbrains/yaml/schema/YamlJsonSchemaDeprecationInspection.java index 679658f8d453..157e845dc5a4 100644 --- a/plugins/yaml/src/org/jetbrains/yaml/schema/YamlJsonSchemaDeprecationInspection.java +++ b/plugins/yaml/src/org/jetbrains/yaml/schema/YamlJsonSchemaDeprecationInspection.java @@ -51,7 +51,7 @@ public class YamlJsonSchemaDeprecationInspection extends YamlJsonSchemaInspectio return; } - final MatchResult result = new JsonSchemaResolver(project, schema, false, position).detailedResolve(); + final MatchResult result = new JsonSchemaResolver(project, schema, position).detailedResolve(); for (JsonSchemaObject object : result.mySchemas) { String message = object.getDeprecationMessage(); if (message != null) { diff --git a/plugins/yaml/src/org/jetbrains/yaml/schema/YamlJsonSchemaGotoDeclarationHandler.java b/plugins/yaml/src/org/jetbrains/yaml/schema/YamlJsonSchemaGotoDeclarationHandler.java index 67123b4cdf01..c7fba36cb7d0 100644 --- a/plugins/yaml/src/org/jetbrains/yaml/schema/YamlJsonSchemaGotoDeclarationHandler.java +++ b/plugins/yaml/src/org/jetbrains/yaml/schema/YamlJsonSchemaGotoDeclarationHandler.java @@ -33,9 +33,7 @@ public class YamlJsonSchemaGotoDeclarationHandler implements GotoDeclarationHand if (steps == null) return null; final JsonSchemaObject schemaObject = service.getSchemaObject(file); if (schemaObject != null) { - final PsiElement target = new JsonSchemaResolver(sourceElement.getProject(), schemaObject, false, steps) - .findNavigationTarget(literal.getValue(), - JsonSchemaService.isSchemaFile(containingFile)); + final PsiElement target = new JsonSchemaResolver(sourceElement.getProject(), schemaObject, steps).findNavigationTarget(literal.getValue()); if (target != null) { return new PsiElement[] {target}; }