@@ -36,25 +36,41 @@ public Optional<ChangedPaths> diff(
3636 final Map <String , PathItem > left , final Map <String , PathItem > right ) {
3737 ChangedPaths changedPaths = new ChangedPaths (left , right );
3838 changedPaths .getIncreased ().putAll (right );
39+
3940 left .keySet ()
4041 .forEach (
4142 (String url ) -> {
4243 PathItem leftPath = left .get (url );
4344 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+ });
4867 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 );
5470 Map <String , String > params = new LinkedHashMap <>();
55- if (!url .equals (result . get () )) {
71+ if (!url .equals (rightUrl )) {
5672 List <String > oldParams = extractParameters (url );
57- List <String > newParams = extractParameters (result . get () );
73+ List <String > newParams = extractParameters (rightUrl );
5874 for (int i = 0 ; i < oldParams .size (); i ++) {
5975 params .put (oldParams .get (i ), newParams .get (i ));
6076 }
@@ -65,7 +81,7 @@ public Optional<ChangedPaths> diff(
6581 openApiDiff
6682 .getPathDiff ()
6783 .diff (leftPath , rightPath , context )
68- .ifPresent (path -> changedPaths .getChanged ().put (result . get () , path ));
84+ .ifPresent (path -> changedPaths .getChanged ().put (rightUrl , path ));
6985 } else {
7086 changedPaths .getMissing ().put (url , leftPath );
7187 }
@@ -79,4 +95,14 @@ public static Paths valOrEmpty(Paths path) {
7995 }
8096 return path ;
8197 }
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+ }
82108}
0 commit comments