SwagmanMock
A postman to swagger conversion tool, which automatically
- Converts your postman collection to OpeanAPI Spec (3.0.0)
- Mocks your openapi collection to generate responses from postman examples
Other than these, this tool can easily handle ignored fields in responses (exaplined below)
Installation
NOTE This repo needs you to have python 3.5+ installed
As of now, I haven't pushed this tool to pipy repo yet. Hence, its clone only for now.
PIP
pip install swagmanmock
Manual
To install, simple clone this repo
git clone https://github.com/codeasashu/swagmanmock.git
and install
python setup.py install
Quick Start
This tool can be used as a python package or as a standalone cli.
To start, simply type swagmanmock --help and it will display help
Usage: swagmanmock [OPTIONS] COMMAND [ARGS]...
Convert or mock your postman collection to openapi schema
Options:
--help Show this message and exit.
Commands:
convert
mock
Convert postman to openapi (swagger) spec
Easy!! Just use convert command (default output is yaml)
swagmanmock convert postman-collection.json spec.yaml
Or, you can output to json by
swagmanmock convert -f json postman-collection.json spec.yaml
Mocking spec
I am using the some cherry on top of the awesome project Connexion
Basically, I am using postman example as mock responses, given the request has matching parameters (query, headers etc.). Even if they do not match, this tool gives out the mock responses for provided schema.
swagmanmock mock spec.yaml
Ignore schema
Sometimes, your api responses have some data which varies. For instance, consider this response for the api POST /user:
{
"result": {
"timestamp": 1572696732,
"username": "abc",
"tags": {
"tag1" : "something",
"tag3": "somethig else"
},
"some-changing-key": "whatever"
}
}
You do want to record the username, timestamp fields, but what about some-changing-key field? What about fields inside tags? You want to keep the tags key as it will always be included in response, but do not want to keep some-changing-key as it may or maynot appear in responses.
Sometimes you may want to ignore only the values of a key, while sometimes you want the key value pair to be ignored alltogether
For such cases, you may not want to document them. For such purpose, Ignore file is used.
In ignore file, you can document the fields you want the swagman to ignore. It uses the jsonpath-rw library and uses its syntax (which is quite easy to learn).
To ignore only values but keep the keys, simple use the jsonpath-rw syntax that points to the key. For ex- $.result.tags.[*] will find everything inside tags field in result object.
To ignore both key and values, simply use the above method, i.e. write your jsonpath-rw regex that matches the path, and append :a to it. For example, if you want to delete everything inside tag including tag field itself, you can do so by: $.result.tags.[*]:a
Taking above example, you want to ignore following fields:
- everything inside
tags(ignore value but NOT the keytags) some-changing-keyfield (ignore both key and value)
You can define them in a file ignore.yaml as such:
schema:
/user:
post:
200:
- '$.result.tags.[*]' //Ignore everything inside tags field
- '$.result.some-changing-key:a' //Ignore 'some-changing-key'. Note the leading :a
and then you can convert your postman collection to swagger definition without these fields:
swagmanmock -i ignore.yaml postman-collection.json spec.yaml
PS: Leading :a in jsonpath-rw syntax with ignore both the key and values, otherwise only values are ignored.
Change spec format
The default output conversion format is yaml. However, you can easily change the format to json by:
swagmanmock -f json postman-collection.json spec.json
Metadata
Release files for SwagmanMock 0.1.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| SwagmanMock-0.1.0.tar.gz | 12.7 kB | Without provenance | Details for SwagmanMock-0.1.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| SwagmanMock-0.1.0-py3-none-any.whl | Python 3 | none | any | Details for SwagmanMock-0.1.0-py3-none-any.whl |
Total release size: 40.1 kB
Release files / SwagmanMock-0.1.0.tar.gz
| Download URL | SwagmanMock-0.1.0.tar.gz |
|---|---|
| Size | 12.7 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
72317be839c65a8b11fa1592672312bb4a38994cdc5413edaee73111cce68f3f
|
|
BLAKE2b-256 checksum How to use checksums |
fd7a953381a0430a89455cb2ba7516e76dcb93c8c57b28dd74f7e48f2d56ed98
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.1.0 pkginfo/1.5.0.1 requests/2.20.1 setuptools/42.0.0 requests-toolbelt/0.9.1 tqdm/4.39.0 CPython/3.7.4
|
Release files / SwagmanMock-0.1.0-py3-none-any.whl
| Download URL | SwagmanMock-0.1.0-py3-none-any.whl |
|---|---|
| Size | 27.4 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
8b5d7eb10600cfe12cb953fee8cb56b4202da4aff8325935eec2d56456659285
|
|
BLAKE2b-256 checksum How to use checksums |
6e12a1e0b457efa3208e6c63dc463c1ff0be67aa61449a637be9186fe0076503
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.1.0 pkginfo/1.5.0.1 requests/2.20.1 setuptools/42.0.0 requests-toolbelt/0.9.1 tqdm/4.39.0 CPython/3.7.4
|
Security for SwagmanMock 0.1.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.