β οΈ This still is a beta software and not guranteed to work. Help this project by reporting errors in the code examples!
Generates sample code for different languages and inserts them in you OpenApi specification file as x-codeSamples
.
Tools like redoc will provide those examples in the documentation.
- cURL
- PHP
- JavaScript (Cookies not supported)
- application/xml
- text/xml
- application/json
- text/json
- application/x-www-form-urlencoded
- multipart/form-data
- basic
- apikey
- cookie
- header
- openidconnect
- oauth2
- Embedding
- AnyOf
- AllOf
- OneOf
/pet/findByTags:
get:
...
x-codeSamples:
- label: curl
lang: curl
source: 'curl "http://petstore.swagger.io/v2/pet/findByTags?tags%5B%5D=example-string"
-H "Authorization: Bearer ${TOKEN}" -d "" -X GET'
You can find more examples in the Examples
folder.
$ go install github.com/CubicrootXYZ/openapi-code-sample-generator@latest
or chose a version from the Github releases:
$ go install github.com/CubicrootXYZ/openapi-code-sample-generator@v0.0.1
Depending on your environment the module is now available with
openapi-code-sample-generator
or you need to search for the binary, those are common places:
/home/$user/go/bin/openapi-code-sample-generator
$GOPATH/bin
To add code samples:
$ openapi-code-sample-generator generate --input-file example.yaml --output-file out.yaml
To convert OpenAPI 2 specifications to OpenAPI 3:
$ openapi-code-sample-generator convert --file example.yaml --output-file out.yaml
For getting help:
$ openapi-code-sample-generator --help
$ openapi-code-sample-generator generate --help
$ openapi-code-sample-generator convert --help
Try it without the @latest
version tag - older go versions might not be able to work with it.
Update your go version to go 1.16.
go install
usually builds a binary in $GOPATH/bin
if you can not find it there you might just search the whole file system for it:
$ cd / && find * | grep openapi-code-sample-generator
Will show you all directories and files with the modules name.
You then need to execute this binary, e.g.:
$ /home/michael/go/bin/openapi-code-sample-generator generate --help
Or add it to your PATH
variable.
I really enjoy any help and contribution to this project. Feel free to work on open issues or new features.
Please submit your issues or specific feature requests as "Issues". Be as precise as possible.
Fork this repository and add your changes. Open a pull request to merge them in the master branch of this repository.
Great thanks to the maintainers of the used libraries and all contributors.