gpt4 book ai didi

openapi - 如何在 OpenAPI 3.0 的架构中使用 $ref?

转载 作者:行者123 更新时间:2023-12-01 13:17:23 25 4
gpt4 key购买 nike

我想将以下 JSON 表示为 schema在 OpenAPI 3.0 API 定义中:

{
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给您的 QuestionIDGetQuestion模式来表明它们是对象; properties仅靠关键字是不够的。

其中一个属性名称似乎也有拼写错误 - 它是 get-questions (复数)在 GetQuestion架构但 get-question (单数)在您的 JSON 示例中。我猜应该是 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

关于openapi - 如何在 OpenAPI 3.0 的架构中使用 $ref?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53475979/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com