Skip to main content

OpenAPI specification generator for TurboGears2 controllers

Project description

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.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

tgext_apispec-0.1.1.tar.gz (9.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

tgext_apispec-0.1.1-py3-none-any.whl (6.7 kB view details)

Uploaded Python 3

File details

Details for the file tgext_apispec-0.1.1.tar.gz.

File metadata

  • Download URL: tgext_apispec-0.1.1.tar.gz
  • Upload date:
  • Size: 9.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.8.20

File hashes

Hashes for tgext_apispec-0.1.1.tar.gz
Algorithm Hash digest
SHA256 780ef7bf175a71acb4b5738d3e1285983530e3741e966e4b2f1e7b10ac4a7a03
MD5 976466cbc0c222f56302311e91154177
BLAKE2b-256 2e4676595349483b6428d1fe08065013664176280af83680b05f1caabf3bc728

See more details on using hashes here.

File details

Details for the file tgext_apispec-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: tgext_apispec-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 6.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.8.20

File hashes

Hashes for tgext_apispec-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 6d93e1ebf1227805d7b3328deb54cc79cdd5048981ea54c0308c1d0d53821782
MD5 480aa4ea817596dd4aaacdac534b666b
BLAKE2b-256 ff97214df8eb6a3525d1f71b5401f9f6d3caac5e415664f545eeadb9ab1d6c41

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page