Skip to main content

Swagger API Docs Generator for Foliant

The static site on the picture was built with Slate backend together with SwaggerDoc preprocessor

This preprocessor generates Markdown documentation from Swagger spec files. It uses Jinja2 templating engine or Widdershins for generating Markdown from swagger spec files.

Installation

$ pip install foliantcontrib.swaggerdoc

This preprocessor requires Widdershins to be installed on your system (unless you are using Foliant with Full Docker Image):

npm install -g widdershins

Config

To enable the preprocessor, add swaggerdoc to preprocessors section in the project config:

preprocessors:
    - swaggerdoc

The preprocessor has a number of options:

preprocessors:
    - swaggerdoc:
        spec_url: http://localhost/swagger.json
        spec_path: swagger.json
        additional_json_path: tags.json
        mode: widdershins
        template: swagger.j2
        environment: env.yaml
        strict: false

spec_url : URL to Swagger spec file. If it is a list — preprocessor takes the first url which works.

spec_path : Local path to Swagger spec file (relative to project dir).

If both url and path are specified — preprocessor first tries to fetch spec from url, and then (if that fails) looks for the file on local path.

additional_json_path : Only for jinja mode. Local path to swagger spec file with additional info (relative to project dir). It will be merged into original spec file, not overriding existing fields.

mode : Determines how the Swagger spec file would be converted to markdown. Should be one of: jinja, widdershins. Default: widdershins

jinja mode is deprecated. It may be removed in future

template : Only for jinja mode. Path to jinja-template for rendering the generated documentation. Path is relative to the project directory. If no template is specified preprocessor will use default template (and put it into project dir if it was missing). Default: swagger.j2

environment : Only for widdershins mode. Parameters for widdershins converter. You can either pass a string containing relative path to YAML or JSON file with all parameters (like in example above) or specify all parameters in YAML format under this key. More info on widdershins parameters.

strict : If the strict option is enabled, then if a critical error is detected, the build will be aborted after applying the preprocessor.

header_accept : If the header_accept option is enabled, the Accept: text/plain header will be added to HTTP requests when retrieving the Swagger specification file from a URL. This can be useful when the server returns different content types based on the Accept header. By default, this option is enabled (true).

Usage

Add a <swaggerdoc></swaggerdoc> tag at the position in the document where the generated documentation should be inserted:

# Introduction

This document contains the automatically generated documentation of our API.

<swaggerdoc></swaggerdoc>

Each time the preprocessor encounters the tag <swaggerdoc></swaggerdoc> it inserts the whole generated documentation text instead of it. The path or url to Swagger spec file are taken from foliant.yml.

You can also specify some parameters (or all of them) in the tag options:

# Introduction

Introduction text for API documentation.

<swaggerdoc spec_url="http://localhost/swagger.json"
            mode="jinja"
            template="swagger.j2">
</swaggerdoc>

<swaggerdoc spec_url="http://localhost/swagger.json"
            mode="widdershins"
            environment="env.yml">
</swaggerdoc>

Tag parameters have the highest priority.

This way you can have documentation from several different Swagger spec files in one foliant project (even in one md-file if you like it so).

Customizing output

Widdershins

In widdershins mode the output markdown is generated by widdershins Node.js application. It supports customizing the output with doT.js templates.

  1. Clone the original widdershins repository and modify the templates located in one of the subfolders in the templates folder.
  2. Save the modified templates somewhere near your foliant project.
  3. Specify the path to modified templates in the user_templates field of the environment configuration. For example, like this:
preprocessors:
    - swaggerdoc:
        spec_path: swagger.yml
        environment:
            user_templates: !path ./widdershins_templates/

Jinja

jinja mode is deprecated. It may be removed in future

In jinja mode the output markdown is generated by the Jinja2 template. In this template all fields from Swagger spec file are available under the dictionary named swagger_data.

To customize the output create a template which suits your needs. Then supply the path to it in the template parameter.

If you wish to use the default template as a starting point, build the foliant project with swaggerdoc preprocessor turned on. After the first build the default template will appear in your foliant project dir under name swagger.j2.

Tests

To run the tests locally, you will need to install NodeJS. If you have NodeJS installed, then run:

./test.sh

Alternatively, you can also use a Docker to run the tests. To do so, run:

./test_in_docker.sh

Metadata

Release files for foliantcontrib.swaggerdoc 1.2.8

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

Source distribution (sdist)

Source distribution for foliantcontrib.swaggerdoc 1.2.8
File Size Uploaded
foliantcontrib_swaggerdoc-1.2.8.tar.gz 12.2 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for foliantcontrib.swaggerdoc 1.2.8
File Interpreter ABI Platform
foliantcontrib_swaggerdoc-1.2.8-py3-none-any.whl Python 3 none any Details

Total release size: 22.0 kB

Release files / foliantcontrib_swaggerdoc-1.2.8.tar.gz

Download URL foliantcontrib_swaggerdoc-1.2.8.tar.gz
Size 12.2 kB
Tags Source
SHA-256 checksum
How to use checksums
583d5800614bf532069aeadfea8a4b8e707ac926d3429286996c6fb02b151544
BLAKE2b-256 checksum
How to use checksums
0ca94374a27df39f8b0f29dd1645d961fd70f2dfd1298677d093a2003b1bac37
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.9.25

Release files / foliantcontrib_swaggerdoc-1.2.8-py3-none-any.whl

Download URL foliantcontrib_swaggerdoc-1.2.8-py3-none-any.whl
Size 9.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
6193ce8eb62b79db510696c9ac50521bfed0773ad01e1e1d61c14340b99f9fed
BLAKE2b-256 checksum
How to use checksums
1791921ad87f8c4204924436e44d899c7a2c6f167114832681088fb089177ead
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.9.25

Release history Release notifications | RSS feed

This release

1.2.8 This release

2 release files

1.2.6

2 release files

1.2.5

2 release files

1.2.4

2 release files

1.2.3

2 release files

1.2.2

2 release files

1.2.1

2 release files

1.2.0

2 release files

1.1.3

2 release files

1.1.2

2 release files

1.1.1

2 release files

1.1.0

2 release files

1.0.0

2 release files

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