Skip to main content

Creating swagger/OAS specs for an existing api by hand is tedious and error-prone. swaggergenerator fixes this by creating schemas from example interactions:

Generation is a three step process. Here’s an example using httpbin:

import requests
from swaggergenerator import Generator, get_yaml

# 1: Create a Generator.
generator = Generator()

# 2: Provide one or more examples. They can be for different paths and verbs.
response = requests.get('https://httpbin.org/get')
generator.provide_example(response.request, response)

# 3: Generate a schema (specifically, a Swagger Paths Object).
print get_yaml(generator.generate_paths())
/get:
  get:
    description: TODO
    parameters: []
    responses:
      '200':
        description: TODO
        schema:
          additionalProperties: false
          properties:
            args:
              additionalProperties: false
              properties: {}
              type: object
            headers:
              additionalProperties: false
              properties:
                Accept:
                  type: string
                Accept-Encoding:
                  type: string
                Connection:
                  type: string
                Content-Length:
                  type: string
                Host:
                  type: string
                User-Agent:
                  type: string
              type: object
            origin:
              type: string
            url:
              type: string
          type: object

You can install it with $ pip install swaggergenerator.

Generation details

Generally, the generated schemas err on the side of being too strict. For example, additionalProperties is always set to False and parameters are always required. The recommended workflow is to generate schemas, validate them against all interactions in your test suite, and iterate until tests pass.

Here are the swagger features you can expect to be generated:

  • path objects for arbitrary verb/path combinations

  • all-digit path parameters

  • complex path parameters (when given alongside an all-digit example)

  • request schemas for 2xx responses

  • response schemas for 2xx responses

  • references to existing definitions

Here are some swagger features that won’t be generated. If your api uses any of these, you’ll need to fix up your output manually:

  • nullable/polymorphic types

  • heterogeneous arrays

  • optional properties

  • additionalProperties != False

Contributing

Inside your vitualenv:

$ cd swaggergenerator
$ pip install -e .
$ pip install -r requirements.txt

To run the tests:

$ py.test tests/

Metadata

Release files for swaggergenerator 0.0.5

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for swaggergenerator 0.0.5
File Size Uploaded Provenance
swaggergenerator-0.0.5.tar.gz 8.0 kB Without provenance Details

Release files / swaggergenerator-0.0.5.tar.gz

Download URL swaggergenerator-0.0.5.tar.gz
Size 8.0 kB
Tags Source
SHA-256 checksum
How to use checksums
96f45c8bbd5d6b57c19e80e313d4ad1b720b6252d434a633e13e95ae316c0681
BLAKE2b-256 checksum
How to use checksums
1e56e5a6ca09fccb2345f7672bd900e4db480b7991052dee819f1b3056a8135f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Security for swaggergenerator 0.0.5

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.

Release history Release notifications | RSS feed

This release

0.0.5 This release

1 release file No provenance

0.0.4

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page