Created by: ideas-into-software
This addresses: https://github.com/OpenAPITools/openapi-generator/issues/8378
Overview
If the Swagger/OpenAPI specification used as the input spec contains examples at parameter level, those will be extracted and utilized as parameter values.
The handleParamValue
custom Mustache lambda, registered for use in the K6 script.mustache
template, handles the conditional checks, formatting, and outputting of parameter values.
If a given parameter has value specified – either in example
or examples
field, defined at the parameter level – that value will be used.
For list (examples
), entire list will be output in the generated script and the first element from that list will be assigned as parameter value.
If a given parameter does not have an example defined, a placeholder value with TODO_EDIT_THE_
prefix will be generated for that parameter, and you will have to assign a value before you can run the script.
In other words, you can now generate K6 test scripts which are ready to run, provided the Swagger/OpenAPI specification used as the input spec contains examples for all of the path/query parameters; see modules/openapi-generator/src/test/resources/3_0/examples.yaml
for an example of such specification, and https://swagger.io/docs/specification/adding-examples/ for more information about adding examples.
Thanks to these enhancements, OpenAPI / Swagger spec can be utilized as the single source of truth for automated test generation, including extracting of test data from examples embedded in spec.
Data entered once can then be re-used by OpenAPI code generation tools such as the K6 OpenAPI generator. This allows for full round-trip flow and ensures REST API tests are always up to date.
Generate script based on Swagger/OpenAPI specification with examples
Generate script based on the modules/openapi-generator/src/test/resources/3_0/examples.yaml
spec:
$ ./mvnw clean install
$ java -jar modules/openapi-generator-cli/target/openapi-generator-cli.jar generate \
-i modules/openapi-generator/src/test/resources/3_0/examples.yaml \
-g k6 \
-o /var/tmp/k6_examples \
--skip-validate-spec
Minor fixes
Aside from the above mentioned enhancements, following minor fixes were applied:
-
LoggerFactory.getLogger
– logger named afterK6ClientCodegen.class
, instead ofJavascriptClientCodegen.class
-
Inside
org.openapitools.codegen.languages.K6ClientCodegen.preprocessOpenAPI(OpenAPI)
method,List<CodegenParameter> formParameters
instead ofList<CodegenParameter> formParameteres
-
modules/openapi-generator/src/test/resources/3_0/examples.yaml
– fixed problems which caused errors when using as input spec
PR checklist
-
Read the contribution guidelines. -
Pull Request title clearly describes the work in the pull request and Pull Request description provides details about how to validate the work. Missing information here may result in delayed response from the community. -
Run the following to build the project and update samples: ./mvnw clean package ./bin/generate-samples.sh ./bin/utils/export_docs_generators.sh
./bin/generate-samples.sh bin/configs/java*
. For Windows users, please run the script in Git BASH. -
File the PR against the correct branch: master
,5.1.x
,6.0.x
-
If your PR is targeting a particular programming language, @mention the technical committee members, so they are more likely to review the pull request.