[ K6 Generator ] Support for extracting examples defined at parameter level of Swagger/OpenAPI specification, plus minor fixes#9750
Conversation
|
@ideas-into-software thanks for the PR cc @mostafa |
mostafa
left a comment
There was a problem hiding this comment.
Thanks @ideas-into-software for the PR and well done! I just left a few comments.
| group("{{{groupName}}}", () => { | ||
| {{#variables}} | ||
| let {{{key}}} = "TODO_EDIT_THE_{{{value}}}"; | ||
| let {{{key}}} = {{#lambda.handleParamValue}}{{value}}{{/lambda.handleParamValue}} |
There was a problem hiding this comment.
I think this doesn't need the extra indentation.
| }); | ||
| group("/pet/findByStatus", () => { | ||
| let status = "TODO_EDIT_THE_STATUS"; | ||
| let status = 'TODO_EDIT_THE_STATUS'; // specify value as there is no example value for this parameter in OpenAPI spec |
There was a problem hiding this comment.
The extra indentation in the template is reflected here, which I think is unnecessary.
| let password = 'TODO_EDIT_THE_PASSWORD'; // specify value as there is no example value for this parameter in OpenAPI spec | ||
| let username = 'TODO_EDIT_THE_USERNAME'; // specify value as there is no example value for this parameter in OpenAPI spec |
There was a problem hiding this comment.
Correct me if I'm wrong, but I expect these values to be filled with examples? Or is it another example script altogether? If so, please regenerate that example alongside this one.
There was a problem hiding this comment.
Hi
Thank you for the feedback. My IDE does not understand mustache (yet), so apologies for the indentation problems; I’ll fix it ASAP.
Regarding the samples/client/petstore/k6/script.js, please keep in mind samples are generated based on whatever is defined in the config, and for K6 generator, the config file https://github.com/OpenAPITools/openapi-generator/blob/master/bin/configs/other/k6.yaml uses modules/openapi-generator/src/test/resources/2_0/petstore.yaml as the input spec; unfortunately, that OpenAPI spec (modules/openapi-generator/src/test/resources/2_0/petstore.yaml) does not have any examples, and since it’s used by many other generators I did not want to introduce any changes in such place; this is also the reason why in the docs for this PR I provided example (“Generate script based on Swagger/OpenAPI specification with examples”) based on modules/openapi-generator/src/test/resources/3_0/examples.yaml.
Whether modules/openapi-generator/src/test/resources/2_0/petstore.yaml itself should be updated with examples – perhaps that’s a good question to ask the OpenAPI maintainers?
There was a problem hiding this comment.
modules/openapi-generator/src/test/resources/2_0/petstore.yaml
We usually keep it as it's and create a new one based on it with more edge cases per generator.
|
I'll merge this into master and replace all tabs with 4 spaces in the java files. |
|
Please have a look at f7bc2aa that I made in the master. |
This addresses: #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
handleParamValuecustom Mustache lambda, registered for use in the K6script.mustachetemplate, handles the conditional checks, formatting, and outputting of parameter values.If a given parameter has value specified – either in
exampleorexamplesfield, 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.yamlfor 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.yamlspec:$ ./mvnw clean installMinor fixes
Aside from the above mentioned enhancements, following minor fixes were applied:
LoggerFactory.getLogger– logger named afterK6ClientCodegen.class, instead ofJavascriptClientCodegen.classInside
org.openapitools.codegen.languages.K6ClientCodegen.preprocessOpenAPI(OpenAPI)method,List<CodegenParameter> formParametersinstead ofList<CodegenParameter> formParameteresmodules/openapi-generator/src/test/resources/3_0/examples.yaml– fixed problems which caused errors when using as input specPR checklist
This is important, as CI jobs will verify all generator outputs of your HEAD commit as it would merge with master.
These must match the expectations made by your contribution.
You may regenerate an individual generator by passing the relevant config(s) as an argument to the script, for example
./bin/generate-samples.sh bin/configs/java*.For Windows users, please run the script in Git BASH.
master,5.1.x,6.0.x