2016-03-30 6 views
9

मैं किसी ऑब्जेक्ट के लिए एक swagger स्कीमा परिभाषा को परिभाषित करने का प्रयास कर रहा हूं जिसमें विभिन्न प्रकार की वस्तुओं की एक सरणी शामिल है।आप एक swagger स्कीमा कैसे बनाते हैं जिसमें विभिन्न प्रकारों की एक सरणी शामिल है

यहां टेम्पलेट ऑब्जेक्ट (और सभी संबंधित ऑब्जेक्ट प्रकार) के लिए जेसन स्कीमा है। मुझे पता है कि swagger भविष्यवाणी करने के लिए एक का समर्थन नहीं करता है, इसलिए मैं सिर्फ यह समझने की कोशिश कर रहा हूं कि इस डेटा संरचना को स्वैगर में कैसे वर्णन किया जाए। मैं इस वाक्य रचना पर कई रूपों की कोशिश की है, लेकिन कोई भी काम किया है और यह निकटतम मैं कल्पना के आधार पर आ सकता था और कुछ उदाहरण यहां मिली: 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 oneOf और anyOf का समर्थन करेंगे।

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); 
संबंधित मुद्दे