Skip to main content

tgext.apispec

OpenAPI specification generator for TurboGears2 controllers.

This extension automatically generates OpenAPI specifications from TurboGears controllers by introspecting @validate decorators and docstrings.

Installation

pip install tgext.apispec

Usage

Basic usage in your TurboGears application:

import tg
from tg import expose
from tg.controllers import TGController
from tgext.apispec.openapi import get_spec

class RootController(TGController):
    @expose("json")
    def openapi(self):
        # Each call builds the complete specification.
        return get_spec(self, prefix=tg.url('/api')).to_dict()

# ``prefix`` is caller-controlled; this example generates paths under
# ``/api``. Child controllers are discovered recursively.

Controller Example

from tg import expose, validate
from tg.controllers import RestController
from tg.validation import Convert, RequireValue

class MoviesController(RestController):
    @expose('json')
    @validate({
        'title': RequireValue(),
        'year': Convert(int, default=None),
    })
    def post(self, title, year=None):
        """Create a new movie.

        ---
        tags: [movies]
        responses:
          201:
            description: Movie created
          412:
            description: Validation error
        """
        # Your implementation
        ...

The extension will automatically:

  • Extract parameter types from @validate decorators

  • Parse docstrings for descriptions and metadata using a --- separator

  • Generate proper OpenAPI schema for each endpoint

Features

  • Automatic Parameter Extraction: Extracts parameter types from @validate decorators

  • Docstring Support: Uses docstrings for descriptions, tags, and response definitions

  • RestController Support: Properly handles RestController methods (get_all, get_one, post, put, delete)

  • Type Conversion: Converts TG validators to OpenAPI types (int, float, bool, string, email, date, datetime)

  • JSON Endpoints Only: Only generates spec for JSON-exposed endpoints

Docstring Format

Use a --- separator in your docstring to add OpenAPI metadata:

def my_action(self):
    """My action description.

    ---
    tags: [my_tag]
    responses:
      200:
        description: Success
      404:
        description: Not found
    """

Supported docstring metadata:

  • tags: List of tags for the endpoint

  • responses: Response definitions

  • description: Endpoint description, automatically extracted from the first part of the docstring

Development

The package targets TurboGears 2.5.1, which is expected to come from the TurboGears development branch until that version is released.

python -m pip install "TurboGears2 @ git+https://github.com/TurboGears/tg2.git@development"
python -m pip install -e ".[testing,lint]"
python -m pytest
python -m ruff format --check .
python -m ruff check .

License

MIT License. See LICENSE for details.

Release files for tgext.apispec 0.1.2

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

Source distribution (sdist)

Source distribution for tgext.apispec 0.1.2
File Size Uploaded
tgext_apispec-0.1.2.tar.gz 9.3 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for tgext.apispec 0.1.2
File Interpreter ABI Platform
tgext_apispec-0.1.2-py3-none-any.whl Python 3 none any Details

Total release size: 16.0 kB

Release files / tgext_apispec-0.1.2.tar.gz

Download URL tgext_apispec-0.1.2.tar.gz
Size 9.3 kB
Tags Source
SHA-256 checksum
How to use checksums
e89612c1aec1279b0e827e35dbe816f7e10b7847e25a081babd4fd1e8591b8e4
BLAKE2b-256 checksum
How to use checksums
5e5ac32c6b6b0f603d5db67a1fb216df70fcbe77d9074a28175b90148e5b1e09
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.8.20

Release files / tgext_apispec-0.1.2-py3-none-any.whl

Download URL tgext_apispec-0.1.2-py3-none-any.whl
Size 6.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
cf294cd80898163c948c4ed514cab636df1cba12359f16a6f7b90d5a3186669d
BLAKE2b-256 checksum
How to use checksums
8c6f49f065638971f76ebaa24554cdf9c290dd211809b8d50eb34143a7acc778
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.8.20

Release history Release notifications | RSS feed

This release

0.1.2 This release

2 release files

0.1.1

2 release files

0.1.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