apispec 6.10.0
可插拔的 API 规范生成器。目前支持 OpenAPI Specification(原名 Swagger specification)。
pip install apispec 复制 PIP 安装说明
可插拔的 API 规范生成器。目前支持 OpenAPI Specification(原名 Swagger specification)。
功能
支持 OpenAPI Specification(版本 2 和 3)
与框架无关
内置支持 marshmallow
提供用于解析文档字符串的工具
安装
$ pip install -U apispec
使用 marshmallow 插件时,请确保使用兼容的 marshmallow 版本:
$ pip install -U apispec[marshmallow]
示例应用
from apispec import APISpec
from apispec.ext.marshmallow import MarshmallowPlugin
from apispec_webframeworks.flask import FlaskPlugin
from flask import Flask
from marshmallow import Schema, fields
# 创建 APISpec
spec = APISpec(
title="Swagger Petstore",
version="1.0.0",
openapi_version="3.0.2",
plugins=[FlaskPlugin(), MarshmallowPlugin()],
)
# 可选的 marshmallow 支持
class CategorySchema(Schema):
id = fields.Int()
name = fields.Str(required=True)
class PetSchema(Schema):
category = fields.List(fields.Nested(CategorySchema))
name = fields.Str()
# 可选的安全方案支持
api_key_scheme = {"type": "apiKey", "in": "header", "name": "X-API-Key"}
spec.components.security_scheme("ApiKeyAuth", api_key_scheme)
# 可选的 Flask 支持
app = Flask(__name__)
@app.route("/random")
def random_pet():
"""一个可爱的毛茸茸动物接口。
---
get:
description: 获取一只随机宠物
security:
- ApiKeyAuth: []
responses:
200:
content:
application/json:
schema: PetSchema
"""
pet = get_random_pet()
return PetSchema().dump(pet)
# 注册路径及其中的实体
with app.test_request_context():
spec.path(view=random_pet)
生成的 OpenAPI 规范
import json
print(json.dumps(spec.to_dict(), indent=2))
# {
# "paths": {
# "/random": {
# "get": {
# "description": "获取随机宠物",
# "security": [
# {
# "ApiKeyAuth": []
# }
# ],
# "responses": {
# "200": {
# "content": {
# "application/json": {
# "schema": {
# "$ref": "#/components/schemas/Pet"
# }
# }
# }
# }
# }
# }
# }
# },
# "tags": [],
# "info": {
# "title": "Swagger Petstore",
# "version": "1.0.0"
# },
# "openapi": "3.0.2",
# "components": {
# "parameters": {},
# "responses": {},
# "schemas": {
# "Category": {
# "type": "object",
# "properties": {
# "name": {
# "type": "string"
# },
# "id": {
# "type": "integer",
# "format": "int32"
# }
# },
# "required": [
# "name"
# ]
# },
# "Pet": {
# "type": "object",
# "properties": {
# "name": {
# "type": "string"
# },
# "category": {
# "type": "array",
# "items": {
# "$ref": "#/components/schemas/Category"
# }
# }
# }
# }
# "securitySchemes": {
# "ApiKeyAuth": {
# "type": "apiKey",
# "in": "header",
# "name": "X-API-Key"
# }
# }
# }
# }
# }
print(spec.to_yaml())
# components:
# parameters: {}
# responses: {}
# schemas:
# Category:
# properties:
# id: {format: int32, type: integer}
# name: {type: string}
# required: [name]
# type: object
# Pet:
# properties:
# category:
# items: {$ref: '#/components/schemas/Category'}
# type: array
# name: {type: string}
# type: object
# securitySchemes:
# ApiKeyAuth:
# in: header
# name: X-API-Key
# type: apiKey
# info: {title: Swagger Petstore, version: 1.0.0}
# openapi: 3.0.2
# paths:
# /random:
# get:
# description: 获取随机宠物
# responses:
# 200:
# content:
# application/json:
# schema: {$ref: '#/components/schemas/Pet'}
# security:
# - ApiKeyAuth: []
# tags: []
文档
文档请参阅 https://apispec.readthedocs.io/。
生态系统
GitHub Wiki 中列出了与 apispec 相关的库:
https://github.com/marshmallow-code/apispec/wiki/Ecosystem
支持 apispec
apispec 由一群 志愿者 维护。如果你愿意支持项目未来的发展,欢迎考虑向我们的 Open Collective 捐款:
专业支持
你可以通过 Tidelift Subscription 获得专业支持的 apispec。
Tidelift 为软件开发团队提供统一的软件采购和维护渠道,并由最了解这些软件的专家提供专业级保障,同时还能与现有工具无缝集成。[获取专业支持]
安全联系信息
如需报告安全漏洞,请使用 Tidelift 安全联系渠道。 Tidelift 将协调修复和漏洞披露工作。
项目链接
更新日志:https://apispec.readthedocs.io/en/latest/changelog.html
贡献指南:https://apispec.readthedocs.io/en/latest/contributing.html
许可证
采用 MIT 许可证。详情请参阅随附的 LICENSE 文件。
项目链接
PyPI 于 2026 年 3 月 6 日核验数据 数据由项目维护者提供,并在版本上传至 PyPI 时完成核验。重要日期
PyPI 数据 数据直接来源于 PyPI 数据库。- 发布日期: 2026 年 3 月 6 日
2 位维护者
PyPI 数据 数据直接来源于 PyPI 数据库。
lafrech
sloria
致谢
作者: Steven Loria
维护者: Steven Loria
GitHub 统计
数据由 PyPI 于 2026 年 3 月 6 日核实 GitHub 源代码仓库由项目维护者提供,并在上传时经 PyPI 核实。Star 数、Fork 数以及未关闭的 Issue/PR 数量均取自该仓库,未经独立核实。许可证
MIT 许可证
运行要求
Python >=3.10
提供的额外依赖
dev
docs
marshmallow
tests
yaml
标签


