72
72
import io .swagger .v3 .oas .models .media .Schema ;
73
73
import io .swagger .v3 .oas .models .parameters .RequestBody ;
74
74
import io .swagger .v3 .oas .models .responses .ApiResponse ;
75
+ import io .swagger .v3 .oas .models .security .SecurityRequirement ;
76
+ import io .swagger .v3 .oas .models .security .SecurityScheme ;
75
77
import io .swagger .v3 .oas .models .servers .Server ;
76
78
77
79
public class K6ClientCodegen extends DefaultCodegen implements CodegenConfig {
@@ -300,6 +302,7 @@ static class HTTPRequest {
300
302
@ Nullable
301
303
HTTPBody body ;
302
304
boolean hasBodyExample ;
305
+ boolean hasCookie ;
303
306
@ Nullable
304
307
HTTPParameters params ;
305
308
@ Nullable
@@ -308,7 +311,7 @@ static class HTTPRequest {
308
311
DataExtractSubstituteParameter dataExtract ;
309
312
310
313
public HTTPRequest (String operationId , String method , String path , @ Nullable List <Parameter > query , @ Nullable HTTPBody body ,
311
- boolean hasBodyExample , @ Nullable HTTPParameters params , @ Nullable List <k6Check > k6Checks ,
314
+ boolean hasBodyExample , boolean hasCookie , @ Nullable HTTPParameters params , @ Nullable List <k6Check > k6Checks ,
312
315
DataExtractSubstituteParameter dataExtract ) {
313
316
// NOTE: https://k6.io/docs/javascript-api/k6-http/del-url-body-params
314
317
this .method = method .equals ("delete" ) ? "del" : method ;
@@ -318,6 +321,7 @@ public HTTPRequest(String operationId, String method, String path, @Nullable Lis
318
321
this .query = query ;
319
322
this .body = body ;
320
323
this .hasBodyExample = hasBodyExample ;
324
+ this .hasCookie = hasCookie ;
321
325
this .params = params ;
322
326
this .k6Checks = k6Checks ;
323
327
this .dataExtract = dataExtract ;
@@ -494,6 +498,29 @@ public void preprocessOpenAPI(OpenAPI openAPI) {
494
498
Set <Parameter > extraParameters = new HashSet <>();
495
499
Map <String , Set <Parameter >> pathVariables = new HashMap <>();
496
500
501
+ // get security schema from components
502
+ Map <String , SecurityScheme > securitySchemeMap = openAPI != null ?
503
+ (openAPI .getComponents () != null ? openAPI .getComponents ().getSecuritySchemes () : null ) : null ;
504
+
505
+ // get global security requirements
506
+ List <SecurityRequirement > securityRequirements = openAPI .getSecurity ();
507
+
508
+ // match global security requirements with security schemes and transform them to global auth methods
509
+ // global auth methods is a list of auth methods that are used in all requests
510
+ List <CodegenSecurity > globalAuthMethods = new ArrayList <>();
511
+ Map <String , SecurityScheme > globalSecurityMap = new HashMap <>();
512
+ if (securityRequirements != null ) {
513
+ for (SecurityRequirement securityRequirement : securityRequirements ) {
514
+ for (String securityRequirementKey : securityRequirement .keySet ()) {
515
+ SecurityScheme securityScheme = securitySchemeMap .get (securityRequirementKey );
516
+ if (securityScheme != null ) {
517
+ globalSecurityMap .put (securityRequirementKey , securityScheme );
518
+ }
519
+ }
520
+ }
521
+ globalAuthMethods = fromSecurity (globalSecurityMap );
522
+ }
523
+
497
524
for (String path : openAPI .getPaths ().keySet ()) {
498
525
Map <Integer , HTTPRequest > requests = new HashMap <>();
499
526
Set <Parameter > variables = new HashSet <>();
@@ -503,6 +530,7 @@ public void preprocessOpenAPI(OpenAPI openAPI) {
503
530
for (Map .Entry <PathItem .HttpMethod , Operation > methodOperation : openAPI .getPaths ().get (path ).
504
531
readOperationsMap ().entrySet ()) {
505
532
List <Parameter > httpParams = new ArrayList <>();
533
+ List <Parameter > cookieParams = new ArrayList <>();
506
534
List <Parameter > queryParams = new ArrayList <>();
507
535
List <Parameter > bodyOrFormParams = new ArrayList <>();
508
536
List <k6Check > k6Checks = new ArrayList <>();
@@ -638,7 +666,19 @@ public void preprocessOpenAPI(OpenAPI openAPI) {
638
666
639
667
pathVariables .put (groupName , variables );
640
668
641
- final HTTPParameters params = new HTTPParameters (null , null , httpParams , null , null , null , null , null ,
669
+ // put auth medthods in header or cookie
670
+ for (CodegenSecurity globalAuthMethod : globalAuthMethods ) {
671
+ if (globalAuthMethod .isKeyInHeader ) {
672
+ httpParams .add (new Parameter (globalAuthMethod .keyParamName , getTemplateString (toVarName (globalAuthMethod .keyParamName ))));
673
+ extraParameters .add (new Parameter (toVarName (globalAuthMethod .keyParamName ), globalAuthMethod .keyParamName .toUpperCase (Locale .ROOT )));
674
+ }
675
+ if (globalAuthMethod .isKeyInCookie ) {
676
+ cookieParams .add (new Parameter (globalAuthMethod .keyParamName , getTemplateString (toVarName (globalAuthMethod .keyParamName ))));
677
+ extraParameters .add (new Parameter (toVarName (globalAuthMethod .keyParamName ), globalAuthMethod .keyParamName .toUpperCase (Locale .ROOT )));
678
+ }
679
+ }
680
+
681
+ final HTTPParameters params = new HTTPParameters (null , cookieParams , httpParams , null , null , null , null , null ,
642
682
responseType .length () > 0 ? responseType : null );
643
683
644
684
assert params .headers != null ;
@@ -650,11 +690,18 @@ public void preprocessOpenAPI(OpenAPI openAPI) {
650
690
// calculate order for this current request
651
691
Integer requestOrder = calculateRequestOrder (operationGroupingOrder , requests .size ());
652
692
653
- requests .put (requestOrder , new HTTPRequest (operationId , method .toString ().toLowerCase (Locale .ROOT ), path ,
654
- queryParams .size () > 0 ? queryParams : null ,
655
- bodyOrFormParams .size () > 0 ? new HTTPBody (bodyOrFormParams ) : null , hasRequestBodyExample ,
656
- params .headers .size () > 0 ? params : null , k6Checks .size () > 0 ? k6Checks : null ,
657
- dataExtract .orElse (null )));
693
+ requests .put (requestOrder , new HTTPRequest (
694
+ operationId ,
695
+ method .toString ().toLowerCase (Locale .ROOT ),
696
+ path ,
697
+ queryParams .size () > 0 ? queryParams : null ,
698
+ bodyOrFormParams .size () > 0 ? new HTTPBody (bodyOrFormParams ) : null ,
699
+ hasRequestBodyExample ,
700
+ params .cookies .size () > 0 ? true : false ,
701
+ params .headers .size () > 0 ? params : null ,
702
+ k6Checks .size () > 0 ? k6Checks : null ,
703
+ dataExtract .orElse (null ))
704
+ );
658
705
}
659
706
660
707
addOrUpdateRequestGroup (requestGroups , groupName , pathVariables .get (groupName ), requests );
0 commit comments