2017-03-14 148 views
0

我想通过使用allOf找出这个Swagger API继承的东西。这是我的噱头yaml文件。Swagger:不允许的其他属性:allOf

swagger: '2.0' 
info: 
    title: Test API 
    version: '1' 
basePath: /api/v1 
schemes: 
    - https 
produces: 
    - application/json 

paths: 
    /users: 
    get: 
     summary: Collection of users 
     tags: 
     - users 
     responses: 
     200: 
      description: A list of Users 
      schema: 
      $ref: "#/definitions/Users"   
     500: 
      $ref: "#/responses/BadRequest" 

definitions: 
    User: 
    required: 
     - username 
    properties: 
     firstName: 
     type: string 
     lastName: 
     type: string 
     username: 
     type: string 
    Users: 
    type: array 
    items: 
     $ref: "#/definitions/User" 

responses: 
    NonSuccess: 
    description: Generic response for all non-success responses 
    schema: 
     type: object 
     required: 
     - code 
     - message 
     properties: 
     code: 
      type: integer 
      description: The success code, 0 or -1. 
     message: 
      type: string 
      description: The description message for this success code 
     errors: 
      type: array 
      description: A map of errors within the request. Keyed by the parameter name and the values are the error details 

    BadRequest: 
    description: Invalid request parameters 
    allOf: 
     - $ref: "#/responses/NonSuccess" 

当我贴到online editor,我得到的是我有一个真正的困难时期试图找出以下错误。

✖ Swagger Error 
Additional properties not allowed: allOf 
Jump to line 60 

✖ Swagger Error 
Not a valid response definition 
Jump to line 22 

的主要问题似乎是Additional properties not allowed: allOf,我似乎无法找出什么我做错了在这种情况下。我试图声明一个基本的非成功响应,以便所有非200响应都会继承,这样API将会有非常标准的非成功响应。我的印象是我可以用allOf做到这一点,然后添加或覆盖该响应中的字段。我究竟做错了什么?

回答

3

allOf标记只能用于Schema对象。不过,您可以在响应的模式部分中明确使用它。这是一个例子。

swagger: '2.0' 
info: 
    title: Test API 
    version: '1' 
basePath: /api/v1 
schemes: 
    - https 
produces: 
    - application/json 

paths: 
    /users: 
    get: 
     summary: Collection of users 
     tags: 
     - users 
     responses: 
     200: 
      description: A list of Users 
      schema: 
      $ref: "#/definitions/Users"   
     500: 
      $ref: "#/responses/BadRequest" 

definitions: 
    User: 
    required: 
     - username 
    properties: 
     firstName: 
     type: string 
     lastName: 
     type: string 
     username: 
     type: string 
    Users: 
    type: array 
    items: 
     $ref: "#/definitions/User" 

    Response: 
    type: object 
    required: 
     - code 
     - message 
    properties: 
     code: 
     type: integer 
     description: The success code, 0 or -1. 
     message: 
     type: string 
     description: The description message for this success code 
     errors: 
     type: array 
     description: A map of errors within the request. Keyed by the parameter name and the values are the error details 

    BadRequest: 
    type: object 
    required: 
     - validationErrors 
    properties: 
     validationErrors: 
     type: array 
     items: 
      type: string 

responses: 
    NonSuccess: 
    description: Generic response for a non-success 
    schema: 
     $ref: "#/definitions/Response" 

    BadRequest: 
    description: Invalid request parameters 
    schema: 
     allOf: 
     - $ref: "#/definitions/Response" 
     - $ref: "#/definitions/BadRequest" 
+0

叶,这是我最终结束的地方。 – SynackSA