XContentHelper.mergeDefaults with single key object list ([ { key: .. } , {key : ... } .. ] ) didn't add defaults which were not already in content

Closes #3538
This commit is contained in:
Boaz Leskes 2013-08-19 17:57:22 +02:00
parent 7f7f79d622
commit 11fab5c66f
2 changed files with 72 additions and 0 deletions

View File

@ -233,6 +233,9 @@ public class XContentHelper {
Map.Entry<String, Object> entry = map.entrySet().iterator().next();
if (processed.containsKey(entry.getKey())) {
mergeDefaults(processed.get(entry.getKey()), map);
} else {
// put the default entries after the content ones.
processed.put(entry.getKey(), map);
}
}
for (Map<String, Object> map : processed.values()) {

View File

@ -0,0 +1,69 @@
package org.elasticsearch.test.unit.common.xcontent.support;
/*
* Licensed to ElasticSearch under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. ElasticSearch licenses this
* file to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
import org.elasticsearch.common.xcontent.XContentHelper;
import org.hamcrest.Matchers;
import org.junit.Test;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import static org.hamcrest.MatcherAssert.assertThat;
public class XContentHelperTests {
Map<String, Object> getMap(Object... keyValues) {
Map<String, Object> map = new HashMap<String, Object>();
for (int i = 0; i < keyValues.length; i++) {
map.put((String) keyValues[i], keyValues[++i]);
}
return map;
}
Map<String, Object> getNamedMap(String name, Object... keyValues) {
Map<String, Object> map = getMap(keyValues);
Map<String, Object> namedMap = new HashMap<String, Object>(1);
namedMap.put(name, map);
return namedMap;
}
List<Object> getList(Object... values) {
return Arrays.asList(values);
}
@Test
public void testMergingListValuesAreMapsOfOne() {
Map<String, Object> defaults = getMap("test", getList(getNamedMap("name1", "t1", "1"), getNamedMap("name2", "t2", "2")));
Map<String, Object> content = getMap("test", getList(getNamedMap("name2", "t3", "3"), getNamedMap("name4", "t4", "4")));
Map<String, Object> expected = getMap("test",
getList(getNamedMap("name2", "t2", "2", "t3", "3"), getNamedMap("name4", "t4", "4"), getNamedMap("name1", "t1", "1")));
XContentHelper.mergeDefaults(content, defaults);
assertThat(content, Matchers.equalTo(expected));
}
}