4

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.

1 Answer 1

1

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'}
                }
            }
        )
    )
)
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you. This is very cool, but judging the example: 1. It doesn't generate and serve a swagger UI. 2. It integrates (very loosely, missing swagger UI) with Flask, which anyway uses annotations for the routes (I can't use annotations for anything). APIspec may be useful to build another solution (w\o starting from scratch) but I cannot use it in its current state.
I have found that Falcon does not need annotations. It may be possible to generate the swagger.json with apispec and serve it as a static page. But generating the swagger UI would still be a challenge.
I found a small example that generates a swagger ui with Falcon, but it seems to be using annotations.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.