-
Notifications
You must be signed in to change notification settings - Fork 3
Open
Labels
claudeIssues created by ClaudeIssues created by Claude
Description
Overview
Add OpenAPI/Swagger documentation to DigiScript's REST API, including an interactive Swagger UI page for exploring and testing endpoints.
Motivation
- Developer Experience: Interactive API documentation makes it easier for contributors to understand available endpoints
- Client Development: Auto-generated API specs can be used to generate client SDKs
- Testing: Swagger UI provides built-in API testing capabilities
- Maintenance: Auto-generated docs stay in sync with code changes
Research Summary
After researching available options for Python Tornado + Marshmallow stack, there are three viable approaches:
Option 1: apispec + apispec-webframeworks (Recommended)
Pros:
- Official Marshmallow integration - can auto-generate schemas from our existing
SQLAlchemyAutoSchemaclasses - OpenAPI 3.0 support
- Most flexible and well-maintained (actively developed by marshmallow-code org)
- Works with our existing Marshmallow schemas in
schemas/schemas.py - Tornado plugin available via
apispec-webframeworks
Cons:
- Requires more manual setup for path registration
- Need to write code to generate and serve the spec file
Example Integration:
from apispec import APISpec
from apispec.ext.marshmallow import MarshmallowPlugin
from apispec_webframeworks.tornado import TornadoPlugin
spec = APISpec(
title="DigiScript API",
version="1.0.0",
openapi_version="3.0.0",
plugins=[MarshmallowPlugin(), TornadoPlugin()],
)
# Auto-generates schema from existing Marshmallow schemas
spec.components.schema("Show", schema=ShowSchema)References:
Option 2: tornado-swagger
Pros:
- Simple decorator-based approach
- Built-in Swagger UI at
/api/doc - Minimal code changes needed
Cons:
- Requires embedding YAML specs in docstrings
- Less integration with existing Marshmallow schemas
- May require duplicating schema definitions
Example:
@ApiRoute("/show/script", ApiVersion.V1)
class ScriptController(BaseAPIController):
@requires_show
def get(self):
"""
---
tags:
- Script
summary: Get script page
parameters:
- name: page
in: query
required: true
schema:
type: integer
responses:
200:
description: Script page retrieved
"""References:
Option 3: tornado-swirl
Pros:
- OpenAPI 3.0 native support
- Uses Google-style docstrings (more readable than YAML)
- Adapted for modern Tornado 5+ and Python 3
Cons:
- Less maintained than apispec
- Requires
@restapidecorator for route registration (different from our@ApiRoute) - May require refactoring our routing system
References:
Implementation Considerations
Integration with Existing Architecture
DigiScript uses:
- Custom routing:
@ApiRoutedecorator with automatic/api/v{version}/prefix - Existing Marshmallow schemas: Already defined in
schemas/schemas.pyusingSQLAlchemyAutoSchema - Auto-discovery: Controllers in
controllers/api/are auto-loaded viamodule_discovery.py
Recommended Approach
Option 1 (apispec) appears best because:
- ✅ Reuses existing Marshmallow schemas (no duplication)
- ✅ Mature, actively maintained project
- ✅ OpenAPI 3.0 support
- ✅ Can integrate with our custom
@ApiRoutedecorator
Implementation Steps
-
Add dependencies:
pip install apispec apispec-webframeworks swagger-ui-py
-
Create spec generator (
utils/api_spec.py):- Initialize APISpec with Marshmallow + Tornado plugins
- Auto-register schemas from
schemas/schemas.py - Create decorator or helper to register routes from
@ApiRoutecontrollers
-
Add Swagger UI endpoint:
- New controller at
/api/docsserving Swagger UI - Spec JSON endpoint at
/api/openapi.json
- New controller at
-
Document existing endpoints:
- Add docstrings to controller methods
- Use
spec.path()to register routes with parameters/responses
-
Update documentation:
- Add link to API docs in README
- Document how to add specs for new endpoints
Acceptance Criteria
- OpenAPI 3.0 spec generated from existing Marshmallow schemas
- Interactive Swagger UI accessible at
/api/docs - OpenAPI spec JSON available at
/api/openapi.json - At least 5-10 key endpoints documented as examples
- Documentation in README on how to add API docs for new endpoints
- CI checks pass (linting, tests)
Additional References
Metadata
Metadata
Assignees
Labels
claudeIssues created by ClaudeIssues created by Claude