|
2 | 2 |
|
3 | 3 | import com.qdesrame.openapi.diff.model.Change;
|
4 | 4 | import com.qdesrame.openapi.diff.model.Changed;
|
5 |
| -import com.qdesrame.openapi.diff.model.CompatibleChanged; |
6 | 5 | import com.qdesrame.openapi.diff.model.DiffContext;
|
7 | 6 | import com.qdesrame.openapi.diff.model.schema.ChangedExtensions;
|
8 | 7 |
|
9 | 8 | import java.util.*;
|
10 | 9 | import java.util.function.Function;
|
11 | 10 |
|
12 | 11 | import static com.qdesrame.openapi.diff.utils.ChangedUtils.isChanged;
|
| 12 | +import static com.qdesrame.openapi.diff.utils.Copy.copyMap; |
13 | 13 |
|
14 | 14 | public class ExtensionsDiff {
|
15 | 15 | private final OpenApiDiff openApiDiff;
|
@@ -51,27 +51,29 @@ public Optional<ChangedExtensions> diff(Map<String, Object> left, Map<String, Ob
|
51 | 51 | }
|
52 | 52 |
|
53 | 53 | 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); |
57 | 57 | for (String key : left.keySet()) {
|
58 | 58 | Object leftValue = left.get(key);
|
59 | 59 | if (right.containsKey(key)) {
|
60 | 60 | Object rightValue = right.remove(key);
|
61 | 61 | executeExtensionDiff(key, Change.changed(leftValue, rightValue), context)
|
| 62 | + .filter(Changed::isDifferent) |
62 | 63 | .ifPresent(changed -> changedExtensions.getChanged().put(key, changed));
|
63 | 64 | } else {
|
64 | 65 | executeExtensionDiff(key, Change.removed(leftValue), context)
|
| 66 | + .filter(Changed::isDifferent) |
65 | 67 | .ifPresent(changed -> changedExtensions.getMissing().put(key, changed));
|
66 | 68 | }
|
67 | 69 | }
|
68 | 70 | right.forEach((key, value) -> executeExtensionDiff(key, Change.added(value), context)
|
| 71 | + .filter(Changed::isDifferent) |
69 | 72 | .ifPresent(changed -> changedExtensions.getIncreased().put(key, changed)));
|
70 | 73 | return isChanged(changedExtensions);
|
71 | 74 | }
|
72 | 75 |
|
73 | 76 | 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)); |
76 | 78 | }
|
77 | 79 | }
|
0 commit comments