88import io .swagger .v3 .oas .models .Paths ;
99import org .apache .commons .lang3 .tuple .ImmutablePair ;
1010import cli .weka .RequestMethodsWekaClassifier ;
11+ import cli .ai .gpt ;
1112
1213import java .util .ArrayList ;
1314import java .util .List ;
@@ -18,13 +19,17 @@ public class RequestTypeDescriptionRule implements IRestRule {
1819 private static final RuleIdentifier RULE_IDENTIFIER = RuleIdentifier .REQUEST_TYPE_DESCRIPTION ;
1920 static final RuleCategory RULE_CATEGORY = RuleCategory .META ;
2021 static final RuleSeverity RULE_SEVERITY = RuleSeverity .WARNING ;
21- static final List <RuleSoftwareQualityAttribute > SOFTWARE_QUALITY_ATTRIBUTES = List .of (RuleSoftwareQualityAttribute .MAINTAINABILITY );
22+ static final List <RuleSoftwareQualityAttribute > SOFTWARE_QUALITY_ATTRIBUTES = List
23+ .of (RuleSoftwareQualityAttribute .MAINTAINABILITY );
2224 private static final String IMPROVEMNT_SUB_STRING = " The request should be of type: " ;
2325 private boolean isActive ;
2426 final String MODEL = "/models/request_model.dat" ;
27+ private gpt gptClient ;
28+ private boolean enableAI = false ;
2529
2630 public RequestTypeDescriptionRule (boolean isActive ) {
2731 this .isActive = isActive ;
32+ this .gptClient = new gpt ();
2833 }
2934
3035 @ Override
@@ -62,6 +67,11 @@ public void setIsActive(boolean isActive) {
6267 this .isActive = isActive ;
6368 }
6469
70+ @ Override
71+ public void setEnableAI (boolean enableAI ) {
72+ this .enableAI = enableAI ;
73+ }
74+
6575 @ Override
6676 public List <Violation > checkViolation (OpenAPI openAPI ) {
6777 RequestMethodsWekaClassifier wt = new RequestMethodsWekaClassifier ();
@@ -72,57 +82,103 @@ public List<Violation> checkViolation(OpenAPI openAPI) {
7282 Set <String > paths = openAPI .getPaths ().keySet ();
7383 Paths pathsTest = openAPI .getPaths ();
7484
75- if (paths .isEmpty ()) return violations ;
85+ if (paths .isEmpty ())
86+ return violations ;
7687 pathsTest .values ().forEach (pathItem -> {
77- String keyPath = pathsTest .keySet ().stream ().filter (key -> pathsTest .get (key ).equals (pathItem )).findFirst ().get ();
78- if (pathItem .getGet () != null ){
88+ String keyPath = pathsTest .keySet ().stream ().filter (key -> pathsTest .get (key ).equals (pathItem )).findFirst ()
89+ .get ();
90+ if (pathItem .getGet () != null ) {
7991 String description = pathItem .getGet ().getDescription ();
8092 String summary = pathItem .getGet ().getSummary ();
81- if (summary != null && !summary .isEmpty ()){
82- getViolationGetRequest (wt , keyPath , summary , ErrorMessage .REQUESTTYPETUNNELINGGET , "get" , ImprovementSuggestion .REQUESTTYPEGET , true , violations );
83- }else if (description != null && !description .isEmpty ()){
84- getViolationGetRequest (wt , keyPath , description , ErrorMessage .REQUESTTYPETUNNELINGGET , "get" , ImprovementSuggestion .REQUESTTYPEGET , true , violations );
93+
94+ if (this .enableAI ) {
95+ this .gptClient .tunnelingViolationAI (this , keyPath , description , summary , "get" , ImprovementSuggestion .REQUESTTYPEGET , IMPROVEMNT_SUB_STRING , violations );
96+ } else {
97+ if (summary != null && !summary .isEmpty ()) {
98+ getViolationGetRequest (wt , keyPath , summary ,
99+ ErrorMessage .REQUESTTYPETUNNELINGGET , "get" ,
100+ ImprovementSuggestion .REQUESTTYPEGET , true , violations );
101+ } else if (description != null && !description .isEmpty ()) {
102+ getViolationGetRequest (wt , keyPath , description ,
103+ ErrorMessage .REQUESTTYPETUNNELINGGET , "get" ,
104+ ImprovementSuggestion .REQUESTTYPEGET , true , violations );
105+ }
85106 }
86107
108+
87109 }
88- if (pathItem .getPost () != null ){
110+ if (pathItem .getPost () != null ) {
89111 String description = pathItem .getPost ().getDescription ();
90112 String summary = pathItem .getPost ().getSummary ();
91- if (summary != null && !summary .isEmpty ()){
92- getViolationGetRequest (wt , keyPath , summary , ErrorMessage .REQUESTTYPETUNNELINGPOST , "post" , ImprovementSuggestion .REQUESTTYPEPOST , true , violations );
93- }else if (description != null && !description .isEmpty ()){
94- getViolationGetRequest (wt , keyPath , description , ErrorMessage .REQUESTTYPETUNNELINGPOST , "post" , ImprovementSuggestion .REQUESTTYPEPOST , true , violations );
113+
114+ if (this .enableAI ) {
115+ this .gptClient .tunnelingViolationAI (this , keyPath , description , summary , "post" , ImprovementSuggestion .REQUESTTYPEPOST , IMPROVEMNT_SUB_STRING , violations );
116+ } else {
117+ if (summary != null && !summary .isEmpty ()) {
118+ getViolationGetRequest (wt , keyPath , summary ,
119+ ErrorMessage .REQUESTTYPETUNNELINGPOST , "post" ,
120+ ImprovementSuggestion .REQUESTTYPEPOST , true , violations );
121+ } else if (description != null && !description .isEmpty ()) {
122+ getViolationGetRequest (wt , keyPath , description ,
123+ ErrorMessage .REQUESTTYPETUNNELINGPOST , "post" ,
124+ ImprovementSuggestion .REQUESTTYPEPOST , true , violations );
125+ }
95126 }
96127 }
97- if (pathItem .getPut () != null ){
128+ if (pathItem .getPut () != null ) {
98129 String description = pathItem .getPut ().getDescription ();
99130 String summary = pathItem .getPut ().getSummary ();
100- if (summary != null && !summary .isEmpty ()){
101- getViolationGetRequest (wt , keyPath , summary , "" , "put" , ImprovementSuggestion .REQUESTTYPEPUT , false , violations );
102- }else if (description != null && !description .isEmpty ()){
103- getViolationGetRequest (wt , keyPath , description , "" , "put" , ImprovementSuggestion .REQUESTTYPEPUT , false , violations );
131+
132+ if (this .enableAI ) {
133+ this .gptClient .tunnelingViolationAI (this , keyPath , description , summary , "put" , ImprovementSuggestion .REQUESTTYPEPUT , IMPROVEMNT_SUB_STRING , violations );
134+ } else {
135+ if (summary != null && !summary .isEmpty ()) {
136+ getViolationGetRequest (wt , keyPath , summary , "" , "put" ,
137+ ImprovementSuggestion .REQUESTTYPEPUT , false ,
138+ violations );
139+ } else if (description != null && !description .isEmpty ()) {
140+ getViolationGetRequest (wt , keyPath , description , "" , "put" ,
141+ ImprovementSuggestion .REQUESTTYPEPUT ,
142+ false , violations );
143+ }
104144 }
105145 }
106- if (pathItem .getDelete () != null ){
146+ if (pathItem .getDelete () != null ) {
107147 String description = pathItem .getDelete ().getDescription ();
108148 String summary = pathItem .getDelete ().getSummary ();
109- if (summary != null && !summary .isEmpty ()){
110- getViolationGetRequest (wt , keyPath , summary , "" , "delete" , ImprovementSuggestion .REQUESTTYPEDELETE , false , violations );
111- }else if (description != null && !description .isEmpty ()){
112- getViolationGetRequest (wt , keyPath , description , "" , "delete" , ImprovementSuggestion .REQUESTTYPEDELETE , false , violations );
149+
150+ if (this .enableAI ) {
151+ this .gptClient .tunnelingViolationAI (this , keyPath , description , summary , "delete" ,
152+ ImprovementSuggestion .REQUESTTYPEDELETE , IMPROVEMNT_SUB_STRING , violations );
153+ } else {
154+ if (summary != null && !summary .isEmpty ()) {
155+ getViolationGetRequest (wt , keyPath , summary , "" , "delete" ,
156+ ImprovementSuggestion .REQUESTTYPEDELETE ,
157+ false , violations );
158+ } else if (description != null && !description .isEmpty ()) {
159+ getViolationGetRequest (wt , keyPath , description , "" , "delete" ,
160+ ImprovementSuggestion .REQUESTTYPEDELETE , false , violations );
161+ }
113162 }
114163 }
115164 });
116165
117166 return violations ;
118167 }
119168
120- private void getViolationGetRequest (RequestMethodsWekaClassifier wt , String keyPath , String description , String requestTypeTunnelingType , String requestType , String requestTypeMessage , boolean switchRequestType , List <Violation > violations ) {
169+ private void getViolationGetRequest (RequestMethodsWekaClassifier wt , String keyPath , String description ,
170+ String requestTypeTunnelingType , String requestType , String requestTypeMessage , boolean switchRequestType ,
171+ List <Violation > violations ) {
121172 ImmutablePair <String , Double > predictionValues = wt .predict (description );
122- if ((predictionValues != null && predictionValues .right != null && predictionValues .left != null ) && predictionValues .left .equals ("invalid" ) && (predictionValues .right >= 0.75 ) && switchRequestType ) {
123- violations .add (new Violation (this , RestAnalyzer .locMapper .getLOCOfPath (keyPath ), ImprovementSuggestion .REQUESTTYPETUNELING , keyPath , requestTypeTunnelingType ));
124- } else if ((predictionValues != null && predictionValues .right != null && predictionValues .left != null ) && !predictionValues .left .equals (requestType ) && (predictionValues .right >= 0.75 )) {
125- violations .add (new Violation (this , RestAnalyzer .locMapper .getLOCOfPath (keyPath ), requestTypeMessage + IMPROVEMNT_SUB_STRING + predictionValues .left .toUpperCase (), keyPath , ErrorMessage .REQUESTTYPE ));
173+ if ((predictionValues != null && predictionValues .right != null && predictionValues .left != null )
174+ && predictionValues .left .equals ("invalid" ) && (predictionValues .right >= 0.75 ) && switchRequestType ) {
175+ violations .add (new Violation (this , RestAnalyzer .locMapper .getLOCOfPath (keyPath ),
176+ ImprovementSuggestion .REQUESTTYPETUNELING , keyPath , requestTypeTunnelingType ));
177+ } else if ((predictionValues != null && predictionValues .right != null && predictionValues .left != null )
178+ && !predictionValues .left .equals (requestType ) && (predictionValues .right >= 0.75 )) {
179+ violations .add (new Violation (this , RestAnalyzer .locMapper .getLOCOfPath (keyPath ),
180+ requestTypeMessage + IMPROVEMNT_SUB_STRING + predictionValues .left .toUpperCase (), keyPath ,
181+ ErrorMessage .REQUESTTYPE ));
126182 }
127183 }
128184
0 commit comments