Skip to content
This repository was archived by the owner on Feb 9, 2021. It is now read-only.

Commit 056a3ec

Browse files
authoredFeb 20, 2020
[K6] minor improvements (OpenAPITools#5368)
* update readme * minor improvement * add windows batch file for k6 * update template to reference openapi-generator * update samples * update doc
1 parent b16e07c commit 056a3ec

File tree

12 files changed

+302
-21
lines changed

12 files changed

+302
-21
lines changed
 

‎.gitignore

-1
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ packages/
3030
.vagrant/
3131
.vscode/
3232
**/.vs
33-
bin
3433
.factorypath
3534

3635
.settings

‎README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ OpenAPI Generator allows generation of API client libraries (SDK generation), se
6464

6565
| | Languages/Frameworks |
6666
|-|-|
67-
**API clients** | **ActionScript**, **Ada**, **Apex**, **Bash**, **C**, **C#** (.net 2.0, 3.5 or later, .NET Standard 1.3 - 2.0, .NET Core 2.0), **C++** (cpp-restsdk, Qt5, Tizen), **Clojure**, **Dart (1.x, 2.x)**, **Elixir**, **Elm**, **Eiffel**, **Erlang**, **Go**, **Groovy**, **Haskell** (http-client, Servant), **Java** (Jersey1.x, Jersey2.x, OkHttp, Retrofit1.x, Retrofit2.x, Feign, RestTemplate, RESTEasy, Vertx, Google API Client Library for Java, Rest-assured, Spring 5 Web Client, MicroProfile Rest Client), **Kotlin**, **Lua**, **Nim**, **Node.js/JavaScript** (ES5, ES6, AngularJS with Google Closure Compiler annotations, Flow types), **Objective-C**, **OCaml**, **Perl**, **PHP**, **PowerShell**, **Python**, **R**, **Ruby**, **Rust** (rust, rust-server), **Scala** (akka, http4s, scalaz, swagger-async-httpclient), **Swift** (2.x, 3.x, 4.x, 5.x), **Typescript** (AngularJS, Angular (2.x - 8.x), Aurelia, Axios, Fetch, Inversify, jQuery, Node, Rxjs)
67+
**API clients** | **ActionScript**, **Ada**, **Apex**, **Bash**, **C**, **C#** (.net 2.0, 3.5 or later, .NET Standard 1.3 - 2.0, .NET Core 2.0), **C++** (cpp-restsdk, Qt5, Tizen), **Clojure**, **Dart (1.x, 2.x)**, **Elixir**, **Elm**, **Eiffel**, **Erlang**, **Go**, **Groovy**, **Haskell** (http-client, Servant), **Java** (Jersey1.x, Jersey2.x, OkHttp, Retrofit1.x, Retrofit2.x, Feign, RestTemplate, RESTEasy, Vertx, Google API Client Library for Java, Rest-assured, Spring 5 Web Client, MicroProfile Rest Client), **k6**, **Kotlin**, **Lua**, **Nim**, **Node.js/JavaScript** (ES5, ES6, AngularJS with Google Closure Compiler annotations, Flow types), **Objective-C**, **OCaml**, **Perl**, **PHP**, **PowerShell**, **Python**, **R**, **Ruby**, **Rust** (rust, rust-server), **Scala** (akka, http4s, scalaz, swagger-async-httpclient), **Swift** (2.x, 3.x, 4.x, 5.x), **Typescript** (AngularJS, Angular (2.x - 8.x), Aurelia, Axios, Fetch, Inversify, jQuery, Node, Rxjs)
6868
**Server stubs** | **Ada**, **C#** (ASP.NET Core, NancyFx), **C++** (Pistache, Restbed, Qt5 QHTTPEngine), **Erlang**, **F#** (Giraffe), **Go** (net/http, Gin), **Haskell** (Servant), **Java** (MSF4J, Spring, Undertow, JAX-RS: CDI, CXF, Inflector, Jersey, RestEasy, Play Framework, [PKMST](https://github.com/ProKarma-Inc/pkmst-getting-started-examples), [Vert.x](https://vertx.io/)), **Kotlin** (Spring Boot, Ktor, Vertx), **PHP** (Laravel, Lumen, Slim, Silex, [Symfony](https://symfony.com/), [Zend Expressive](https://github.com/zendframework/zend-expressive)), **Python** (Flask), **NodeJS**, **Ruby** (Sinatra, Rails5), **Rust** (rust-server), **Scala** ([Finch](https://github.com/finagle/finch), [Lagom](https://github.com/lagom/lagom), [Play](https://www.playframework.com/), Scalatra)
6969
**API documentation generators** | **HTML**, **Confluence Wiki**, **Asciidoc**
7070
**Configuration files** | [**Apache2**](https://httpd.apache.org/)
@@ -871,6 +871,7 @@ Here is a list of template creators:
871871
* Confluence Wiki: @jhitchcock
872872
* Configuration
873873
* Apache2: @stkrwork
874+
* k6: @mostafa
874875
* Schema
875876
* Avro: @sgadouar
876877
* GraphQL: @wing328 [:heart:](https://www.patreon.com/wing328)

‎bin/windows/k6-petstore.bat

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
set executable=.\modules\openapi-generator-cli\target\openapi-generator-cli.jar
2+
3+
If Not Exist %executable% (
4+
mvn clean package
5+
)
6+
7+
REM set JAVA_OPTS=%JAVA_OPTS% -Xmx1024M -DloggerPath=conf/log4j.properties
8+
set ags=generate -i modules\openapi-generator\src\test\resources\2_0\petstore.yaml -g k6 -o samples\client\petstore\k6
9+
10+
java %JAVA_OPTS% -jar %executable% %ags%

‎docs/generators.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ The following generators are available:
3737
* [javascript-flowtyped](generators/javascript-flowtyped.md)
3838
* [jaxrs-cxf-client](generators/jaxrs-cxf-client.md)
3939
* [jmeter](generators/jmeter.md)
40-
* [k6](generators/k6.md)
40+
* [k6 (beta)](generators/k6.md)
4141
* [kotlin](generators/kotlin.md)
4242
* [lua](generators/lua.md)
4343
* [nim (beta)](generators/nim.md)

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

+43-13
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,34 @@
1+
/*
2+
* Copyright 2018 OpenAPI-Generator Contributors (https://openapi-generator.tech)
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
117
package org.openapitools.codegen.languages;
218

19+
import io.swagger.v3.oas.models.OpenAPI;
20+
import io.swagger.v3.oas.models.Operation;
21+
import io.swagger.v3.oas.models.PathItem;
322
import io.swagger.v3.oas.models.info.Info;
423
import io.swagger.v3.oas.models.info.License;
524
import io.swagger.v3.oas.models.media.Schema;
625
import io.swagger.v3.oas.models.parameters.RequestBody;
726
import io.swagger.v3.oas.models.responses.ApiResponse;
827
import io.swagger.v3.oas.models.servers.Server;
9-
import org.openapitools.codegen.*;
10-
import io.swagger.v3.oas.models.*;
1128
import org.apache.commons.lang3.StringUtils;
29+
import org.openapitools.codegen.*;
30+
import org.openapitools.codegen.meta.GeneratorMetadata;
31+
import org.openapitools.codegen.meta.Stability;
1232
import org.openapitools.codegen.utils.ModelUtils;
1333
import org.slf4j.Logger;
1434
import org.slf4j.LoggerFactory;
@@ -19,7 +39,16 @@
1939

2040
import static org.openapitools.codegen.utils.StringUtils.*;
2141

22-
public class k6Codegen extends DefaultCodegen implements CodegenConfig {
42+
public class K6ClientCodegen extends DefaultCodegen implements CodegenConfig {
43+
44+
public K6ClientCodegen() {
45+
super();
46+
47+
generatorMetadata = GeneratorMetadata.newBuilder(generatorMetadata)
48+
.stability(Stability.BETA)
49+
.build();
50+
51+
}
2352

2453
static class Parameter {
2554
String key;
@@ -75,9 +104,9 @@ static class HTTPParameters {
75104
String responseType;
76105

77106
public HTTPParameters(@Nullable String auth, @Nullable List<Parameter> cookies,
78-
@Nullable List<Parameter> headers, @Nullable List<Parameter> jar, @Nullable Integer redirects,
79-
@Nullable List<Parameter> tags, @Nullable Integer timeout, @Nullable String compression,
80-
@Nullable String responseType) {
107+
@Nullable List<Parameter> headers, @Nullable List<Parameter> jar, @Nullable Integer redirects,
108+
@Nullable List<Parameter> tags, @Nullable Integer timeout, @Nullable String compression,
109+
@Nullable String responseType) {
81110
this.auth = auth;
82111
this.cookies = cookies;
83112
this.headers = headers;
@@ -113,7 +142,7 @@ static class HTTPRequest {
113142
List<k6Check> k6Checks;
114143

115144
public HTTPRequest(String method, String path, @Nullable List<Parameter> query, @Nullable HTTPBody body,
116-
@Nullable HTTPParameters params, @Nullable List<k6Check> k6Checks) {
145+
@Nullable HTTPParameters params, @Nullable List<k6Check> k6Checks) {
117146
this.method = method;
118147
this.path = path;
119148
this.query = query;
@@ -144,8 +173,8 @@ public HTTPRequestGroup(String groupName, Set<Parameter> variables, List<HTTPReq
144173
public static final String BASE_URL = "baseURL";
145174
public static final String PRESERVE_LEADING_PARAM_CHAR = "preserveLeadingParamChar";
146175
static final Collection<String> INVOKER_PKG_SUPPORTING_FILES = Arrays.asList("script.mustache", "README.mustache");
147-
static final String[][] JAVASCRIPT_SUPPORTING_FILES = new String[][] {
148-
new String[] { "script.mustache", "script.js" }, new String[] { "README.mustache", "README.md" } };
176+
static final String[][] JAVASCRIPT_SUPPORTING_FILES = new String[][]{
177+
new String[]{"script.mustache", "script.js"}, new String[]{"README.mustache", "README.md"}};
149178

150179
protected String projectName;
151180
protected String moduleName;
@@ -171,7 +200,7 @@ public String getName() {
171200

172201
@Override
173202
public String getHelp() {
174-
return "Generates k6 script.";
203+
return "Generates a k6 script (beta).";
175204
}
176205

177206
@Override
@@ -402,7 +431,8 @@ public void preprocessOpenAPI(OpenAPI openAPI) {
402431
supportingFiles.add(new SupportingFile(templateFile, folder, supportingTemplateFile[1]));
403432
}
404433
}
405-
//
434+
435+
//
406436
private String generateNestedModelTemplate(CodegenModel model) {
407437
StringBuilder reference = new StringBuilder();
408438
int modelEntrySetSize = model.getAllVars().size();
@@ -430,13 +460,13 @@ private String getDoubleQuotedString(String input) {
430460

431461
/**
432462
* Concatenates an array of path segments into a path string.
433-
*
463+
*
434464
* @param segments The path segments to concatenate. A segment may contain
435465
* either of the file separator characters '\' or '/'. A segment
436466
* is ignored if it is <code>null</code>, empty or
437467
* &quot;.&quot;.
438468
* @return A path string using the correct platform-specific file separator
439-
* character.
469+
* character.
440470
*/
441471
private String createPath(String... segments) {
442472
StringBuilder buf = new StringBuilder();

‎modules/openapi-generator/src/main/resources/META-INF/services/org.openapitools.codegen.CodegenConfig

+1-1
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ org.openapitools.codegen.languages.JavascriptClientCodegen
6363
org.openapitools.codegen.languages.JavascriptFlowtypedClientCodegen
6464
org.openapitools.codegen.languages.JavascriptClosureAngularClientCodegen
6565
org.openapitools.codegen.languages.JMeterClientCodegen
66-
org.openapitools.codegen.languages.k6Codegen
66+
org.openapitools.codegen.languages.K6ClientCodegen
6767
org.openapitools.codegen.languages.LuaClientCodegen
6868
org.openapitools.codegen.languages.MysqlSchemaCodegen
6969
org.openapitools.codegen.languages.NimClientCodegen

‎modules/openapi-generator/src/main/resources/k6/licenseInfo.mustache

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
* Contact: {{infoEmail}}
1010
{{/infoEmail}}
1111
*
12-
* NOTE: This class is auto generated by the swagger code generator program.
12+
* NOTE: This class is auto generated by OpenAPI Generator.
1313
* https://github.com/OpenAPITools/openapi-generator
1414
*
1515
* OpenAPI generator version: {{generatorVersion}}

‎samples/client/petstore/k6/script.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44
*
55
* OpenAPI spec version: 1.0.0
66
*
7-
* NOTE: This class is auto generated by the swagger code generator program.
8-
* https://github.com/swagger-api/swagger-codegen.git
7+
* NOTE: This class is auto generated by OpenAPI Generator.
8+
* https://github.com/OpenAPITools/openapi-generator
99
*
10-
* Swagger Codegen version: 4.3.0-SNAPSHOT
10+
* OpenAPI generator version: 4.3.0-SNAPSHOT
1111
*/
1212

1313

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# OpenAPI Generator Ignore
2+
# Generated by openapi-generator https://github.com/openapitools/openapi-generator
3+
4+
# Use this file to prevent files from being overwritten by the generator.
5+
# The patterns follow closely to .gitignore or .dockerignore.
6+
7+
# As an example, the C# client generator defines ApiClient.cs.
8+
# You can make changes and tell OpenAPI Generator to ignore just this file by uncommenting the following line:
9+
#ApiClient.cs
10+
11+
# You can match any string of characters against a directory, file or extension with a single asterisk (*):
12+
#foo/*/qux
13+
# The above matches foo/bar/qux and foo/baz/qux, but not foo/bar/baz/qux
14+
15+
# You can recursively match patterns against a directory, file or extension with a double asterisk (**):
16+
#foo/**/qux
17+
# This matches foo/bar/qux, foo/baz/qux, and foo/bar/baz/qux
18+
19+
# You can also negate patterns with an exclamation (!).
20+
# For example, you can ignore all files in a docs folder with the file extension .md:
21+
#docs/*.md
22+
# Then explicitly reverse the ignore rule for a single file:
23+
#!docs/README.md
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
4.3.0-SNAPSHOT

‎samples/config/petstore/k6/README.md

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Generated k6 script
2+
3+
The `script.js` file contains most of the Swagger/OpenAPI specification and you can customize it to your needs.
4+
5+
Global header variables are defined at the top of the file, like `api_key`. Each path in the specification is converted into a [group](https://docs.k6.io/docs/tags-and-groups) in k6 and each group contains all the request methods related to that path. Path and query parameters are extracted from the specification and put at the start of the group. The URL is constructed from the base URL plus path and query.
6+
7+
k6 specific parameters are in the [`params`](https://docs.k6.io/docs/params-k6http) object, and `body` contains the [request](https://docs.k6.io/docs/http-requests) body which is in the form of `identifier: type`, which the `type` should be substituted by a proper value. Then goes the request and the check.
8+
9+
[Check](https://docs.k6.io/docs/checks) are like asserts but differ in that they don't halt execution, instead they just store the result of the check, pass or fail, and let the script execution continue.
10+
11+
Each request is always followed by a 0.1 second [sleep](https://docs.k6.io/docs/sleep-t-1) to prevent the script execution from flooding the system with too many requests simultaneously.
12+
13+
Note that the default iteration count and VU count is 1. So each request in each group will be executed once. For more information, see the [k6 options](https://docs.k6.io/docs/options).

0 commit comments

Comments
 (0)
This repository has been archived.