2017-06-20 81 views
0

我目前已經設置了我的解決方案來爲每個端點生成Swagger文檔。不過,我有幾個端點只能用於管理員。在你的下方你將能夠看到一個例子。Swagger爲管理員和普通用戶生成不同的文檔

一個普通用戶可以創建模型,但是隻有管理員可以拉動數據庫中的每個模型。

面臨的挑戰是生成2套swagger文檔?一個供普通用戶查看,另一個供Admin用戶查看。我知道,如果我將[ApiExplorerSettings(IgnoreApi = true)]添加到我的終點,它將不會出現在生成的文檔中,但這意味着我的管理員用戶無法看到那些重要的文檔。關於如何根據用戶動態生成兩組文檔的任何建議都將有所幫助。

[SwaggerResponse((int)System.Net.HttpStatusCode.OK, Type = typeof(RestOkResponse<PackageResponse>))] 
[SwaggerResponse((int)System.Net.HttpStatusCode.InternalServerError, Type = typeof(RestErrorResponse))] 
[SwaggerResponse((int)System.Net.HttpStatusCode.BadRequest, Type = typeof(RestErrorResponse))] 
[SwaggerResponse((int)System.Net.HttpStatusCode.Forbidden, Type = typeof(RestErrorResponse))] 
[SwaggerResponse((int)System.Net.HttpStatusCode.NotFound)] 
[HttpPost("/v1/packages")] 
[Authorize()] 
public async Task<IActionResult> CreateModel([FromBody]Request request) 
{ 
    ... 
} 

的下面方法是僅管理員:

[SwaggerResponse((int)System.Net.HttpStatusCode.OK, Type = typeof(RestOkResponse<PackageResponse>))] 
[SwaggerResponse((int)System.Net.HttpStatusCode.InternalServerError, Type = typeof(RestErrorResponse))] 
[SwaggerResponse((int)System.Net.HttpStatusCode.BadRequest, Type = typeof(RestErrorResponse))] 
[SwaggerResponse((int)System.Net.HttpStatusCode.Forbidden, Type = typeof(RestErrorResponse))] 
[SwaggerResponse((int)System.Net.HttpStatusCode.NotFound)] 
[ApiExplorerSettings(IgnoreApi = true)] 
[HttpPost("/v1/packages")] 
[Authorize()] 
public async Task<IActionResult> GetAllModelsFromDatabase([FromBody]Request request) 
{ 
    ... 
} 

回答

0

的動態過程,在這個答案找到。

Dynamically Ignore WebAPI method on controller for api explorer documentation

它可以昂首闊步文件分離。然而,沒有內置的方法來做到這一點。一個人必須要去除未想節點從一個文檔文件: https://github.com/swagger-api/swagger-editor/issues/233

此,如果你主持編輯自己那麼 parameters_common.yaml路徑可以得到解決的HTTP路徑工作正常,在當前編輯器。 目前無法在文件之間跳轉或創建新文件。 如果你正在用Swagger做一個大項目,我建議你自己託管 編輯器。當編輯器和您正在構建的API位於同一個源的 上時,XHR調用不必是跨源的,它可以幫助編輯器 在「嘗試操作」中顯示有關調用的更多詳細信息,而您的API 沒有具有跨越源標題。

有關如何將swagger文件分割成更小節點的示例。 http://azimi.me/2015/07/16/split-swagger-into-smaller-files.html

相關問題