gpt4 book ai didi

json - 如何创建swagger数组

转载 作者:行者123 更新时间:2023-12-03 21:21:35 27 4
gpt4 key购买 nike

我正在尝试为以下 JSON 创建一个 swagger 文档,但出现以下错误:具有“类型:数组”的模式,需要同级的“项目:”字段

JSON:

{
"_id": "string",
"name": "string",
"descriptions": {},
"date": "string",
"customer": {
"id": "string",
"name": {
"firstName": "string",
"lastName": "string",
"middleName": "string"
}
},
"productDetials": {
"id": "string",
"name": {
"name": "string",
"model": "string",
"price": "string",
"comments": "string"
}
},
"Phone": [{
"id": "string",
"category": "string",
"version": "string",
"condition": "string",
"availability": "string"

}
]
}

有人可以帮我获取此 JSON 的 swagger 文档吗?

任何帮助将非常感激。

最佳答案

首先,您必须定义依赖于 JSON(对象)的模型。

在你的情况下:

  • Order (我猜)
  • Customer
  • CustomerName
  • ProductDetails
  • ProductName
  • Phone

  • 然后在 definitions 中定义模型YAML(Swagger 架构文档)的部分:
    Order:
    type: "object"
    properties:
    _id:
    type: "string"
    name:
    type: "string"
    descriptions:
    type: "object"
    date:
    type: "string"
    customer:
    $ref: "#/definitions/Customer"
    productDetails:
    $ref: "#/definitions/ProductDetails"
    phoneNumbers:
    type: "array"
    items:
    $ref: "#/definitions/Phone"
    Customer:
    type: "object"
    properties:
    id:
    type: "string"
    name:
    $ref: "#/definitions/CustomerName"
    CustomerName:
    type: "object"
    properties:
    firstName:
    type: "string"
    lastName:
    type: "string"
    middleName:
    type: "string"
    ProductDetails:
    type: "object"
    properties:
    id:
    type: "string"
    name:
    $ref: "#/definitions/ProductName"
    ProductName:
    type: "object"
    properties:
    name:
    type: "string"
    model:
    type: "string"
    price:
    type: "string"
    comments:
    type: "string"
    Phone:
    type: "object"
    properties:
    id:
    type: "string"
    category:
    type: "string"
    version:
    type: "string"
    condition:
    type: "string"
    availability:
    type: "string"

    如果你想定义一个 阵列 以特定型号作为项目 - 取 array作为 type并定义 items (根据提供的错误代码,您忘记了它)。 items是数组的内容 - 所以 Phone您的案例中的模型:
    ...
    phoneNumbers:
    type: "array"
    items:
    $ref: "#/definitions/Phone"

    关于json - 如何创建swagger数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53398870/

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