2017-03-14 190 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