2017-03-14 2 views
0

Ich versuche, diese Swagger-API-Vererbung stuff mit allOf herauszufinden. Das ist meine Swagger-Yaml-Datei.Swagger: Zusätzliche Eigenschaften nicht erlaubt: 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" 

Als ich fügen Sie diesen in die online editor, erhalte ich die folgenden Fehler, dass ich eine echte harte Zeit habe versucht, herauszufinden.

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

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

Das Hauptproblem scheint Additional properties not allowed: allOf zu sein und ich bin nicht scheinen, um herauszufinden, was ich falsch mache in diesem Fall. Ich habe versucht, eine grundlegende Nicht-Erfolgsantwort zu deklarieren, so dass alle Nicht-200-Antworten erben werden, so dass die API eine sehr standardmäßige Nicht-Erfolgsantwort hat. Ich hatte den Eindruck, ich könnte dies mit allOf tun und dann die Felder aus dieser Antwort hinzufügen oder überschreiben. Was genau mache ich falsch?

Antwort

3

Das Tag allOf kann nur für Schema-Objekte verwendet werden. Sie können es jedoch definitiv für den Schema-Teil der Antwort verwenden. Hier ist ein Beispiel dafür.

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

Yip, das ist ziemlich viel, wo ich landete. – SynackSA

Verwandte Themen