2016-03-30 11 views
9

Sto tentando di definire una definizione di schema swagger per un oggetto che contiene una matrice di oggetti di tipi diversi.Come si crea uno schema di swagger che include una serie di tipi diversi

Ecco lo schema JSON per un oggetto modello (e tutti i tipi di oggetti correlati). Sono consapevole che swagger non supporta il predicato oneOf, quindi sto solo cercando di capire come descrivere questa struttura di dati in swagger. Ho provato molte variazioni su questa sintassi, ma nessuno lavorato e questo era il più vicino ho potuto venire in base alla specifiche e alcuni esempi trovato qui: http://json-schema.org/example2.html

swagger: '2.0' 
info: 
    version: 1.0.0 
    title: IDMU 
paths: 

definitions: 
    template: 
    type: object 
    properties: 
     collection: 
     type: string 
     name: 
     type: string 
     columnValue: 
     type: string 
     description: 
     type: string 
     outputFile: 
     type: string 
     content: 
     type: string 
     directives: 
     type: array 
     items: 
      type: object 
      oneOf: 
      - 
       $ref: '#/definitions/directiveRequire' 
      - 
       $ref: '#/definitions/directiveReplace' 
      - 
       $ref: '#/definitions/directiveReplaceRowSql' 
      - 
       $ref: '#/definitions/directiveReplaceRowCsv' 
      - 
       $ref: '#/definitions/directiveReplaceColSql' 
      - 
       $ref: '#/definitions/directiveReplaceColCsv' 
      - 
       $ref: '#/definitions/directiveInsertTag' 
      - 
       $ref: '#/definitions/directiveInsertCsv' 
      - 
       $ref: '#/definitions/directiveInsertSql' 
    providerCsv: 
    type: object 
    properties: 
     type: 
     type: integer 
     maximum: 3 
     minimum: 3 
     tag: 
     type: string 
     url: 
     type: string 
     staticData: 
     type: string 
    providerTag: 
    type: object 
    properties: 
     type: 
     type: integer 
     maximum: 2 
     minimum: 2 
     tag: 
     type: string 
     condition: 
     type: integer 
     list: 
     type: boolean 
     value: 
     type: string 
    providerSql: 
    type: object 
    properties: 
     type: 
     type: integer 
     maximum: 1 
     minimum: 1 
     source: 
     type: string 
     columns: 
     type: string 
     from: 
     type: string 
     where: 
     type: string 
    directive: 
    type: object 
    discriminator: type 
    properties: 
     type: 
     type: integer 
     softFail: 
     type: boolean 
    required: 
     - type 
    directiveRequire: 
    type: object 
    allOf: 
     - $ref: '#/definitions/directive' 
     - properties: 
      tags: 
      type: array 
      items: 
       type: string 
    directiveReplace: 
    type: object 
    allOf: 
     - $ref: '#/definitions/directive' 
     - properties: 
      description: 
      type: string 
      from: 
      type: string 
      to: 
      type: string 
    directiveReplaceRowSql: 
    type: object 
    allOf: 
     - $ref: '#/definitions/directive' 
     - properties: 
      description: 
      type: string 
      provider: 
      $ref: '#/definitions/providerSql' 
    directiveReplaceRowCsv: 
    type: object 
    allOf: 
     - $ref: '#/definitions/directive' 
     - properties: 
      description: 
      type: string 
      provider: 
      $ref: '#/definitions/providerCsv' 
    directiveReplaceColCsv: 
    type: object 
    allOf: 
     - $ref: '#/definitions/directive' 
     - properties: 
      description: 
      type: string 
      fromColumn: 
      type: string 
      toColumn: 
      type: string 
      provider: 
      $ref: '#/definitions/providerCsv' 
    directiveReplaceColSql: 
    type: object 
    allOf: 
     - $ref: '#/definitions/directive' 
     - properties: 
      description: 
      type: string 
      fromColumn: 
      type: string 
      toColumn: 
      type: string 
      provider: 
      $ref: '#/definitions/providerSql' 
    directiveInsertTag: 
    type: object 
    allOf: 
     - $ref: '#/definitions/directive' 
     - properties: 
      description: 
      type: string 
      notLast: 
      type: array 
      items: 
       type: string 
      onlyLast: 
      type: array 
      items: 
       type: string 
      provider: 
      $ref: '#/definitions/providerTag' 
     directiveInsertSql: 
     type: object 
     allOf: 
      - $ref: '#/definitions/directive' 
      - properties: 
       description: 
       type: string 
       notLast: 
       type: array 
       items: 
        type: string 
       onlyLast: 
       type: array 
       items: 
        type: string 
       provider: 
       $ref: '#/definitions/providerSql' 
     directiveInsertCsv: 
     type: object 
     allOf: 
      - $ref: '#/definitions/directive' 
      - properties: 
       description: 
       type: string 
       notLast: 
       type: array 
       items: 
        type: string 
       onlyLast: 
       type: array 
       items: 
        type: string 
       provider: 
       $ref: '#/definitions/providerCsv' 

risposta

10

OpenAPI specifica 3.0 sosterrà oneOf e anyOf.

In 2.0, è possibile definire un oggetto con proprietà variabili come solo type: object (oggetto in formato libero). Per il vostro caso, si consiglia di fare questo:

 schema: 
     type: array 
     items: 
      type: object 
+5

Questo non risponde alla domanda se si desidera definire tali oggetti. Oppure finisci con nessun modello, che potrebbe essere accettabile ma certamente non il migliore. – koxon

+0

Ciò consente anche solo di avere un elemento nell'array. fa schifo. –

5

È possibile impostare il items: riferimento al tipo di base. Il modello di ereditarietà varia in base alla lingua durante l'esportazione da swagger in particolare, ma in pratica le definizioni dei metodi specificano i tipi di parametri accettabili utilizzando il modello di base se si desidera essere in grado di accettare più sottoclassi che ereditano lo stesso modello di base.

Swagger frammento -

definitions: 
    template: 
    type: object 
    properties: 
     collection: 
     type: string 
     ... 
     directives: 
     type: array 
     items: 
      $ref: '#/definitions/directive' 
    directive: 
    type: object 
    discriminator: type 
    properties: 
     type: 
     type: integer 
     softFail: 
     type: boolean 
    required: 
     - type 
    directiveRequire: 
    allOf: 
    - $ref: '#/definitions/directive' 
    - type: object 
     properties: 
     tags: 
      type: array 
      items: 
      type: string 
    directiveReplace: 
    allOf: 
    - $ref: '#/definitions/directive' 
    - type: object 
     properties: 
     description: 
      type: string 
     from: 
      type: string 
     to: 
      type: string 

pseudo -

class template { 
    // all the other properties 
    directive[] directives; 
    function addDirective(directive newDirective) { 
    this.directives.push(newDirective); 
    } 
} 

class directive { 
    int type; 
    boolean softFail; 
} 

class directiveRequire inherits directive { 
//inherits type, softFail 
string[] tags; 
} 

class directiveReplace { 
    //inherits type, softFail 
    string description; 
    string from; 
    string to; 
} 

template templateOne = new template(); 

directiveReplace directiveOne = new directiveReplace(); 
directiveOne.type = "replace"; 
directiveOne.softFail = false; 
directiveOne.description = "first directive replace"; 
directiveOne.from = "first"; 
directiveOne.to = "one"; 

directiveRequire directiveTwo = new directiveRequire(); 
directiveTwo.type = "require"; 
directiveTwo.softFail = true; 
directiveTwo.tags = ["second","directive"]; 

templateOne.addDirective(directiveOne); 
templateOne.addDirective(directiveTwo); 
Problemi correlati