Swagger:重用对象内部的模式定义 属性

Swagger: Reuse schema definition inside object property

问题:我想重用一个定义。一次直接,一次在对象内部。以下代码有效,但来自 http://editor.swagger.io/#/ 的验证器说 "Not a valid response definition" 并指向“200”行。

我真的必须定义两次帐户吗?或者这是验证器的问题?

 *     responses:
 *       200: <---- ERROR POINTING HERE
 *         description: Updated account
 *         schema:
 *           type: object
 *           properties:
 *             data:
 *               type: object
 *               properties:
 *                 account:
 *                   schema:
 *                     $ref: '#/definitions/Account'

定义本身:

 "Account" : {
    "type" : "object",
    "required": [
        "id", "username", "lastname", "firstname", "traderid", "customerid", "company", "validated"
    ],
    "properties": {
        "id": {
            "type": "string"
        },
        "username" : {
            "type": "string"
        },
        "lastname" : {
            "type": "string"
        },
        "firstname" : {
            "type": "string"
        },
        "traderid" : {
            "type": "string"
        },
        "customerid" : {
            "type": "string"
        },
        "company" : {
            "type": "string"
        },
        "validated" : {
            "type": "boolean"
        }
    },
    "example" : {
        "id": "57790fdde3fd3ed82681f39c",
        "username": "yuhucebafu",
        "validated": false,
        "customerid": "57790fdce3fd3ed82681f39a"
    }
},

问题出在 account 属性 中使用 schema

200:
  description: Updated account
  schema:
   type: object
   properties:
     data:
       type: object
       properties:
         account:
           schema: # <-- Problem is here.
             $ref: '#/definitions/Account'

这是更正后的响应定义:

200:
  description: Updated account
  schema:
   type: object
   properties:
     data:
       type: object
       properties:
         account:
           $ref: '#/definitions/Account'

属性 名称 "schema" 仅在 Swagger 的 response object, or a parameter object 中用作顶级 属性,其中 in 设置为 "body".

一旦您开始指定架构,该结构中的所有内容大部分都遵循标准 JSON Schema,这是高度递归的。例如:

  • 在一个对象架构中,每个 properties/[propertyName] 的值都是一个架构。
  • 在数组模式中,items 的值是一个模式。
  • 每个 /definitions/[name] 的值是一个架构。
  • allOfanyOfoneOf 中每个数组元素的值都是一个架构。
  • additionalProperties 的值可以是模式(或布尔值)。
  • ...

你懂的。 JSON 架构在所有这些情况下都没有使用 属性 名称 schema,因为它会在实际上 "everything is a schema."