从有效的openapi规范中提取json模式的实用程序
openapi2jsonschema的Python项目详细描述
openapi2jsonschema
从 有效的OpenAPI规范。
为什么
openapi包含一个使用json超集的definitions
类型的列表
架构。这些是各种openapi兼容工具在内部使用的。我
但是我发现自己想单独使用这些模式
现有OpenAPI工具。为定义的类型生成单独的架构
在openapi中,允许构建各种独立的工具
很容易维护,因为规范定义是共享的。
安装
openapi2jsonschema
是用python实现的。假设你有一个
python intepreter和pip已安装,您应该可以使用以下命令进行安装:
pip install openapi2jsonschema
这还没有经过广泛的测试,目前在我的 机器状态。
用法
最简单的用法是将openapi2jsonschema
工具指向一个url
包含json(或yaml)openapi定义,如下所示:
openapi2jsonschema https://raw.githubusercontent.com/kubernetes/kubernetes/master/api/openapi-spec/swagger.json
这将在schemas
目录中生成一组模式。工具
提供许多选项来修改输出:
$ openapi2jsonschema --help
Usage: openapi2jsonschema [OPTIONS] SCHEMA
Converts a valid OpenAPI specification into a set of JSON Schema files
Options:
-o, --output PATH Directory to store schema files
-p, --prefix TEXT Prefix for JSON references (only for OpenAPI versions
before 3.0)
--stand-alone Whether or not to de-reference JSON schemas
--kubernetes Enable Kubernetes specific processors
--strict Prohibits properties not in the schema
(additionalProperties: false)
--help Show this message and exit.
示例
我的特定用例能够验证kubernetes
没有kubernetes客户机的配置文件,如kubectl
和
没有服务器。为此,我有一个bash脚本,如下所示:
#!/bin/bash -xe# This script uses openapi2jsonschema to generate a set of JSON schemasfor# the specified Kubernetes versions in three different flavours:## X.Y.Z - URL referenced based on the specified GitHub repository# X.Y.Z-standalone - de-referenced schemas, more useful as standalone documents # X.Y.Z-local - relative references, useful to avoid the network dependency REPO="garethr/kubernetes=json-schema"declare -a arr=(1.6.6 1.6.5 1.6.4 1.6.3 1.6.2 1.6.1 1.6.0 1.5.6 1.5.4 1.5.3 1.5.2 1.5.1 1.5.0 )for version in "${arr[@]}"doschema=https://raw.githubusercontent.com/kubernetes/kubernetes/v${version}/api/openapi-spec/swagger.json prefix=https://raw.githubusercontent.com/${REPO}/master/v${version}/_definitions.json openapi2jsonschema -o "${version}-standalone" --stand-alone "${schema}" openapi2jsonschema -o "${version}-local""${schema}" openapi2jsonschema -o "${version}"" --prefix "${prefix}" "${schema}"done
运行此脚本的输出可以在随附的 garethr/kubernetes-json-schema