2016-03-30 17 views
9

さまざまな種類のオブジェクトの配列を含むオブジェクトのスワッガースキーマ定義を定義しようとしています。さまざまな種類の配列を含むスガッガースキーマを作成する方法

ここにテンプレートオブジェクト(およびすべての関連オブジェクトタイプ)のjsonスキーマがあります。私はswaggerがoneOf述語をサポートしていないことを知っています。だから、私はこのデータ構造をいかにしてスワッガーで記述するかを考えようとしています。私はこの構文上の多くのバリエーションを試してみたが、どれも働いていないと、これは私が仕様とここにあるいくつかの例をもとに来ることができ最も近かった: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' 

答えて

10

OpenAPIの仕様3.0はoneOfanyOfをサポートします。

2.0では、さまざまなプロパティを持つオブジェクトをtype: object(自由形式のオブジェクト)として定義することができます。あなたのケースのために、あなたはこれをやってみたいことがあります。

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

あなたがそれらのオブジェクトを定義したい場合、これは質問に答えていません。あるいは、モデルがなくても結構ですが、それは容認できるかもしれませんが、確かに最善ではありません。 – koxon

+0

これはまたあなたの配列の中に一つのアイテムしか持てないようにします。吸う。 –

5

あなたは、ベース型にitems:の参照を設定することができます。継承モデルは、エクスポート時に言語ごとに具体的に異なりますが、実際には、同じ基本モデルを継承する複数のサブクラスを受け入れることができるようにするには、基本モデルを使用して許容可能なパラメータ型を指定します。

闊歩スニペット -

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 

擬似コード -

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); 
関連する問題