52
52
import java .net .URI ;
53
53
import java .net .URISyntaxException ;
54
54
import java .net .URL ;
55
- import java .util .ArrayList ;
56
- import java .util .Arrays ;
57
- import java .util .Iterator ;
58
- import java .util .LinkedHashMap ;
59
- import java .util .LinkedHashSet ;
60
- import java .util .List ;
61
- import java .util .Map ;
62
- import java .util .Optional ;
63
- import java .util .Set ;
55
+ import java .util .*;
64
56
import java .util .stream .Collectors ;
65
57
import java .util .stream .Stream ;
66
58
@@ -526,13 +518,64 @@ public Paths getPaths(ObjectNode obj, String location, ParseResult result) {
526
518
} else {
527
519
ObjectNode path = (ObjectNode ) pathValue ;
528
520
PathItem pathObj = getPathItem (path ,String .format ("%s.'%s'" , location ,pathName ), result );
521
+ String [] eachPart = pathName .split ("/" );
522
+ Arrays .stream (eachPart )
523
+ .filter (part -> part .startsWith ("{" ) && part .endsWith ("}" ) && part .length () > 2 )
524
+ .forEach (part -> {
525
+ String pathParam = part .substring (1 , part .length () - 1 );
526
+ boolean definedInPathLevel = isPathParamDefined (pathParam , pathObj .getParameters ());
527
+ if (!definedInPathLevel ) {
528
+ List <Operation > operationsInAPath = getAllOperationsInAPath (pathObj );
529
+ operationsInAPath .forEach (operation -> {
530
+ if (!isPathParamDefined (pathParam , operation .getParameters ())) {
531
+ result .warning (location + ".'" + pathName + "'" ," Declared path parameter " + pathParam + " needs to be defined as a path parameter in path or operation level" );
532
+ return ;
533
+ }
534
+ });
535
+ }
536
+ });
529
537
paths .put (pathName , pathObj );
530
538
}
531
539
}
532
540
}
533
541
return paths ;
534
542
}
535
543
544
+ private boolean isPathParamDefined (String pathParam , List <Parameter > parameters ) {
545
+ if (parameters == null || parameters .isEmpty ()) {
546
+ return false ;
547
+ } else {
548
+ Parameter pathParamDefined = parameters .stream ()
549
+ .filter (parameter -> pathParam .equals (parameter .getName ()))
550
+ .findFirst ()
551
+ .orElse (null );
552
+ if (pathParamDefined == null ) {
553
+ return false ;
554
+ }
555
+ }
556
+ return true ;
557
+ }
558
+
559
+ private void addToOperationsList (List <Operation > operationsList , Operation operation ) {
560
+ if (operation == null ) {
561
+ return ;
562
+ }
563
+ operationsList .add (operation );
564
+ }
565
+
566
+ public List <Operation > getAllOperationsInAPath (PathItem pathObj ) {
567
+ List <Operation > operations = new ArrayList <>();
568
+ addToOperationsList (operations , pathObj .getGet ());
569
+ addToOperationsList (operations , pathObj .getPut ());
570
+ addToOperationsList (operations , pathObj .getPost ());
571
+ addToOperationsList (operations , pathObj .getPatch ());
572
+ addToOperationsList (operations , pathObj .getDelete ());
573
+ addToOperationsList (operations , pathObj .getTrace ());
574
+ addToOperationsList (operations , pathObj .getOptions ());
575
+ addToOperationsList (operations , pathObj .getHead ());
576
+ return operations ;
577
+ }
578
+
536
579
public PathItem getPathItem (ObjectNode obj , String location , ParseResult result ) {
537
580
538
581
@@ -2669,6 +2712,7 @@ protected static class ParseResult {
2669
2712
private Map <Location , JsonNode > extra = new LinkedHashMap <>();
2670
2713
private Map <Location , JsonNode > unsupported = new LinkedHashMap <>();
2671
2714
private Map <Location , String > invalidType = new LinkedHashMap <>();
2715
+ private List <Location > warnings = new ArrayList <>();
2672
2716
private List <Location > missing = new ArrayList <>();
2673
2717
2674
2718
public ParseResult () {
@@ -2686,6 +2730,10 @@ public void missing(String location, String key) {
2686
2730
missing .add (new Location (location , key ));
2687
2731
}
2688
2732
2733
+ public void warning (String location , String key ) {
2734
+ warnings .add (new Location (location , key ));
2735
+ }
2736
+
2689
2737
public void invalidType (String location , String key , String expectedType , JsonNode value ) {
2690
2738
invalidType .put (new Location (location , key ), expectedType );
2691
2739
}
@@ -2715,6 +2763,11 @@ public List<String> getMessages() {
2715
2763
String message = "attribute " + location + l .key + " is missing" ;
2716
2764
messages .add (message );
2717
2765
}
2766
+ for (Location l : warnings ) {
2767
+ String location = l .location .equals ("" ) ? "" : l .location + "." ;
2768
+ String message = "attribute " + location +l .key ;
2769
+ messages .add (message );
2770
+ }
2718
2771
for (Location l : unsupported .keySet ()) {
2719
2772
String location = l .location .equals ("" ) ? "" : l .location + "." ;
2720
2773
String message = "attribute " + location + l .key + " is unsupported" ;
0 commit comments