Translate Openabi Schemas to JSon Schemas
py-openapi-schema-to-json-schema的Python项目详细描述
概述[构建状态](https://travis-ci.org/pglass/py-openapi-schema-to-json-schema.svg?branch=master)(https://travis ci.org/pglass/py openapi schema to json schema)
[![pypi](https://img.shields.io/pypi/v/py openapi schema to json schema.svg)](https://pypi.org/project/py openapi schema to json schema/)
[![pypi-python版本](https://img.shields.io/pypi/pyversions/py openapi schema to json schema.svg)(https://pypi.org/project/py openapi schema to json schema/)
**这是麻省理工学院授权的直接python端口
[mikunn/openapi schema to json schema](https://github.com/mikunn/openapi schema to json schema)
([v2.1.0](https://github.com/mikunn/openapi schema to json schema/tree/v2.1.0)**。
这个端口同样是麻省理工学院授权的。
openapi 3模式和json模式基本相似。但是,json模式
验证器不知道这两种格式之间的差异。这意味着
在某些常见的
情况下,使用标准json模式验证器
with openapi 3 schemas验证请求/响应json将导致不正确的验证。
解决此问题的一种方法是将openapi 3模式转换为json
模式,这就是此库的目的。
请参见[此处](https://github.com/mikunn/openapi schema to json schema/tree/v2.1.0 why)
了解更多基本原理,以及[phil sturgeon的博客文章](
https://philsurgeon.uk/api/2018/03/30/openapi and json schema division/)
关于这个问题。
如果"nullable"为"true",则将"null"添加到"type"数组中
*支持嵌套的"allof"等深层结构。
*删除[特定于openapi的
属性](https://github.com/oai/openapi specification/blob/master/versions/3.0.0.md\fixed-fields-20)
例如"discriminator"、"deprecated"等,除非另有指定
schema对象
**中带有"x-patternproperties"的rts`patternproperties`注意**:`$ref`没有被取消引用。您将需要另一个库来
read spec和遵循`$ref`字段。
``bash
>
openapi_schema={
"type":"object",
"properties":{
"name":{
"type":"string",
"nullable":true,
}
},
"x-patternproperties":{
"^[a-z]+$":{
"type":"number",
}
}
}
}
options={"supportpatternperties":true}
converted=到json schema(openapi_schema,options)
print(json.dumps(converted,indent=2))
````
这显示了
`nullable:true`到`type:["string","null"]``的转换,以及使用openapi扩展字段
(`x-patternproperties`->;`patternproperties`)
``json
{
"patternproperties":{
"^[a-z]+$":{
"type":"number"
}
}/>"properties":{
"name":{
"type":{
"string"
"null"
>
,
>"$schema":"http://json schema.org/draft-04/schema 35;",
"type":"object"
>
<
"name":"properties":"properties":"object"
"name":"to-json-schema"函数接受"to-json-schema"函数,函数接受"to-json-schema"函数,函数接受"to-json-schema"函数,函数接受"to-json"函数。options`dictionary作为第二个
参数。
`` python
默认值
选项={
'cloneschema':真,
'日期toDateTime:false,
"支持模式属性":false,
"keepNotsupported":[],
"模式属性handler":
"openapi_schema_to_json_schema.patternPropertieshadler,
"removeReadOnly":false,
"removeWriteOnly":true,
"克隆"架构`(bool)
如果设置为"false",则就地转换提供的架构。如果"true",则使用"copy.deepcopy"克隆
架构。默认值为"true"。
\DateToDateTime(bool)
如果设置为"true",则
将"format:"date"设置为"format:"date time`。
例如
``python
import json
{datetodatedatetime:true}
>print(json.dumps(conconconverted,indent=2))
```
``json
{
"format":"date time",
"$schema":"http://json schema.org/draft-04/schema",
"type":"string"
>
````
默认情况下,将从结果架构中删除以下字段:
`nullable`、`discriminator`、`readonly`、`writeonly`、`xml`、`externaldocs`、
`example`和`deprecated`,因为JSON架构草案4不支持这些字段。
提供要保留的字段的列表(作为字符串)和它们不会被删除。
如果属性设置为"required",则它也将从"required"列表中删除。即使将readonly设置为与
`KeepNot支持的
`KeepNot支持的`
35 35 \35 \35 \35 35<<<<<<
<
模式专业perties`属性存在,将
`x-patternproperties`更改为'patternperties',并调用
`patternpertieshandler'。如果未定义"patternpropertieshadler",请调用默认处理程序。有关详细信息,请参见"patternpropertieshandler"。
patternpropertieshandler(函数)
函数采用在根级别定义
`x-patternproperties`的模式。此时
"x-patternproperties"更改为"patternproperties"。它必须返回
已修改的架构。
如果未提供处理程序,则使用默认处理程序。如果
"additionalproperties"已设置并且是一个对象,则如果"additionalproperties"对象与"patternproperties"中的模式
对象具有深度相等关系,则默认处理程序将其设置为
false。这是因为我们可能希望在openapi规范文件中定义
`additionalproperties',但希望根据
模式进行验证。如果
的"additionalproperties"被允许使用相同的结构,那么这个模式将是无用的。创建自己的处理程序以覆盖此
功能。
请参见"tests/to-json schema/test-pattern\u properties.py"以获取此示例。
credits
----[mikunn](https://github.com/mikunn)以获取[原始
openapi架构到json架构](https://github.com/mik)unn/openapi schema to json schema)
-[phil sturgeon](https://github.com/philsturgeon)为他写了一篇很棒的博客文章
[博客文章](https://philsturgeon.uk/api/2018/03/30/openapi和json schema difference/)
关于这个问题(以及他的[反向实现](https://github.com/wework/json-schema-t)o-openapi-schema)
[![pypi](https://img.shields.io/pypi/v/py openapi schema to json schema.svg)](https://pypi.org/project/py openapi schema to json schema/)
[![pypi-python版本](https://img.shields.io/pypi/pyversions/py openapi schema to json schema.svg)(https://pypi.org/project/py openapi schema to json schema/)
**这是麻省理工学院授权的直接python端口
[mikunn/openapi schema to json schema](https://github.com/mikunn/openapi schema to json schema)
([v2.1.0](https://github.com/mikunn/openapi schema to json schema/tree/v2.1.0)**。
这个端口同样是麻省理工学院授权的。
openapi 3模式和json模式基本相似。但是,json模式
验证器不知道这两种格式之间的差异。这意味着
在某些常见的
情况下,使用标准json模式验证器
with openapi 3 schemas验证请求/响应json将导致不正确的验证。
解决此问题的一种方法是将openapi 3模式转换为json
模式,这就是此库的目的。
请参见[此处](https://github.com/mikunn/openapi schema to json schema/tree/v2.1.0 why)
了解更多基本原理,以及[phil sturgeon的博客文章](
https://philsurgeon.uk/api/2018/03/30/openapi and json schema division/)
关于这个问题。
如果"nullable"为"true",则将"null"添加到"type"数组中
*支持嵌套的"allof"等深层结构。
*删除[特定于openapi的
属性](https://github.com/oai/openapi specification/blob/master/versions/3.0.0.md\fixed-fields-20)
例如"discriminator"、"deprecated"等,除非另有指定
schema对象
**中带有"x-patternproperties"的rts`patternproperties`注意**:`$ref`没有被取消引用。您将需要另一个库来
read spec和遵循`$ref`字段。
``bash
>
openapi_schema={
"type":"object",
"properties":{
"name":{
"type":"string",
"nullable":true,
}
},
"x-patternproperties":{
"^[a-z]+$":{
"type":"number",
}
}
}
}
options={"supportpatternperties":true}
converted=到json schema(openapi_schema,options)
print(json.dumps(converted,indent=2))
````
这显示了
`nullable:true`到`type:["string","null"]``的转换,以及使用openapi扩展字段
(`x-patternproperties`->;`patternproperties`)
``json
{
"patternproperties":{
"^[a-z]+$":{
"type":"number"
}
}/>"properties":{
"name":{
"type":{
"string"
"null"
>
,
>"$schema":"http://json schema.org/draft-04/schema 35;",
"type":"object"
>
<
"name":"properties":"properties":"object"
"name":"to-json-schema"函数接受"to-json-schema"函数,函数接受"to-json-schema"函数,函数接受"to-json-schema"函数,函数接受"to-json"函数。options`dictionary作为第二个
参数。
`` python
默认值
选项={
'cloneschema':真,
'日期toDateTime:false,
"支持模式属性":false,
"keepNotsupported":[],
"模式属性handler":
"openapi_schema_to_json_schema.patternPropertieshadler,
"removeReadOnly":false,
"removeWriteOnly":true,
"克隆"架构`(bool)
如果设置为"false",则就地转换提供的架构。如果"true",则使用"copy.deepcopy"克隆
架构。默认值为"true"。
\DateToDateTime(bool)
如果设置为"true",则
将"format:"date"设置为"format:"date time`。
例如
``python
import json
{datetodatedatetime:true}
>print(json.dumps(conconconverted,indent=2))
```
``json
{
"format":"date time",
"$schema":"http://json schema.org/draft-04/schema",
"type":"string"
>
````
默认情况下,将从结果架构中删除以下字段:
`nullable`、`discriminator`、`readonly`、`writeonly`、`xml`、`externaldocs`、
`example`和`deprecated`,因为JSON架构草案4不支持这些字段。
提供要保留的字段的列表(作为字符串)和它们不会被删除。
如果属性设置为"required",则它也将从"required"列表中删除。即使将readonly设置为与
`KeepNot支持的
`KeepNot支持的`
35 35 \35 \35 \35 35<<<<<<
<
模式专业perties`属性存在,将
`x-patternproperties`更改为'patternperties',并调用
`patternpertieshandler'。如果未定义"patternpropertieshadler",请调用默认处理程序。有关详细信息,请参见"patternpropertieshandler"。
patternpropertieshandler(函数)
函数采用在根级别定义
`x-patternproperties`的模式。此时
"x-patternproperties"更改为"patternproperties"。它必须返回
已修改的架构。
如果未提供处理程序,则使用默认处理程序。如果
"additionalproperties"已设置并且是一个对象,则如果"additionalproperties"对象与"patternproperties"中的模式
对象具有深度相等关系,则默认处理程序将其设置为
false。这是因为我们可能希望在openapi规范文件中定义
`additionalproperties',但希望根据
模式进行验证。如果
的"additionalproperties"被允许使用相同的结构,那么这个模式将是无用的。创建自己的处理程序以覆盖此
功能。
请参见"tests/to-json schema/test-pattern\u properties.py"以获取此示例。
credits
----[mikunn](https://github.com/mikunn)以获取[原始
openapi架构到json架构](https://github.com/mik)unn/openapi schema to json schema)
-[phil sturgeon](https://github.com/philsturgeon)为他写了一篇很棒的博客文章
[博客文章](https://philsturgeon.uk/api/2018/03/30/openapi和json schema difference/)
关于这个问题(以及他的[反向实现](https://github.com/wework/json-schema-t)o-openapi-schema)