Trylks
Trylks

Reputation: 1498

Generate Swagger specification from Python code without annotations

I am searching for a way to generate a Swagger specification (the JSON and Swagger-UI) from the definition of a Python service. I have found many options (normally Flask-based), but all of them use annotations, which I cannot properly handle from within the code, e.g. define 10 equivalent services with different routes in a loop (the handler could be currified in this example, first function call to obtain it).

Is there any way to do this without annotations? I would like to generate the method in the API and its documentation by calling a function (or a method, or a constructor of a class) with the values corresponding to the implementation and the documentation of that API method.

Upvotes: 4

Views: 8000

Answers (1)

ge7600
ge7600

Reputation: 399

I suggest looking into apispec.

apispec is a pluggable API specification generator.

Currently supports the OpenAPI 2.0 specification (f.k.a. Swagger 2.0)

apispec

from apispec import APISpec

spec = APISpec(
    title='Gisty',
    version='1.0.0',
    info=dict(description='A minimal gist API')
)

spec.definition('Gist', properties={
    'id': {'type': 'integer', 'format': 'int64'},
    'content': {'type': 'string'},
})

spec.add_path(
    path='/gist/{gist_id}',
    operations=dict(
        get=dict(
            responses={
                '200': {
                    'schema': {'$ref': '#/definitions/Gist'}
                }
            }
        )
    )
)

Upvotes: 1

Related Questions