From 11fab5c66f64045528d540d8958ea09bc5020d52 Mon Sep 17 00:00:00 2001 From: Boaz Leskes Date: Mon, 19 Aug 2013 17:57:22 +0200 Subject: [PATCH] XContentHelper.mergeDefaults with single key object list ([ { key: .. } , {key : ... } .. ] ) didn't add defaults which were not already in content Closes #3538 --- .../common/xcontent/XContentHelper.java | 3 + .../xcontent/support/XContentHelperTests.java | 69 +++++++++++++++++++ 2 files changed, 72 insertions(+) create mode 100644 src/test/java/org/elasticsearch/test/unit/common/xcontent/support/XContentHelperTests.java diff --git a/src/main/java/org/elasticsearch/common/xcontent/XContentHelper.java b/src/main/java/org/elasticsearch/common/xcontent/XContentHelper.java index 7244fc22ba8..f3468e667fe 100644 --- a/src/main/java/org/elasticsearch/common/xcontent/XContentHelper.java +++ b/src/main/java/org/elasticsearch/common/xcontent/XContentHelper.java @@ -233,6 +233,9 @@ public class XContentHelper { Map.Entry 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 map : processed.values()) { diff --git a/src/test/java/org/elasticsearch/test/unit/common/xcontent/support/XContentHelperTests.java b/src/test/java/org/elasticsearch/test/unit/common/xcontent/support/XContentHelperTests.java new file mode 100644 index 00000000000..1f296690f4a --- /dev/null +++ b/src/test/java/org/elasticsearch/test/unit/common/xcontent/support/XContentHelperTests.java @@ -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 getMap(Object... keyValues) { + Map map = new HashMap(); + for (int i = 0; i < keyValues.length; i++) { + map.put((String) keyValues[i], keyValues[++i]); + } + return map; + } + + Map getNamedMap(String name, Object... keyValues) { + Map map = getMap(keyValues); + + Map namedMap = new HashMap(1); + namedMap.put(name, map); + return namedMap; + } + + List getList(Object... values) { + return Arrays.asList(values); + } + + @Test + public void testMergingListValuesAreMapsOfOne() { + + Map defaults = getMap("test", getList(getNamedMap("name1", "t1", "1"), getNamedMap("name2", "t2", "2"))); + Map content = getMap("test", getList(getNamedMap("name2", "t3", "3"), getNamedMap("name4", "t4", "4"))); + Map 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)); + } + + +}