用于检查swagger规范向后兼容性的python库
swagger-spec-compatibilit的Python项目详细描述
夸张的规格兼容性
关于
swagger-spec-compatibility是一个yelp维护的库,它提供了自动检测的工具 Swagger/OpenAPI 2.0 specification变化的安全性 关于向后兼容性。
swagger-spec-compatibility旨在让开发人员确信他们的规范更改是安全的,并且客户机 使用以前版本的swagger规范构建的系统可以继续正常通信。
文件
如何安装
# to install the latest released version $ pip install swagger-spec-compatiblity # 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][-r {MIS-E001,MIS-E002,REQ-E001,REQ-E002,REQ-E003,RES-E001,RES-E002,RES-E003}[{MIS-E001,MIS-E002,REQ-E001,REQ-E002,REQ-E003,RES-E001,RES-E002,RES-E003} ...]]{explain,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,run}helpfor sub-command explain explain selected rules run run backward compatibility detection optional arguments: -h, --help show this help message and exit -r {MIS-E001,MIS-E002,REQ-E001,REQ-E002,REQ-E003,RES-E001,RES-E002,RES-E003}[{MIS-E001,MIS-E002,REQ-E001,REQ-E002,REQ-E003,RES-E001,RES-E002,RES-E003} ...], --rules {MIS-E001,MIS-E002,REQ-E001,REQ-E002,REQ-E003,RES-E001,RES-E002,RES-E003}[{MIS-E001,MIS-E002,REQ-E001,REQ-E002,REQ-E003,RES-E001,RES-E002,RES-E003} ...] Rules to apply for compatibility detection. (default: ['MIS-E001', 'MIS-E002', 'REQ-E001', 'REQ-E002', 'REQ-E003', 'RES-E001', 'RES-E002', 'RES-E003'])
$ swagger_spec_compatibility explain -r=REQ-E001 -r=REQ-E002 explain 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
发展
使用Sphinx记录代码。
virtualenv是 建议保持依赖项和库的隔离。
设置
# Initialize your dev environment $ make minimal # Ensure that you have activated the virtualenvironment $ source ./venv/bin/activate
提示:如果安装了aactivator,虚拟环境将 自动激活
如何定义新的兼容性规则
使用以下步骤定义新规则:
- 在swagger_spec_compatibility/rules/ 中定义新的规则类
# Example of the file content (assume that the file will be named FILE.py)classRuleClassName(BaseRule):description=''error_code='ERROR_CODE'error_level=Level.LEVELrule_type=RuleType.TYPEshort_name=''@classmethoddefvalidate(cls,left_spec,right_spec):# type: (Spec, Spec) -> typing.Iterable[ValidationMessage]# Implement here your logicraiseNotImplemented()# Please make sure that:# * `description` and `short_name` are reasonably explicative to support `swagger_spec_compatibility explain` command# * `error_code` has REQ- prefix for `RuleType.REQUEST_CONTRACT`, RES- for `RuleType.RESPONSE_CONTRACT` and MIS- for `RuleType.MISCELLANEOUS`
- 添加测试以确保规则按预期运行(在tests/rules/FILE_test.py中的测试)
- 在docs/source/rules/ERROR_CODE.rst中为定义的规则添加文档。尽量保持风格一致 其他文档页的
- 添加在docs/source/rules/examples/ERROR_CODE.rst中触发规则的swagger规范更改的示例。 确保定义一个tester.py文件,该文件将通过使用 bravado客户机(查看其他测试人员的示例)。 注意:测试人员由自动测试执行,这是为了确保文档与 代码库。
- [可选]添加集成测试以确保不会引入回归和/或验证新规则的边缘情况。 集成测试定义如下:case-<incrementalnumber>-<number of expected reports>-reports-<short description>目录 有两个文件:old.yaml和new.yaml。这两个文件表示需要检查的两个版本的swagger规范 向后兼容。
贡献
- 叉它(http://github.com/Yelp/swagger-spec-compatibility/fork)
- 创建功能分支(git checkout -bmy-new-feature)
- 添加您的修改
- 提交更改(git commit -m "Add some feature")
- 推到分支(git push origin my-new-feature)
- 创建新的拉取请求