openapi - 如何在 OpenAPI 3.0 的模式中使用 $ref?
问题描述
我想schema
在 OpenAPI 3.0 API 定义中将以下 JSON 表示为:
{
get-question: {
question-id:string
}
}
到目前为止,我已经写过:
components:
schemas:
#schema of a question-id
QuestionID: #{question-id: string}
properties:
question-id:
type: string
required:
- question-id
#schema of a get-question request which contains a question id
GetQuestion: #{get-question: {question-id:string}}
properties:
get-questions:
type: $ref:'#/components/schemas/QuestionID'
required:
- get-questions
但我在 Swagger 编辑器中收到这些错误:
Schema error at components.schemas['GetQuestion']
should have required property '$ref'
missingProperty: $ref
Jump to line 79
Schema error at components.schemas['GetQuestion']
should match exactly one schema in oneOf
Jump to line 79
Schema error at components.schemas['GetQuestion'].properties['get-questions']
should have required property '$ref'
missingProperty: $ref
Jump to line 81
Schema error at components.schemas['GetQuestion'].properties['get-questions']
should match exactly one schema in oneOf
Jump to line 81
Schema error at components.schemas['GetQuestion'].properties['get-questions'].type
should be equal to one of the allowed values
allowedValues: array, boolean, integer, number, object, string
Jump to line 82
什么是正确的语法$ref
?
解决方案
$ref
用于代替,而type
不是作为 的值type
。还要注意:
YAML 中用于分隔键和值的空格。
get-questions:
$ref: '#/components/schemas/QuestionID'
您还需要添加type: object
到您的QuestionID
和GetQuestion
模式以表明它们是对象;properties
仅关键字是不够的。
其中一个属性名称似乎也有错字 - 它在模式中是get-questions
(复数),但在您的 JSON 示例中是(单数)。我想应该是。GetQuestion
get-question
get-question
完整示例:
components:
schemas:
# schema of a question-id
QuestionID: # {question-id: string}
type: object # <-----
properties:
question-id:
type: string
required:
- question-id
#schema of a get-question request which contains a question id
GetQuestion: # {get-question: {question-id:string}}
type: object # <-----
properties:
get-question:
$ref: '#/components/schemas/QuestionID' # <-----
required:
- get-questions
推荐阅读
- tcl - 如何强制执行完全字节码编译?
- r - data.table 按组汇总多列
- java - 为什么我在下面的代码中遇到以下错误:线程“main”java.lang.OutOfMemoryError中的异常:Java堆空间
- ruby - 如何使用 FbGraph2 gem 获取与用户关联的帐户/页面?
- apache - 将 QUERY_STRING 传递给类似规则的 RedirectMatch
- matlab - 如何在matlab中绘制几个向量qith quiver3
- sql - 无法将月份数字转换为名称
- android - 在目录之间移动项目会产生错误
- css - 部署到 netlify 后情感 css 不渲染但在本地工作
- javascript - 如何获取组件的方法实名?