跳转到主要内容

用于检查Swagger规范向后兼容性的Python库

项目描述

https://github.com/Yelp/swagger-spec-compatibility/workflows/ci/badge.svg https://img.shields.io/codecov/c/gh/Yelp/swagger-spec-compatibility.svg PyPi version Supported Python versions

swagger-spec-compatibility

关于

swagger-spec-compatibility 是一个由Yelp维护的库,提供了自动检测 Swagger/OpenAPI 2.0规范 变更与 向后兼容性 相关的安全性的工具。

swagger-spec-compatibility 的目标是让开发者对其规范变更的安全性有信心,并且使用以前版本的Swagger规范构建的客户端可以继续正确通信。

免责声明

该库并不打算涵盖所有可能的向后不兼容情况。
这是因为OpenAPI 2.0规范非常具有表达性和灵活性,导致许多向后不兼容情况。

目前支持的检测规则是根据覆盖 常见 破坏性变更(我们在Yelp内部遇到的)或从贡献者那里收到的支持而构建的。

如果您遇到了破坏性变更,并且希望在事情变得严重之前让工具帮助您找出问题,请随时在项目中创建问题。您也可以创建实现规则的拉取请求,我们始终欢迎贡献者。

文档

更多文档可在swagger-spec-compatibility.readthedocs.org找到。

如何安装

# to install the latest released version
$ pip install swagger-spec-compatibility

# to install the latest master [from Github]
$ pip install git+https://github.com/Yelp/swagger-spec-compatibility

示例用法

以下命令假定库已经安装

$ swagger_spec_compatibility -h
usage: swagger_spec_compatibility [-h] {explain,info,run} ...

Tool for the identification of backward incompatible changes between two swagger specs.

The tool provides the following level of results:
- WARNING: the Swagger specs are technically compatible but the are likely to break known Swagger implementations
- ERROR: new Swagger spec does introduce a breaking change respect the old implementation

positional arguments:
  {explain,info,run}  help for sub-command
    explain           explain selected rules
    info              Reports tool's information
    run               run backward compatibility detection

optional arguments:
  -h, --help          show this help message and exit
$ swagger_spec_compatibility explain -r REQ-E001 REQ-E002
Rules explanation
[REQ-E001] Added Required Property in Request contract:
    Adding a required property to an object used in requests leads client request to fail if the property is not present.
[REQ-E002] Removed Enum value from Request contract:
    Removing an enum value from a request parameter is backward incompatible as a previously valid request will not be
    valid. This happens because a request containing the removed enum value, valid according to the "old" Swagger spec, is
    not valid according to the new specs.
$ old_spec_path=docs/source/rules/examples/REQ-E001/old.yaml
$ new_spec_path=docs/source/rules/examples/REQ-E001/new.yaml

# Run with all the registered rules
$ swagger_spec_compatibility run ${old_spec_path} ${new_spec_path}
ERROR rules:
    [REQ-E001] Added Required Property in Request contract : #/paths//endpoint/post/parameters/0/schema
$ echo $?
1

# Run with a subset of registered rules
$ swagger_spec_compatibility -r=MIS-E001 -r=MIS-E002 run ${old_spec_path} ${new_spec_path}
$ echo $?
0
$ swagger_spec_compatibility info
swagger-spec-compatibility: 1.3.0
Python version: CPython - 3.6.9
Python compiler: GCC 4.2.1 Compatible Apple LLVM 10.0.1 (clang-1001.0.46.4)
Discovered rules:
    MIS-E001: swagger_spec_compatibility.rules.deleted_endpoint.DeletedEndpoint
    MIS-E002: swagger_spec_compatibility.rules.changed_type.ChangedType
    REQ-E001: swagger_spec_compatibility.rules.added_required_property_in_request.AddedRequiredPropertyInRequest
    REQ-E002: swagger_spec_compatibility.rules.removed_enum_value_from_request.RemovedEnumValueFromRequest
    REQ-E003: swagger_spec_compatibility.rules.removed_properties_from_request_objects_with_additional_properties_set_to_false.RemovedPropertiesFromRequestObjectsWithAdditionalPropertiesSetToFalse
    RES-E001: swagger_spec_compatibility.rules.added_properties_in_response_objects_with_additional_properties_set_to_false.AddedPropertiesInResponseObjectsWithAdditionalPropertiesSetToFalse
    RES-E002: swagger_spec_compatibility.rules.removed_required_property_from_response.RemovedRequiredPropertyFromResponse
    RES-E003: swagger_spec_compatibility.rules.added_enum_value_in_response.AddedEnumValueInRequest

开发

代码使用Sphinx进行文档化。

virtualenv建议用于保持依赖项和库的隔离。

设置

# Initialize your dev environment
$ make minimal

# Ensure that you have activated the virtualenvironment
$ source ./venv/bin/activate

提示:如果您已安装aactivator,虚拟环境将自动激活

如何定义新的兼容性规则

按照以下步骤定义新规则

  1. 运行python -m create_new_rule以生成规则骨架。

    工具将创建检测规则类、测试等。请查看工具输出以获取创建的文件的确切列表。

  2. 实现规则逻辑(swagger_spec_compatibility/rules/{filename}.py)并确保测试覆盖率(tests/rules/{filename}_test.py)。

  3. 更新docs/source/rules/examples/{rule_code}/(new|old).yaml示例Swagger规范更改,并更新docs/source/rules/examples/{rule_code}/tester.py测试文件。

    这些文件的目标是提供一个简单的规范更改,通过使用bravado客户端触发向后不兼容的检测规则(查看其他测试示例)。

    注意:测试器由自动测试执行,因此tester.py应无错误完成,并且规范更改应触发新创建的规则。

  4. swagger_spec_compatibility/rules/{filename}.pydocs/source/rules/{error_code}.rst中添加定义规则的文档。

    尽量与其他文档页面保持风格一致。

  5. [可选]添加集成测试以确保不会引入回归,并/或验证新规则的边缘情况。

    集成测试定义如下:case-<增量 编号>-<预期报告数>-reports-<简短描述>目录,包含两个文件:old.yamlnew.yaml。这两个文件代表需要检查向后兼容性的Swagger规范的两个版本。

贡献

  1. 将其分支(http://github.com/Yelp/swagger-spec-compatibility/fork

  2. 创建您的功能分支(git checkout -b my-new-feature

  3. 添加您的修改

  4. 提交您的更改(git commit -m "添加一些功能"

  5. 将分支推送到远程(git push origin my-new-feature

  6. 创建新的拉取请求

许可

版权所有 2019 Yelp, Inc。

Swagger Spec Compatibility采用Apache License 2.0许可。

项目详情


下载文件

下载适用于您平台的文件。如果您不确定选择哪个,请了解更多关于安装包的信息。

源代码发行版

swagger-spec-compatibility-1.3.4.tar.gz (24.7 kB 查看哈希值)

上传时间 源代码

构建发行版

swagger_spec_compatibility-1.3.4-py2.py3-none-any.whl (43.1 kB 查看哈希值)

上传时间 Python 2 Python 3

由以下支持