可插入的api规范生成器。目前支持openapi规范(f.k.a.swagger规范)。

apispec的Python项目详细描述


PyPI versionBuild statusDocumentationmarshmallow 2/3 compatibleOpenAPI Specification 2/3 compatiblecode style: black

可插入的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: []

文档

文档可在https://apispec.readthedocs.io/找到。

生态系统

在github wiki上可以找到与apispec相关的库的列表:

https://github.com/marshmallow-code/apispec/wiki/Ecosystem

支持APISpec

APISpec由一组 volunteers。 如果你想支持这个项目的未来,请考虑 为我们的开放式集体做出贡献:

Donate to our collective

专业支持

专业支持的APISpec可通过 Tidelift Subscription

Tidelift为软件开发团队提供了购买和维护软件的单一来源, 有了最了解它的专家的专业级别保证, 同时与现有工具无缝集成。[Get professional support]

Get supported apispec with Tidelift

安全联系人信息

若要报告安全漏洞,请使用 Tidelift security contact。 Tidelift将协调修复和披露。

许可证

麻省理工学院许可。有关详细信息,请参见捆绑的LICENSE文件。

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
java静态对象初始化   java请求的资源上不存在“Accesscontrolalloworigin”头。起源http://localhost:4200因此不允许访问   java i无法通过executor的submit方法处理列表的所有记录   javadoc中的java文档逻辑   java如何使RCPPlugin独立   java使用cobertura插件跳过*测试*   验证Java处理多个可能未知的日期/时间格式   java将JPanel添加到JFrame   BB 7.0中的黑莓Java标签   java动态地向注释处理环境添加类型   java如何将头信息作为键值对传递给使用jersey的rest服务   java使用ImageJ调整图像大小并裁剪图像   为什么Java8的ToIntFunction<t>不扩展函数<t,Integer>   java无法向Jackson ObjectMapper类注册模块   解析如何将此jsonarray解析为字符串或数组java   如何从Java调用具有数值参数的Scala方法   类如何从ColdFusion实现Java接口?   java如何让线程只添加到一个带有同步