在对象属性内重用架构定义

Mor*_*ool 2 swagger swagger-2.0

问题:我想重用定义。一次直接进入,一次进入一个物体。以下代码有效,但是来自http://editor.swagger.io/#/的验证器说“无效的响应定义”并指向行“ 200”。

我真的必须两次定义“帐户”吗?还是验证者有问题?

 *     responses:
 *       200: <---- ERROR POINTING HERE
 *         description: Updated account
 *         schema:
 *           type: object
 *           properties:
 *             data:
 *               type: object
 *               properties:
 *                 account:
 *                   schema:
 *                     $ref: '#/definitions/Account'
Run Code Online (Sandbox Code Playgroud)

定义本身:

 "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"
    }
},
Run Code Online (Sandbox Code Playgroud)

Ted*_*ein 5

问题在于schemaaccount属性中的使用。

200:
  description: Updated account
  schema:
   type: object
   properties:
     data:
       type: object
       properties:
         account:
           schema: # <-- Problem is here.
             $ref: '#/definitions/Account'
Run Code Online (Sandbox Code Playgroud)

这是正确的响应定义:

200:
  description: Updated account
  schema:
   type: object
   properties:
     data:
       type: object
       properties:
         account:
           $ref: '#/definitions/Account'
Run Code Online (Sandbox Code Playgroud)

属性名“模式”仅作为在扬鞭的顶层特性响应对象,或一个参数对象,其中in被设置为“主体”。

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

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

你明白了。JSON Schema schema在所有这些情况下都不使用属性名,因为它会在实际上“一切都是模式”的语言中引入很多噪音。