Skip to content
GitLab
Projects Groups Snippets
  • /
  • Help
    • Help
    • Support
    • Community forum
    • Submit feedback
    • Contribute to GitLab
  • Sign in / Register
  • O openapi-generator
  • Project information
    • Project information
    • Activity
    • Labels
    • Members
  • Repository
    • Repository
    • Files
    • Commits
    • Branches
    • Tags
    • Contributors
    • Graph
    • Compare
  • Issues 3,476
    • Issues 3,476
    • List
    • Boards
    • Service Desk
    • Milestones
  • Merge requests 402
    • Merge requests 402
  • CI/CD
    • CI/CD
    • Pipelines
    • Jobs
    • Schedules
  • Deployments
    • Deployments
    • Environments
    • Releases
  • Packages and registries
    • Packages and registries
    • Package Registry
    • Infrastructure Registry
  • Monitor
    • Monitor
    • Incidents
  • Analytics
    • Analytics
    • Value stream
    • CI/CD
    • Repository
  • Wiki
    • Wiki
  • Snippets
    • Snippets
  • Activity
  • Graph
  • Create a new issue
  • Jobs
  • Commits
  • Issue Boards
Collapse sidebar
  • OpenAPI Tools
  • openapi-generator
  • Merge requests
  • !9750

[ K6 Generator ] Support for extracting examples defined at parameter level of Swagger/OpenAPI specification, plus minor fixes

  • Review changes

  • Download
  • Email patches
  • Plain diff
Merged Administrator requested to merge github/fork/DataInMotion/k6-openapi-extract-examples into master Jun 11, 2021
  • Overview 0
  • Commits 3
  • Pipelines 0
  • Changes 7

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 after K6ClientCodegen.class, instead of JavascriptClientCodegen.class

  • Inside org.openapitools.codegen.languages.K6ClientCodegen.preprocessOpenAPI(OpenAPI) method, List<CodegenParameter> formParameters instead of List<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
    Commit all changed files. 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.
  • 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.
Assignee
Assign to
Reviewers
Request review from
Time tracking
Source branch: github/fork/DataInMotion/k6-openapi-extract-examples