Skip to content

Commit

Permalink
Add unit test to ObjectMapperMergeTests
Browse files Browse the repository at this point in the history
  • Loading branch information
felixbarny committed Oct 23, 2024
1 parent 4beccfd commit 5ee4db4
Showing 1 changed file with 34 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,19 @@
package org.elasticsearch.index.mapper;

import org.elasticsearch.index.IndexVersion;
import org.elasticsearch.script.ScriptCompiler;
import org.elasticsearch.test.ESTestCase;

import java.util.Collections;
import java.util.Optional;

import static org.elasticsearch.index.mapper.MapperService.MergeReason.INDEX_TEMPLATE;
import static org.elasticsearch.index.mapper.MapperService.MergeReason.MAPPING_AUTO_UPDATE;
import static org.elasticsearch.index.mapper.MapperService.MergeReason.MAPPING_AUTO_UPDATE_PREFLIGHT;
import static org.elasticsearch.index.mapper.MapperService.MergeReason.MAPPING_UPDATE;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.instanceOf;
import static org.hamcrest.Matchers.is;

public final class ObjectMapperMergeTests extends ESTestCase {

Expand Down Expand Up @@ -318,6 +324,34 @@ public void testMergeSubobjectsFalseWithObject() {
assertNotNull(parentMapper.getMapper("child.grandchild"));
}

public void testConflictingDynamicUpdate() {
RootObjectMapper mergeInto = new RootObjectMapper.Builder("_doc", Optional.empty()).add(
new KeywordFieldMapper.Builder("http.status_code", IndexVersion.current())
).build(MapperBuilderContext.root(false, false));
RootObjectMapper mergeWith = new RootObjectMapper.Builder("_doc", Optional.empty()).add(
new NumberFieldMapper.Builder(
"http.status_code",
NumberFieldMapper.NumberType.LONG,
ScriptCompiler.NONE,
false,
true,
IndexVersion.current(),
null
)
).build(MapperBuilderContext.root(false, false));

MapperService.MergeReason autoUpdateMergeReason = randomFrom(MAPPING_AUTO_UPDATE, MAPPING_AUTO_UPDATE_PREFLIGHT);
ObjectMapper merged = mergeInto.merge(mergeWith, MapperMergeContext.root(false, false, autoUpdateMergeReason, Long.MAX_VALUE));
FieldMapper httpStatusCode = (FieldMapper) merged.getMapper("http.status_code");
assertThat(httpStatusCode, is(instanceOf(KeywordFieldMapper.class)));

IllegalArgumentException e = expectThrows(
IllegalArgumentException.class,
() -> mergeInto.merge(mergeWith, MapperMergeContext.root(false, false, MAPPING_UPDATE, Long.MAX_VALUE))
);
assertThat(e.getMessage(), equalTo("mapper [http.status_code] cannot be changed from type [keyword] to [long]"));
}

private static RootObjectMapper createRootSubobjectFalseLeafWithDots() {
FieldMapper.Builder fieldBuilder = new KeywordFieldMapper.Builder("host.name", IndexVersion.current());
FieldMapper fieldMapper = fieldBuilder.build(MapperBuilderContext.root(false, false));
Expand Down

0 comments on commit 5ee4db4

Please sign in to comment.