Skip to content

Commit 42b0b20

Browse files
[k6] Add authentication variables in headers and cookies (OpenAPITools#19060)
* Add global auth parsing including headers and cookies for k6. * Add global auth statements. * Generate samples. * Revert "Generate samples." This reverts commit b8cff86. * Copy petstore-with-fake-endpoints-models-for-testing.yaml to a seperate k6 path as its example yaml file. * Add global security statement in k6 example yaml instead of using the generic one.
1 parent 5fdcd48 commit 42b0b20

File tree

5 files changed

+2449
-51
lines changed

5 files changed

+2449
-51
lines changed

bin/configs/k6.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
generatorName: k6
22
outputDir: samples/client/petstore/k6
3-
inputSpec: modules/openapi-generator/src/test/resources/3_0/petstore-with-fake-endpoints-models-for-testing.yaml
3+
inputSpec: modules/openapi-generator/src/test/resources/3_0/k6/petstore-with-fake-endpoints-models-for-testing.yaml
44
templateDir: modules/openapi-generator/src/main/resources/k6
55
additionalProperties:
66
appName: PetstoreClient

modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/K6ClientCodegen.java

+54-7
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,8 @@
7272
import io.swagger.v3.oas.models.media.Schema;
7373
import io.swagger.v3.oas.models.parameters.RequestBody;
7474
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;
7577
import io.swagger.v3.oas.models.servers.Server;
7678

7779
public class K6ClientCodegen extends DefaultCodegen implements CodegenConfig {
@@ -300,6 +302,7 @@ static class HTTPRequest {
300302
@Nullable
301303
HTTPBody body;
302304
boolean hasBodyExample;
305+
boolean hasCookie;
303306
@Nullable
304307
HTTPParameters params;
305308
@Nullable
@@ -308,7 +311,7 @@ static class HTTPRequest {
308311
DataExtractSubstituteParameter dataExtract;
309312

310313
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,
312315
DataExtractSubstituteParameter dataExtract) {
313316
// NOTE: https://k6.io/docs/javascript-api/k6-http/del-url-body-params
314317
this.method = method.equals("delete") ? "del" : method;
@@ -318,6 +321,7 @@ public HTTPRequest(String operationId, String method, String path, @Nullable Lis
318321
this.query = query;
319322
this.body = body;
320323
this.hasBodyExample = hasBodyExample;
324+
this.hasCookie = hasCookie;
321325
this.params = params;
322326
this.k6Checks = k6Checks;
323327
this.dataExtract = dataExtract;
@@ -494,6 +498,29 @@ public void preprocessOpenAPI(OpenAPI openAPI) {
494498
Set<Parameter> extraParameters = new HashSet<>();
495499
Map<String, Set<Parameter>> pathVariables = new HashMap<>();
496500

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+
497524
for (String path : openAPI.getPaths().keySet()) {
498525
Map<Integer, HTTPRequest> requests = new HashMap<>();
499526
Set<Parameter> variables = new HashSet<>();
@@ -503,6 +530,7 @@ public void preprocessOpenAPI(OpenAPI openAPI) {
503530
for (Map.Entry<PathItem.HttpMethod, Operation> methodOperation : openAPI.getPaths().get(path).
504531
readOperationsMap().entrySet()) {
505532
List<Parameter> httpParams = new ArrayList<>();
533+
List<Parameter> cookieParams = new ArrayList<>();
506534
List<Parameter> queryParams = new ArrayList<>();
507535
List<Parameter> bodyOrFormParams = new ArrayList<>();
508536
List<k6Check> k6Checks = new ArrayList<>();
@@ -638,7 +666,19 @@ public void preprocessOpenAPI(OpenAPI openAPI) {
638666

639667
pathVariables.put(groupName, variables);
640668

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,
642682
responseType.length() > 0 ? responseType : null);
643683

644684
assert params.headers != null;
@@ -650,11 +690,18 @@ public void preprocessOpenAPI(OpenAPI openAPI) {
650690
// calculate order for this current request
651691
Integer requestOrder = calculateRequestOrder(operationGroupingOrder, requests.size());
652692

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+
);
658705
}
659706

660707
addOrUpdateRequestGroup(requestGroups, groupName, pathVariables.get(groupName), requests);

modules/openapi-generator/src/main/resources/k6/script.mustache

+7-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,13 @@ export default function() {
3030
let body = {{#body}}{{=<% %>=}}{<%#parameters%>"<%& key%>": <%& value%><%^-last%>, <%/-last%><%/parameters%>}<%={{ }}=%>{{/body}};
3131
{{/body}}
3232
{{#params}}
33-
let params = {{#params}}{{=<% %>=}}{headers: {<%# headers%>"<%& key%>": <%& value%><%^-last%>, <%/-last%><%/headers%><%#responseType%>, "Accept": <%& responseType%><%/responseType%>}<%# auth%>, auth: "<%& auth%>"<%/auth%>}<%={{ }}=%>{{/params}};
33+
let params = {{#params}}{{=<% %>=}}{
34+
headers: {
35+
<%# headers%>"<%& key%>": <%& value%><%^-last%>, <%/-last%><%/headers%><%#responseType%>, "Accept": <%& responseType%><%/responseType%>
36+
}<%#hasCookie%>, cookies: {
37+
<%# cookies%>"<%& key%>": <%& value%><%^-last%>, <%/-last%><%/cookies%>
38+
}<%/hasCookie%><%# auth%>, auth: "<%& auth%>"<%/auth%>
39+
}<%={{ }}=%>{{/params}};
3440
{{/params}}
3541
{{#isDelete}}
3642
{{#params}}

0 commit comments

Comments
 (0)