APISpec Web Framework Plugins
apispec-webframeworks is a Python library providing plugins to integrate `apispec` with various web frameworks like Flask, Aiohttp, Bottle, and Tornado. These plugins were originally part of `apispec.ext` but were moved to this separate package to streamline `apispec`'s core. It is currently at version 1.2.0 and receives updates to maintain compatibility with `apispec` and relevant frameworks.
Warnings
- breaking The web framework plugins (e.g., `FlaskPlugin`, `AiohttpPlugin`) were moved from `apispec.ext` to the dedicated `apispec-webframeworks` package. If upgrading `apispec` from a version older than `1.0.0`, you must update your imports.
- gotcha When using `spec.path(view=...)` for Flask views, the call must be made within an active Flask application context or a `test_request_context` for `apispec` to correctly inspect the view and its route.
- gotcha This library provides *plugins* for web frameworks but does not install the frameworks themselves. You must explicitly install your chosen web framework (e.g., `flask`, `aiohttp`, `bottle`, `tornado`) separately.
- gotcha As of version 1.2.0, `apispec-webframeworks` does not officially provide a plugin for Starlette. If you are looking for Starlette integration, consider alternative packages like `starlette-apispec` or `apispec-plugins`.
- gotcha Support for Flask Blueprints within `apispec-webframeworks.flask.FlaskPlugin` may be limited, potentially requiring manual workarounds or alternative libraries like `flask-apispec` or `apispec-plugins` for full integration. There are open discussions about enhancing this.
Install
-
pip install apispec-webframeworks apispec[marshmallow] flask marshmallow -
pip install apispec-webframeworks
Imports
- FlaskPlugin
from apispec_webframeworks.flask import FlaskPlugin
- AiohttpPlugin
from apispec_webframeworks.aiohttp import AiohttpPlugin
- BottlePlugin
from apispec_webframeworks.bottle import BottlePlugin
- TornadoPlugin
from apispec_webframeworks.tornado import TornadoPlugin
Quickstart
from flask import Flask
from apispec import APISpec
from apispec.ext.marshmallow import MarshmallowPlugin
from apispec_webframeworks.flask import FlaskPlugin
from marshmallow import Schema, fields
# 1. Create an APISpec instance
spec = APISpec(
title="My Awesome API",
version="1.0.0",
openapi_version="3.0.2",
info=dict(description="A minimal Flask API example"),
plugins=[
FlaskPlugin(),
MarshmallowPlugin()
],
)
# 2. Define a Marshmallow Schema
class ItemSchema(Schema):
id = fields.Int(dump_only=True)
name = fields.Str(required=True, description="The item's name")
# 3. Register the schema with APISpec
spec.components.schema("Item", schema=ItemSchema)
# 4. Initialize Flask app
app = Flask(__name__)
# 5. Define a Flask route with OpenAPI docstrings
@app.route("/items/<int:item_id>")
def get_item(item_id):
"""Get item by ID
---
parameters:
- in: path
name: item_id
schema:
type: integer
required: true
description: Numeric ID of the item to retrieve
responses:
200:
description: Item details
content:
application/json:
schema: ItemSchema
404:
description: Item not found
"""
# In a real app, you'd fetch from a DB
if item_id == 1:
return ItemSchema().dump({'id': 1, 'name': 'Sample Item'})
return {"message": "Item not found"}, 404
# 6. Register the Flask path with APISpec (must be in request context)
with app.test_request_context():
spec.path(view=get_item)
# 7. Add an endpoint to serve the OpenAPI spec
@app.route("/swagger.json")
def swagger_spec():
return spec.to_dict()
# To run this example:
# 1. Save as app.py
# 2. Run: flask run
# 3. Access: http://127.0.0.1:5000/swagger.json