2016-11-03 6 views
2

現在の展開パターンでは、私の会社で使用されているSwaggerベースのUIで消費されるswagger json出力を手動で書き込む必要があります。私はjsonに、ボディ入力パラメータを含むすべての入力フィールドに対してSwagger UIを設定するための 'デフォルト'値を提供するよう書いています。私のbodyパラメータは、以下に示すように、参照されるオブジェクトです。 「この操作を試す」をクリックしたときに、返されたスワッガーJSONを定義して、要求の本体部分を自動入力する方法を教えてください。Swagger 2.0でJSONを定義する方法Swagger UIでデフォルトのボディパラメータオブジェクトを設定するには?

スワッガーUIにデフォルト/サンプルデータが設定されていないスワッガー仕様の例を以下に示します。

{ 
    "swagger":"2.0", 
    "info":{ 
     "description":"Example API Description", 
     "title":"Example Title", 
     "version":"v3.0" 
    }, 
    "tags":[ 
     { 
     "name":"v3" 
     } 
    ], 
    "consumes":[ 
     "application/json" 
    ], 
    "produces":[ 
     "application/json" 
    ], 
    "paths":{ 
     "/v3/api/{subscriptionID}/example":{ 
     "post":{ 
      "tags":[ 
       "v3" 
      ], 
      "description":"This API will do something", 
      "consumes":[ 
       "application/json" 
      ], 
      "produces":[ 
       "application/json" 
      ], 
      "parameters":[ 
       { 
        "name":"Accept", 
        "in":"header", 
        "description":"The Accept request-header field can be used to specify the media types which are acceptable for the response. If not provided, the default value will be application/json", 
        "required":false, 
        "default":"application/json", 
        "type":"string" 
       }, 
       { 
        "name":"Content-Type", 
        "in":"header", 
        "description":"The MIME type of the body of the request. Required for PUT, POST, and PATCH, where a request body is expected to be provided.", 
        "required":true, 
        "default":"application/json; charset=utf-8", 
        "type":"string" 
       }, 
       { 
        "name":"company_locale", 
        "in":"header", 
        "description":"The desired language as spoken in a particular region preference of the customer of this particular transaction. Consists of two lower case language", 
        "required":true, 
        "default":"en_US", 
        "type":"string" 
       }, 
       { 
        "name":"originatingip", 
        "in":"header", 
        "description":"The originating ip address of the calling device or browser.", 
        "required":true, 
        "default":"100.100.10.1", 
        "type":"string" 
       }, 
       { 
        "name":"transaction_id", 
        "in":"header", 
        "description":"The transaction identifier for this invocation of the service. ", 
        "required":true, 
        "default":"1e2bd51d-a865-4d37-9ac9-c345dc59119b", 
        "type":"string" 
       }, 
       { 
        "name":"subscriptionID", 
        "in":"path", 
        "description":"The unique identifier that represents the subscribed portfolio of products.", 
        "required":true, 
        "default":"1e2bd51d", 
        "type":"string" 
       }, 
       { 
        "name":"body", 
        "in":"body", 
        "description":"The body of the request", 
        "required":true, 
        "schema":{ 
        "$ref":"#/definitions/exampleDefinition" 
        } 
       } 
      ], 
      "responses":{ 
       "200":{ 
        "description":"OK", 
        "headers":{ 
        "transaction_id":{ 
         "type":"string", 
         "default":"de305d54-75b4-431b-adb2-eb6b9e546013", 
         "description":"The identifier for the service transaction attempt." 
        } 
        } 
       } 
      } 
     } 
     } 
    }, 
    "definitions":{ 
     "exampleDefinition":{ 
     "type":"object", 
     "description":"Request details for Example Definition", 
     "properties":{ 
      "action":{ 
       "type":"string", 
       "description":"Specifies the action to be taken" 
      }, 
      "applyToBase":{ 
       "type":"string", 
       "description":"A boolean value that defines the behaviour of the request against the base" 
      }, 
      "addOnIDs":{ 
       "type":"string", 
       "description":"The identifiers for the add-ons" 
      } 
     }, 
     "required":[ 
      "action", 
      "applyToBase", 
      "addOnIDs" 
     ] 
     } 
    } 
} 

私はFile->Paste JSON...をクリックすることでhttp://editor.swagger.io/#/でこのJSONをテストしてきました。私は"Try this operation"をクリックし、下にスクロールして、私のbodyパラメーターの値が入力されていないことを確認します。これは変更したいものです。

ご意見ありがとうございます。例の値を持つために

答えて

6

、あなただけの必要な「例」プロパティを追加する必要があります。

