@@ -36,25 +36,41 @@ public Optional<ChangedPaths> diff(
36
36
final Map <String , PathItem > left , final Map <String , PathItem > right ) {
37
37
ChangedPaths changedPaths = new ChangedPaths (left , right );
38
38
changedPaths .getIncreased ().putAll (right );
39
+
39
40
left .keySet ()
40
41
.forEach (
41
42
(String url ) -> {
42
43
PathItem leftPath = left .get (url );
43
44
String template = normalizePath (url );
44
- Optional <String > result =
45
- right .keySet ().stream ()
46
- .filter (s -> normalizePath (s ).equals (template ))
47
- .findFirst ();
45
+ Optional <Map .Entry <String , PathItem >> result =
46
+ changedPaths .getIncreased ().entrySet ().stream ()
47
+ .filter (item -> normalizePath (item .getKey ()).equals (template ))
48
+ .min ((a , b ) -> {
49
+ if (methodsIntersect (a .getValue (), b .getValue ())) {
50
+ throw new IllegalArgumentException (
51
+ "Two path items have the same signature: " + template );
52
+ }
53
+ if (a .getKey ().equals (url )) {
54
+ return -1 ;
55
+ } else if (b .getKey ().equals ((url ))) {
56
+ return 1 ;
57
+ } else {
58
+ HashSet <PathItem .HttpMethod > methodsA = new HashSet <>(
59
+ a .getValue ().readOperationsMap ().keySet ());
60
+ methodsA .retainAll (leftPath .readOperationsMap ().keySet ());
61
+ HashSet <PathItem .HttpMethod > methodsB = new HashSet <>(
62
+ b .getValue ().readOperationsMap ().keySet ());
63
+ methodsB .retainAll (leftPath .readOperationsMap ().keySet ());
64
+ return Integer .compare (methodsB .size (), methodsA .size ());
65
+ }
66
+ });
48
67
if (result .isPresent ()) {
49
- if (!changedPaths .getIncreased ().containsKey (result .get ())) {
50
- throw new IllegalArgumentException (
51
- "Two path items have the same signature: " + template );
52
- }
53
- PathItem rightPath = changedPaths .getIncreased ().remove (result .get ());
68
+ String rightUrl = result .get ().getKey ();
69
+ PathItem rightPath = changedPaths .getIncreased ().remove (rightUrl );
54
70
Map <String , String > params = new LinkedHashMap <>();
55
- if (!url .equals (result . get () )) {
71
+ if (!url .equals (rightUrl )) {
56
72
List <String > oldParams = extractParameters (url );
57
- List <String > newParams = extractParameters (result . get () );
73
+ List <String > newParams = extractParameters (rightUrl );
58
74
for (int i = 0 ; i < oldParams .size (); i ++) {
59
75
params .put (oldParams .get (i ), newParams .get (i ));
60
76
}
@@ -65,7 +81,7 @@ public Optional<ChangedPaths> diff(
65
81
openApiDiff
66
82
.getPathDiff ()
67
83
.diff (leftPath , rightPath , context )
68
- .ifPresent (path -> changedPaths .getChanged ().put (result . get () , path ));
84
+ .ifPresent (path -> changedPaths .getChanged ().put (rightUrl , path ));
69
85
} else {
70
86
changedPaths .getMissing ().put (url , leftPath );
71
87
}
@@ -79,4 +95,14 @@ public static Paths valOrEmpty(Paths path) {
79
95
}
80
96
return path ;
81
97
}
98
+
99
+ private static boolean methodsIntersect (PathItem a , PathItem b ) {
100
+ Set <PathItem .HttpMethod > methodsA = a .readOperationsMap ().keySet ();
101
+ for (PathItem .HttpMethod method : b .readOperationsMap ().keySet ()) {
102
+ if (methodsA .contains (method )) {
103
+ return true ;
104
+ }
105
+ }
106
+ return false ;
107
+ }
82
108
}
0 commit comments