虽然我很精通JSON Schema规范,但我正在努力过渡到使用Open API 3.0.2规范,我在尝试干燥和组织我的定义时遇到了一个奇怪的问题。我试图将$ref
用于嵌套文件夹中的其他json结构,但遇到了一个错误。我确信我缺少了一个规则或路径字符。我应该在信息块下添加$ref
,看起来工作正常,它只是型号$ref
。
谢谢你的帮助!
我一直在遵循这些指南
- https://davidgarcia.dev/posts/how-to-split-open-api-spec-into-multiple-files/
- https://spec.openapis.org/oas/v3.0.2
- https://apihandyman.io/writing-openapi-swagger-specification-tutorial-part-8-splitting-specification-file/
我正在使用committee gem测试这些模式,错误为x1c 0d1x
已尝试将"$ref": "models/user.json"
DRY解压缩到子文件夹
{
"type": "object",
"description": "A user account.",
"required": [
"id",
"email"
],
"additionalProperties": false,
"properties": {
"id": {
"type": "integer",
"description": "Unique identifier for the given record."
},
"email": {
"type": "string",
"description": "The email associated to the given user."
},
"first_name": {
"type": "string",
"description": "User's first name."
},
"last_name": {
"type": "string",
"description": "User's last name."
},
"full_name": {
"type": "string",
"description": "Full user name"
}
}
}
如何设置文件夹结构
common/
models/
requests/
api.json
api.json
文件
{
"openapi": "3.0.2",
"components": {
"schemas": {
"error": {
"properties": {
"code": {
"type": "string",
"description": "Machine readable code for this specific error message."
},
"message": {
"type": "string",
"description": "Description of the error."
}
},
"required": [
"code",
"message"
],
"type": "object"
},
"user": {
"type": "object",
"description": "A user account.",
"required": [
"id",
"email"
],
"additionalProperties": false,
"properties": {
"id": {
"type": "integer",
"description": "Unique identifier for the given record."
},
"email": {
"type": "string",
"description": "The email associated to the given user."
},
"first_name": {
"type": "string",
"description": "User's first name."
},
"last_name": {
"type": "string",
"description": "User's last name."
},
"full_name": {
"type": "string",
"description": "Full user name"
}
}
},
"user_input": {
"properties": {
"email": {
"type": "string"
},
"first_name": {
"type": "string",
"description": "User's first name."
},
"last_name": {
"type": "string",
"description": "User's last name."
}
},
"required": [
"email"
],
"type": "object"
}
}
},
"info": { "$ref": "common/info.json" },
"paths": {
"/users": {
"get": {
"description": "Returns all the users in the system.",
"operationId": "get--users",
"parameters": [],
"responses": {
"200": {
"description": "[com.test.test]",
"content": {
"application/json": {
"schema": {
"items": {
"$ref": "models/user.json"
},
"type": "array"
}
}
},
"headers": {}
}
},
"tags": [
"user"
]
},
"post": {
"description": "Creates a new user.",
"operationId": "post--users",
"parameters": [],
"requestBody": {
"content": {
"application/json": {
"schema": {
"$ref": "#/components/schemas/user_input"
}
}
}
},
"responses": {
"200": {
"description": "com.test.test",
"content": {
"application/json": {
"schema": {
"$ref": "models/user.json"
}
}
},
"headers": {}
},
"409": {
"description": "[com.test.test]",
"content": {
"application/json": {
"schema": {
"items": {
"$ref": "#/components/schemas/error"
},
"type": "array"
}
}
},
"headers": {}
}
},
"tags": [
"user"
]
}
},
"/users/{id}": {
"get": {
"description": "Returns the given user.",
"operationId": "get--users-id",
"parameters": [
{
"deprecated": false,
"in": "path",
"required": true,
"schema": {
"type": "integer"
},
"name": "id"
}
],
"responses": {
"200": {
"description": "com.test.test",
"content": {
"application/json": {
"schema": {
"$ref": "models/user.json"
}
}
},
"headers": {}
},
"404": {
"description": "unit",
"content": {
"application/json": {
"schema": {
"type": "integer",
"nullable": true
}
}
},
"headers": {}
}
},
"tags": [
"user"
]
}
}
},
"servers": [
{
"url": "http://bobs.ngrok.io"
}
]
}
1条答案
按热度按时间7z5jn7bk1#
不能使用
$ref
来指向info
对象,它必须是内联的。