去年我在采用 OpenAPI 时遇到了这个问题,得出的结论是工作量太大。我无法使用 OpenAPI 从 JSON 验证器获得完整验证,因为 OpenAPI 声明架构定义的方式与完整的 JSON 架构定义之间存在一些差异。
查看 JSON 验证组件的文档,您会发现:
JSON Schema Validator 组件使用 NetworkNT JSON Schema 库 (https://github.com/networknt/json-schema-validator) 对 JSON Schemas v4 草稿执行消息正文的 bean 验证。这是一个完全独立的 JSON Schema,如果您阅读 github 页面,您会发现它。
OpenAPI 支持
OpenAPI 3.0 规范使用 JSON 模式来验证请求/响应,但存在一些差异。使用配置文件,您可以启用该库以使用 OpenAPI 3.0 验证。
OpenAPI 架构似乎是真正 JSON 架构的子集。
在我给你看一个更详细的例子之前。查看此处骆驼文档中给出的示例:https://camel.apache.org/components/latest/json-validator-component.html。将该 json 模式文件与 openAPI 模式定义进行比较,您会发现它们并不相同。
这里的一个有用工具是https://jsonschema.net,您可以在此处粘贴您的 json 示例并推断架构。我在下面的示例中使用了这个工具和 OpenAPI Pet Store 示例,
OpenAPI Petstore 宠物对象示例:
{
"id": 0,
"category": {
"id": 0,
"name": "string"
},
"name": "doggie",
"photoUrls": [
"string"
],
"tags": [
{
"id": 0,
"name": "string"
}
],
"status": "available"
}
保存在 JSON 中的 openAPI 规范产生了这个定义:
"Pet": {
"type": "object",
"required": [
"name",
"photoUrls"
],
"properties": {
"id": {
"type": "integer",
"format": "int64"
},
"category": {
"$ref": "#/definitions/Category"
},
"name": {
"type": "string",
"example": "doggie"
},
"photoUrls": {
"type": "array",
"xml": {
"name": "photoUrl",
"wrapped": true
},
"items": {
"type": "string"
}
},
"tags": {
"type": "array",
"xml": {
"name": "tag",
"wrapped": true
},
"items": {
"$ref": "#/definitions/Tag"
}
},
"status": {
"type": "string",
"description": "pet status in the store",
"enum": [
"available",
"pending",
"sold"
]
}
},
"xml": {
"name": "Pet"
}
}
当我将其转换为正确的 JSON 架构语法时,JSON 架构如下所示:
{
"definitions": {},
"$schema": "http://json-schema.org/draft-07/schema#",
"$id": "http://example.com/root.json",
"type": "object",
"title": "The Root Schema",
"required": [
"id",
"category",
"name",
"photoUrls",
"tags",
"status"
],
"properties": {
"id": {
"$id": "#/properties/id",
"type": "integer",
"title": "The Id Schema",
"default": 0,
"examples": [
0
]
},
"category": {
"$id": "#/properties/category",
"type": "object",
"title": "The Category Schema",
"required": [
"id",
"name"
],
"properties": {
"id": {
"$id": "#/properties/category/properties/id",
"type": "integer",
"title": "The Id Schema",
"default": 0,
"examples": [
0
]
},
"name": {
"$id": "#/properties/category/properties/name",
"type": "string",
"title": "The Name Schema",
"default": "",
"examples": [
"string"
],
"pattern": "^(.*)$"
}
}
},
"name": {
"$id": "#/properties/name",
"type": "string",
"title": "The Name Schema",
"default": "",
"examples": [
"doggie"
],
"pattern": "^(.*)$"
},
"photoUrls": {
"$id": "#/properties/photoUrls",
"type": "array",
"title": "The Photourls Schema",
"items": {
"$id": "#/properties/photoUrls/items",
"type": "string",
"title": "The Items Schema",
"default": "",
"examples": [
"string"
],
"pattern": "^(.*)$"
}
},
"tags": {
"$id": "#/properties/tags",
"type": "array",
"title": "The Tags Schema",
"items": {
"$id": "#/properties/tags/items",
"type": "object",
"title": "The Items Schema",
"required": [
"id",
"name"
],
"properties": {
"id": {
"$id": "#/properties/tags/items/properties/id",
"type": "integer",
"title": "The Id Schema",
"default": 0,
"examples": [
0
]
},
"name": {
"$id": "#/properties/tags/items/properties/name",
"type": "string",
"title": "The Name Schema",
"default": "",
"examples": [
"string"
],
"pattern": "^(.*)$"
}
}
}
},
"status": {
"$id": "#/properties/status",
"type": "string",
"title": "The Status Schema",
"default": "",
"examples": [
"available"
],
"pattern": "^(.*)$"
}
}
}
OpenAPI 的 Schema 定义和 JSON Schema 定义有一些不同。