exampleDefinition: 
    type: object 
    description: Request details for Example Definition 
    properties: 
    action: 
     type: string 
     description: Specifies the action to be taken 
     example: An action value 
    applyToBase: 
     type: string 
     description: >- 
     A boolean value that defines the behaviour of the request against the base 
     example: An apply to base value 
    addOnIDs: 
     type: string 
     description: The identifiers for the add-ons 
     example: an ID 

残念ながらオンラインエディタはそれらを提案していないが、SwaggerUIはありません: SwaggerUI

-1

を移入するには"この操作を試してください"をクリックしたときに使用されるデフォルト値は、定義内のプロパティの「デフォルト」パラメータを追加するだけです。これは、オンラインエディタで動作します。

{ 
    "swagger":"2.0", 
    "info":{ 
     "description":"Example API Description", 
     "title":"Example Title", 
     "version":"v3.0" 
    }, 
    "tags":[ 
     { 
     "name":"v3" 
     } 
    ], 
    "consumes":[ 
     "application/json" 
    ], 
    "produces":[ 
     "application/json" 
    ], 
    "paths":{ 
     "/v3/api/{subscriptionID}/example":{ 
     "post":{ 
      "tags":[ 
       "v3" 
      ], 
      "description":"This API will do something", 
      "consumes":[ 
       "application/json" 
      ], 
      "produces":[ 
       "application/json" 
      ], 
      "parameters":[ 
       { 
        "name":"Accept", 
        "in":"header", 
        "description":"The Accept request-header field can be used to specify the media types which are acceptable for the response. If not provided, the default value will be application/json", 
        "required":false, 
        "default":"application/json", 
        "type":"string" 
       }, 
       { 
        "name":"Content-Type", 
        "in":"header", 
        "description":"The MIME type of the body of the request. Required for PUT, POST, and PATCH, where a request body is expected to be provided.", 
        "required":true, 
        "default":"application/json; charset=utf-8", 
        "type":"string" 
       }, 
       { 
        "name":"company_locale", 
        "in":"header", 
        "description":"The desired language as spoken in a particular region preference of the customer of this particular transaction. Consists of two lower case language", 
        "required":true, 
        "default":"en_US", 
        "type":"string" 
       }, 
       { 
        "name":"originatingip", 
        "in":"header", 
        "description":"The originating ip address of the calling device or browser.", 
        "required":true, 
        "default":"100.100.10.1", 
        "type":"string" 
       }, 
       { 
        "name":"transaction_id", 
        "in":"header", 
        "description":"The transaction identifier for this invocation of the service. ", 
        "required":true, 
        "default":"1e2bd51d-a865-4d37-9ac9-c345dc59119b", 
        "type":"string" 
       }, 
       { 
        "name":"subscriptionID", 
        "in":"path", 
        "description":"The unique identifier that represents the subscribed portfolio of products.", 
        "required":true, 
        "default":"1e2bd51d", 
        "type":"string" 
       }, 
       { 
        "name":"body", 
        "in":"body", 
        "description":"The body of the request", 
        "required":true, 
        "schema":{ 
        "$ref":"#/definitions/exampleDefinition" 
        } 
       } 
      ], 
      "responses":{ 
       "200":{ 
        "description":"OK", 
        "headers":{ 
        "transaction_id":{ 
         "type":"string", 
         "default":"de305d54-75b4-431b-adb2-eb6b9e546013", 
         "description":"The identifier for the service transaction attempt." 
        } 
        } 
       } 
      } 
     } 
     } 
    }, 
    "definitions":{ 
     "exampleDefinition":{ 
     "type":"object", 
     "description":"Request details for Example Definition", 
     "properties":{ 
      "action":{ 
       "type":"string", 
       "description":"Specifies the action to be taken", 
       "default": "The default Action" 
      }, 
      "applyToBase":{ 
       "type":"string", 
       "description":"A boolean value that defines the behaviour of the request against the base", 
       "default": "0" 
      }, 
      "addOnIDs":{ 
       "type":"string", 
       "description":"The identifiers for the add-ons", 
       "default": "The default Add-On" 
      } 
     }, 
     "required":[ 
      "action", 
      "applyToBase", 
      "addOnIDs" 
     ] 
     } 
    } 
} 

`` `

+0

-1。例の値は、 'example'キーワードではなく(Arnaud Lauretの答えのように)、' default'ではなく指定する必要があります。 'default'は[異なる意味](https://stackoverflow.com/a/46172865/113116)ですが、オプションフィールドの属性です。 – Helen

関連する問題