fix(change-summary): minor issues (#20095)

- deep copy previous change summary so that it doesnt get modified
- handle multiple fields during consolidation
This commit is contained in:
Imri Paran 2025-03-06 15:00:01 +01:00 committed by GitHub
parent 2704ed1900
commit 242e85a797
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 25 additions and 1 deletions

View File

@ -56,7 +56,10 @@ public class ChangeSummarizer<T extends EntityInterface> {
new ChangeSummary()
.withChangeSource(changeSource)
.withChangedAt(changedAt)
.withChangedBy(changedBy)));
.withChangedBy(changedBy),
// If its a consolidation, we might have multiple changes for the same field.
// Since we are only interested in the field name, we can just take whichever.
(existing, replacement) -> existing));
}
private boolean isFieldTracked(String fieldName) {

View File

@ -2993,6 +2993,7 @@ public abstract class EntityRepository<T extends EntityInterface> {
ChangeSummaryMap current =
Optional.ofNullable(original.getChangeDescription())
.map(ChangeDescription::getChangeSummary)
.map(changeSummaryMap -> JsonUtils.deepCopy(changeSummaryMap, ChangeSummaryMap.class))
.orElse(new ChangeSummaryMap());
Map<String, ChangeSummary> addedSummaries =

View File

@ -42,6 +42,26 @@ public class ChangeSummarizerTest {
assert result.size() == 0;
}
@Test
public void test_duplicateEntriesConsolidation() {
String fieldName = "description";
ChangeSource changeSource = ChangeSource.MANUAL;
long updatedAt = System.currentTimeMillis();
String updatedBy = "testUser";
List<FieldChange> changes =
List.of(new FieldChange().withName(fieldName), new FieldChange().withName(fieldName));
Map<String, ChangeSummary> result =
changeSummarizer.summarizeChanges(Map.of(), changes, changeSource, updatedBy, updatedAt);
assert result.size() == 1;
Assertions.assertTrue(result.containsKey(fieldName));
result =
changeSummarizer.summarizeChanges(
result, changes, ChangeSource.AUTOMATED, "older-change", updatedAt - 100);
assert result.size() == 0;
}
@Test
public void test_columnDescription() {
String fieldName = "columns.column1.description";