WEB-32244 JSON schema: allOf merge is not recursive

This commit is contained in:
Anton Lobov
2018-04-19 13:56:28 +02:00
parent e551e48b84
commit d385afe53a
2 changed files with 40 additions and 3 deletions
@@ -102,8 +102,7 @@ public class JsonSchemaObject {
// peer pointer is not merged!
public void mergeValues(@NotNull JsonSchemaObject other) {
// we do not copy id, schema
myProperties.putAll(other.myProperties);
mergeProperties(this, other);
myDefinitionsMap = copyMap(myDefinitionsMap, other.myDefinitionsMap);
final Map<String, JsonSchemaObject> map = copyMap(myPatternProperties == null ? null : myPatternProperties.mySchemasMap,
other.myPatternProperties == null ? null : other.myPatternProperties.mySchemasMap);
@@ -161,6 +160,20 @@ public class JsonSchemaObject {
myShouldValidateAgainstJSType |= other.myShouldValidateAgainstJSType;
}
private static void mergeProperties(@NotNull JsonSchemaObject thisObject, @NotNull JsonSchemaObject otherObject) {
for (Map.Entry<String, JsonSchemaObject> prop: otherObject.myProperties.entrySet()) {
String key = prop.getKey();
JsonSchemaObject otherProp = prop.getValue();
if (!thisObject.myProperties.containsKey(key)) {
thisObject.myProperties.put(key, otherProp);
}
else {
JsonSchemaObject existingProp = thisObject.myProperties.get(key);
thisObject.myProperties.put(key, JsonSchemaVariantsTreeBuilder.merge(existingProp, otherProp, otherProp));
}
}
}
public void shouldValidateAgainstJSType() {
myShouldValidateAgainstJSType = true;
}
@@ -281,7 +281,7 @@ class JsonBySchemaCompletionTest : JsonBySchemaCompletionBaseTest() {
}
@Throws(Exception::class)
private fun testImpl(schema: String, text: String,
private fun testImpl(@Language("JSON") schema: String, text: String,
vararg variants: String) {
testBySchema(schema, text, ".json", *variants)
}
@@ -352,4 +352,28 @@ class JsonBySchemaCompletionTest : JsonBySchemaCompletionBaseTest() {
testImpl(ifThenElseSchema, """{"a": 5, "c": <caret>}""", "false", "true")
Assert.assertEquals(2, myItems.size.toLong())
}
@Throws(Exception::class)
fun testNestedPropsMerging() {
testImpl("""{
"allOf": [
{
"properties": {
"severity": {
"type": "string",
"enum": ["a", "b"]
}
}
},
{
"properties": {
"severity": {
}
}
}
]
}""","""{
"severity": <caret>
}""", "\"a\"", "\"b\"");
}
}