2016-11-28 25 views
2

我使用Servicestack版本4.0.52.0及ServiceStack.Api.Swagger 4.0.0.0ServiceStack.Api.Swagger SwaggerResourcesService排除路由與路由配置begining

我在serviceStack應用主機定義的可變參數的路由的所有路由導出類(AppHostBase):配置方法

public class AppHost : AppHostBase 
{ 
    public override void Configure(Funq.Container container) 
    { 
     //Infrastructure code goes here 
     .... 

     //Add routes 
     Routes.Add<TestRequestDTO>("/{MyApplicationKey}/TestOperation/", "GET") 
    } 
} 

雖然這些航線在ServiceStack元數據頁可見,招搖UI不顯示路線與MyApplicationKey begining;更仔細地分析我們的代碼庫中發現ServiceStack.Api.Swagger.SwaggerResourcesService.Get方法問題:

[AddHeader(DefaultContentType = MimeTypes.Json)] 
    [DefaultRequest(typeof(SwaggerResources))] 
    [Restrict(VisibilityTo = RequestAttributes.None)] 
    public class SwaggerResourcesService : Service 
    { 
     private readonly Regex resourcePathCleanerRegex = new Regex(@"/[^\/\{]*", RegexOptions.Compiled); 
     internal static Regex resourceFilterRegex; 

     internal static Action<SwaggerResourcesResponse> ResourcesResponseFilter { get; set; } 

     internal const string RESOURCE_PATH = "/resource"; 

     public object Get(SwaggerResources request) 
     { 
      var basePath = base.Request.GetBaseUrl(); 

      var result = new SwaggerResourcesResponse 
      { 
       BasePath = basePath, 
       Apis = new List<SwaggerResourceRef>(), 
       ApiVersion = HostContext.Config.ApiVersion, 
       Info = new SwaggerInfo 
       { 
        Title = HostContext.ServiceName, 
       } 
      }; 
      var operations = HostContext.Metadata; 
      var allTypes = operations.GetAllOperationTypes(); 
      var allOperationNames = operations.GetAllOperationNames(); 
      foreach (var operationName in allOperationNames) 
      { 
       if (resourceFilterRegex != null && !resourceFilterRegex.IsMatch(operationName)) continue; 
       var name = operationName; 
       var operationType = allTypes.FirstOrDefault(x => x.Name == name); 
       if (operationType == null) continue; 
       if (operationType == typeof(SwaggerResources) || operationType == typeof(SwaggerResource)) 
        continue; 
       if (!operations.IsVisible(Request, Format.Json, operationName)) continue; 

       CreateRestPaths(result.Apis, operationType, operationName); 
      } 

      result.Apis = result.Apis.OrderBy(a => a.Path).ToList(); 

      if (ResourcesResponseFilter != null) 
       ResourcesResponseFilter(result); 

      return new HttpResult(result) { 
       ResultScope =() => JsConfig.With(includeNullValues:false) 
      }; 
     } 

     protected void CreateRestPaths(List<SwaggerResourceRef> apis, Type operationType, string operationName) 
     { 
      var map = HostContext.ServiceController.RestPathMap; 
      var feature = HostContext.GetPlugin<SwaggerFeature>(); 

      var paths = new List<string>(); 

      foreach (var key in map.Keys) 
      { 
       paths.AddRange(map[key].Where(x => x.RequestType == operationType).Select(t => **resourcePathCleanerRegex.Match**(t.Path).Value)); 
      } 
...... 
} 

對我們如何才能解決這個問題的任何提示?在進行資源清單時,是否有任何方法可將MyApplicationKey的值告訴Swagger API?

+0

您可以隨時發送拉取請求。 – mythz

回答

2

有指定自定義ResourcesResponseFilter當你註冊SwaggerFeature插件,e.g修改揚鞭答覆的機會:

Plugins.Add(new SwaggerFeature { 
    ResourceFilterPattern = @"/[^\/\{]*", 
    ResourcesResponseFilter = response => ... 
}); 

這將讓你修改揚鞭響應該揚鞭UI接收。

+0

是的,ResourcesResponseFilter允許我們更改/過濾從SwaggerResourcesService的Get()方法生成的結果,但是因爲我使用{MyApplicationKey}作爲每個API url的前綴,所以SwaggerResourcesService的Get()方法避免了由於這個** private只讀Regex resourcePathCleanerRegex = new Regex(@「/ [^ \/\ {* *,RegexOptions.Compiled); **正則表達式,因此我沒有在ResourcesResponseFilter的結果中獲得我的Url資源。 –

+0

@DishamParikh你也可以用[SwaggerFeature.ResourceFilterPattern](https://github.com/ServiceStack/ServiceStack/blob/master/src/ServiceStack.Api.Swagger/SwaggerFeature.cs#L52)修改'resourcePathCleanerRegex' – mythz

+0

yes,但SwaggerFeature.ResourceFilterPattern不是委託給resourcePathCleanerRegex,它已用於SwaggerResourcesService的CreateRestPaths()方法中,該方法是CreatePaths()的代碼部分,即https://github.com/ServiceStack/ServiceStack/blob/master/src/ServiceStack.Api .Swagger/SwaggerResourcesService.cs#L120正在轉義{MyApplicationKey} –