Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ K6 Generator ] Further K6 OpenAPI generator enhancements (request body example data extraction, support for generating scenario tests and load tests out of the box, and much more) #10614

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions modules/openapi-generator/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,11 @@
<artifactId>commons-lang3</artifactId>
<version>${commons-lang.version}</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-text</artifactId>
<version>${commons-text.version}</version>
</dependency>
<dependency>
<groupId>commons-cli</groupId>
<artifactId>commons-cli</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,10 @@ private static boolean nonempty(List<?> params) {
return params != null && params.size() > 0;
}

private static boolean nonempty(Map<?, ?> params) {
return params != null && params.size() > 0;
}

/**
* Check if there's at least one body parameter
*
Expand Down Expand Up @@ -186,6 +190,15 @@ public boolean getHasDefaultResponse() {
return responses.stream().filter(response -> response.isDefault).findFirst().isPresent();
}

/**
* Check if there's at least one vendor extension
*
* @return true if vendor extensions exists, false otherwise
*/
public boolean getHasVendorExtensions() {
return nonempty(vendorExtensions);
}

/**
* Check if act as Restful index method
*
Expand Down

Large diffs are not rendered by default.

83 changes: 44 additions & 39 deletions modules/openapi-generator/src/main/resources/k6/script.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -19,47 +19,52 @@ export default function() {
let {{{key}}} = {{#lambda.handleParamValue}}{{value}}{{/lambda.handleParamValue}}
{{/variables}}
{{#requests}}
{{#-first}}
let url = BASE_URL + `{{{path}}}{{=<% %>=}}<%#query%><%#-first%>?<%/-first%><%& key%>=<%& value%><%^-last%>&<%/-last%><%/query%><%={{ }}=%>`;

// Request No. {{-index}}
{{#body}}
// TODO: edit the parameters of the request body.
let body = {{#body}}{{=<% %>=}}{<%#parameters%>"<%& key%>": <%& value%><%^-last%>, <%/-last%><%/parameters%>}<%={{ }}=%>{{/body}};
{{/body}}
{{#params}}
let params = {{#params}}{{=<% %>=}}{headers: {<%# headers%>"<%& key%>": <%& value%><%^-last%>, <%/-last%><%/headers%><%#responseType%>, "Accept": <%& responseType%><%/responseType%>}<%# auth%>, auth: "<%& auth%>"<%/auth%>}<%={{ }}=%>{{/params}};
{{/params}}
let request = http.{{method}}(url{{#body}}, body{{/body}}{{#params}}, params{{/params}});
{{#k6Checks}}
{{=<% %>=}}
check(request, {
{
let url = BASE_URL + `{{{path}}}{{=<% %>=}}<%#query%><%#-first%>?<%/-first%><%& key%>=<%& value%><%^-last%>&<%/-last%><%/query%><%={{ }}=%>`;
{{#body}}
{{^hasBodyExample}}
// TODO: edit the parameters of the request body.
{{/hasBodyExample}}
let body = {{#body}}{{=<% %>=}}{<%#parameters%>"<%& key%>": <%& value%><%^-last%>, <%/-last%><%/parameters%>}<%={{ }}=%>{{/body}};
{{/body}}

{{#params}}
let params = {{#params}}{{=<% %>=}}{headers: {<%# headers%>"<%& key%>": <%& value%><%^-last%>, <%/-last%><%/headers%><%#responseType%>, "Accept": <%& responseType%><%/responseType%>}<%# auth%>, auth: "<%& auth%>"<%/auth%>}<%={{ }}=%>{{/params}};
{{/params}}

{{#isDelete}}
{{#params}}
// this is a DELETE method request - if params are also set, empty body must be passed
let request = http.{{method}}(url, {} {{#params}}, params{{/params}});
{{/params}}
{{^params}}
let request = http.{{method}}(url);
{{/params}}
{{/isDelete}}

{{^isDelete}}
let request = http.{{method}}(url{{#body}}, JSON.stringify(body){{/body}}{{#params}}, params{{/params}});
{{/isDelete}}

{{#k6Checks}}
{{=<% %>=}}
check(request, {
"<%& description%>": (r) => r.status === <%& status%>
});
<%={{ }}=%>
{{/k6Checks}}
{{/-first}}
{{^-first}}
// Request No. {{-index}}
{{#body}}
// TODO: edit the parameters of the request body.
body = {{#body}}{{=<% %>=}}{<%#parameters%>"<%& key%>": <%& value%><%^-last%>, <%/-last%><%/parameters%>}<%={{ }}=%>{{/body}};
{{/body}}
{{#params}}
params = {{#params}}{{=<% %>=}}{headers: {<%# headers%>"<%& key%>": <%& value%><%^-last%>, <%/-last%><%/headers%>}<%# auth%>, auth: "<%& auth%>"<%/auth%>}<%={{ }}=%>{{/params}};
{{/params}}
request = http.{{method}}(url{{#body}}, body{{/body}}{{#params}}, params{{/params}});
{{#k6Checks}}
{{=<% %>=}}
check(request, {
"<%& description%>": (r) => r.status === <%& status%>
});
<%={{ }}=%>
{{/k6Checks}}
{{/-first}}
sleep(SLEEP_DURATION);
{{^-last}}

{{/-last}}
});
<%={{ }}=%>
{{/k6Checks}}

{{#dataExtract}}
{{{paramName}}} = JSON.parse(request.body).{{{valuePath}}}; // extract data for subsequent use
{{/dataExtract}}

{{^-last}}
sleep(SLEEP_DURATION);
{{/-last}}
}

{{/requests}}
});
{{/requestGroups}}
Expand Down
1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1544,6 +1544,7 @@
<commons-cli.version>1.4</commons-cli.version>
<commons-io.version>2.11.0</commons-io.version>
<commons-lang.version>3.12.0</commons-lang.version>
<commons-text.version>1.9</commons-text.version>
<diffutils.version>1.3.0</diffutils.version>
<generex.version>1.0.2</generex.version>
<git-commit-id-plugin.version>4.9.10</git-commit-id-plugin.version>
Expand Down
2 changes: 2 additions & 0 deletions samples/client/petstore/k6/.openapi-generator/FILES
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
README.md
script.js
Loading