2017-06-08 68 views
0

語境:在1.2規範從當前REST文檔轉換揚鞭至2.0apiKey如揚鞭UI查詢參數2.0

環境:爪哇8,招搖-行家-插件3.0.1,招搖註解(COM .wordnik)

我卡在哪裏:我能夠成功生成REST API文檔。但是,REST API需要一個ApiKey作爲Query參數。在1.2規範,這是使用的index.html

function addApiKeyAuthorization() { 
    var key = $('#input_apiKey')[0].value; 
    log("key: " + key); 
    if(key && key.trim() != "") { 
     log("added key " + key); 
     //window.authorizations.add("api_key", new ApiKeyAuthorization("api_key", key, "query")); 
     window.authorizations.add("apiKey", new ApiKeyAuthorization("apiKey", key, "header")); 
    } 
    } 

    $('#input_apiKey').change(function() { 
    addApiKeyAuthorization(); 
    }); 

    // if you have an apiKey you would like to pre-populate on the page for demonstration purposes... 

    var apiKey = "ABCD"; 
    $('#input_apiKey').val(apiKey); 
    addApiKeyAuthorization(); 

但是下面的代碼段,用於2.0規範添加,我的搜索導致了YAML文件進行以下更改。

securityDefinitions: 
UserSecurity: 
    type: apiKey 
    in: header 
    name:myApiKey 

當前的index.html在窗函數如下:

window.onload = function() { 
    // Build a system 
    const ui = SwaggerUIBundle({ 
    url: "http://someCoolsite.com/swagger.json", 
    dom_id: '#swagger-ui', 
    presets: [ 
     SwaggerUIBundle.presets.apis, 
     SwaggerUIStandalonePreset 
    ], 
    plugins: [ 
     SwaggerUIBundle.plugins.DownloadUrl 
    ], 
    layout: "StandaloneLayout" 
    }) 
    window.ui = ui 
} 

回答

0

進一步的探索後,我已經找到答案,上面我的問題。

第一:我的index.html是如下:

<script> 
$(function(){ 
    window.onload = function() { 
    // Build a system 
    const ui = SwaggerUIBundle({ 
    url: "http://www.webhostingsite.com/swagger.json", 
    dom_id: '#swagger-ui', 
    presets: [ 
     SwaggerUIBundle.presets.apis, 
     SwaggerUIStandalonePreset 
    ], 
    plugins: [ 
     SwaggerUIBundle.plugins.DownloadUrl 
    ], 
    layout: "StandaloneLayout" 
    }) 
    window.ui = ui 
} 
window.onFailure = function(data) { 
    log("Unable to Load SwaggerUI"); 
} 

function addApiKeyAuthorization() { 
    var key = $('#input_apiKey')[0].value; 
    log("key: " + key); 
    if(key && key.trim() != "") { 
    log("added key " + key); 
    //window.authorizations.add("api_key", new ApiKeyAuthorization("api_key", key, "query")); 
    window.authorizations.add("apiKey", new ApiKeyAuthorization("apiKey", key, "query")); 
    } 
} 

$('#input_apiKey').change(function() { 
    addApiKeyAuthorization(); 
}); 
}); 

然後,我更新了我的swagger.json爲下面:

{ 
    "swagger" : "2.0", 
    "securityDefinitions": { 
    "apiKey": { 
    "type": "apiKey", 
    "name": "apiKey", 
     "in": "query" 
    } 
    }, 
    "host" : "<api base path>", 
    "basePath" : "/v1", 
    "security": [{"apiKey": []}]", //Global security (applies to all operations) 
    ....... 

三:託管指標。用於靜態Web託管的AWS S3上的html和swagger.json。

我出錯的部分是,"security": [{"apiKey": []}]"

我一直在做"security":{"apiKey":[]}一直忘記「安全」的價值是一個列表。

希望這會有所幫助。