Skip to content

Commit

Permalink
Fix bug handle empty mappings when removing mapping types (#1178)
Browse files Browse the repository at this point in the history
* Fix bug handle empty mappings when removing mapping types
* Capture exceptions during transformation and log errors

Signed-off-by: Peter Nied <[email protected]>
  • Loading branch information
peternied authored Dec 4, 2024
1 parent d7889c3 commit 36098eb
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,16 @@ public GlobalMetadata transformGlobalMetadata(GlobalMetadata globalData) {
templatesRoot.fields().forEachRemaining(template -> {
var templateCopy = (ObjectNode) template.getValue().deepCopy();
var indexTemplate = (Index) () -> templateCopy;
transformIndex(indexTemplate, IndexType.TEMPLATE);
templates.set(template.getKey(), indexTemplate.getRawJson());
try {
transformIndex(indexTemplate, IndexType.TEMPLATE);
templates.set(template.getKey(), indexTemplate.getRawJson());
} catch (Exception e) {
log.atError()
.setMessage("Unable to transform object: {}")
.addArgument(indexTemplate::getRawJson)
.setCause(e)
.log();
}
});
newRoot.set("templates", templates);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public class IndexMappingTypeRemoval implements TransformationRule<Index> {
public CanApplyResult canApply(final Index index) {
final var mappingNode = index.getRawJson().get(MAPPINGS_KEY);

if (mappingNode == null) {
if (mappingNode == null || mappingNode.size() == 0) {
return CanApplyResult.NO;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,21 @@ private boolean applyTransformation(final ObjectNode indexJson) {
return wasChanged;
}

@Test
void testApplyTransformation_emptyMappingArray() {
// Setup
var originalJson = indexSettingJson("\"mappings\": [],");
var indexJson = originalJson.deepCopy();

// Action
var wasChanged = applyTransformation(indexJson);
assertThat(canApply(originalJson), equalTo(CanApplyResult.NO));

// Verification
assertThat(wasChanged, equalTo(false));
assertThat(indexJson.toPrettyString(), equalTo(originalJson.toPrettyString()));
}

@Test
void testApplyTransformation_noMappingNode() {
// Setup
Expand Down

0 comments on commit 36098eb

Please sign in to comment.