可插入的api规范生成器。目前支持openapi规范(f.k.a.swagger规范)。
apispec的Python项目详细描述
可插入的api规范生成器。目前支持OpenAPI Specification(f.k.a.狂妄自大的规范)。
功能
- 支持OpenAPI规范(版本2和3)
- 框架不可知
- 内置支持marshmallow
- 用于分析docstring的实用程序
示例应用程序
fromapispecimportAPISpecfromapispec.ext.marshmallowimportMarshmallowPluginfromapispec_webframeworks.flaskimportFlaskPluginfromflaskimportFlask,jsonifyfrommarshmallowimportSchema,fields# Create an APISpecspec=APISpec(title="Swagger Petstore",version="1.0.0",openapi_version="3.0.2",plugins=[FlaskPlugin(),MarshmallowPlugin()],)# Optional marshmallow supportclassCategorySchema(Schema):id=fields.Int()name=fields.Str(required=True)classPetSchema(Schema):category=fields.Nested(CategorySchema,many=True)name=fields.Str()# Optional Flask supportapp=Flask(__name__)@app.route("/random")defrandom_pet():"""A cute furry animal endpoint. --- get: description: Get a random pet responses: 200: content: application/json: schema: PetSchema """pet=get_random_pet()returnjsonify(PetSchema().dump(pet).data)# Register entities and pathsspec.components.schema("Category",schema=CategorySchema)spec.components.schema("Pet",schema=PetSchema)withapp.test_request_context():spec.path(view=random_pet)
生成的openapi规范
importjsonprint(json.dumps(spec.to_dict(),indent=2))# {# "paths": {# "/random": {# "get": {# "description": "Get a random pet",# "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"# }# }# }# }# }# }# }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# info: {title: Swagger Petstore, version: 1.0.0}# openapi: 3.0.2# paths:# /random:# get:# description: Get a random pet# responses:# 200:# content:# application/json:# schema: {$ref: '#/components/schemas/Pet'}# tags: []
文档
专业支持
专业支持的APISpec可通过 Tidelift Subscription。
Tidelift为软件开发团队提供了购买和维护软件的单一来源, 有了最了解它的专家的专业级别保证, 同时与现有工具无缝集成。[Get professional support]
安全联系人信息
若要报告安全漏洞,请使用 Tidelift security contact。 Tidelift将协调修复和披露。
许可证
麻省理工学院许可。有关详细信息,请参见捆绑的LICENSE文件。