Skip to content

Commit 278228d

Browse files
committed
Extension: find changes when nothing changed
Fix issue OpenAPITools#51
1 parent e0d18c4 commit 278228d

File tree

3 files changed

+23
-9
lines changed

3 files changed

+23
-9
lines changed

src/main/java/com/qdesrame/openapi/diff/compare/ExtensionDiff.java

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,13 @@
44
import com.qdesrame.openapi.diff.model.Changed;
55
import com.qdesrame.openapi.diff.model.DiffContext;
66

7-
import java.util.Optional;
8-
97
public interface ExtensionDiff {
108

119
ExtensionDiff setOpenApiDiff(OpenApiDiff openApiDiff);
1210

1311
String getName();
1412

15-
Optional<Changed> diff(Change extension, DiffContext context);
13+
Changed diff(Change extension, DiffContext context);
1614

1715
default boolean isParentApplicable(Change.Type type, Object object, Object extension, DiffContext context) {
1816
return true;

src/main/java/com/qdesrame/openapi/diff/compare/ExtensionsDiff.java

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,14 @@
22

33
import com.qdesrame.openapi.diff.model.Change;
44
import com.qdesrame.openapi.diff.model.Changed;
5-
import com.qdesrame.openapi.diff.model.CompatibleChanged;
65
import com.qdesrame.openapi.diff.model.DiffContext;
76
import com.qdesrame.openapi.diff.model.schema.ChangedExtensions;
87

98
import java.util.*;
109
import java.util.function.Function;
1110

1211
import static com.qdesrame.openapi.diff.utils.ChangedUtils.isChanged;
12+
import static com.qdesrame.openapi.diff.utils.Copy.copyMap;
1313

1414
public class ExtensionsDiff {
1515
private final OpenApiDiff openApiDiff;
@@ -51,27 +51,29 @@ public Optional<ChangedExtensions> diff(Map<String, Object> left, Map<String, Ob
5151
}
5252

5353
public Optional<ChangedExtensions> diff(Map<String, Object> left, Map<String, Object> right, DiffContext context) {
54-
if (null == left) left = new LinkedHashMap<>();
55-
if (null == right) right = new LinkedHashMap<>();
56-
ChangedExtensions changedExtensions = new ChangedExtensions(left, new LinkedHashMap<>(right), context);
54+
left = copyMap(left);
55+
right = copyMap(right);
56+
ChangedExtensions changedExtensions = new ChangedExtensions(left, copyMap(right), context);
5757
for (String key : left.keySet()) {
5858
Object leftValue = left.get(key);
5959
if (right.containsKey(key)) {
6060
Object rightValue = right.remove(key);
6161
executeExtensionDiff(key, Change.changed(leftValue, rightValue), context)
62+
.filter(Changed::isDifferent)
6263
.ifPresent(changed -> changedExtensions.getChanged().put(key, changed));
6364
} else {
6465
executeExtensionDiff(key, Change.removed(leftValue), context)
66+
.filter(Changed::isDifferent)
6567
.ifPresent(changed -> changedExtensions.getMissing().put(key, changed));
6668
}
6769
}
6870
right.forEach((key, value) -> executeExtensionDiff(key, Change.added(value), context)
71+
.filter(Changed::isDifferent)
6972
.ifPresent(changed -> changedExtensions.getIncreased().put(key, changed)));
7073
return isChanged(changedExtensions);
7174
}
7275

7376
private Optional<Changed> executeExtensionDiff(String name, Change change, DiffContext context) {
74-
return executeExtension(name, diff -> diff.setOpenApiDiff(openApiDiff).diff(change, context))
75-
.orElse(Optional.of(CompatibleChanged.compatible(change)));
77+
return executeExtension(name, diff -> diff.setOpenApiDiff(openApiDiff).diff(change, context));
7678
}
7779
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package com.qdesrame.openapi.diff.utils;
2+
3+
import java.util.LinkedHashMap;
4+
import java.util.Map;
5+
6+
public class Copy {
7+
public static <K, V> Map<K, V> copyMap(Map<K, V> map) {
8+
if (map == null) {
9+
return new LinkedHashMap<>();
10+
} else {
11+
return new LinkedHashMap<>(map);
12+
}
13+
}
14+
}

0 commit comments

Comments
 (0)