2017-02-01 4 views
1

OData照会をサポートするASP.Net Web API 2エンドポイントがあります。それはそのようなものです:Web API 2 IQueryable <T>エンドポイントにODataパラメータを追加するSwashbuckleを入手する方法はありますか?

[HttpGet, Route("")] 
public IQueryable<Thing> Get() 
{ 
    return _thingsRepository.Query(); 
} 

OData $フィルタパラメータとそのような素晴らしい仕事。私は彼らが実際のODataコントローラのようにSwaggerに現れることを望みます。

私はSwashbuckle.ODataを使用しています...しかし、私は本当にこの場合は何も買っていないと確信しています。

答えて

3

SwaroBuckleを使用してSwaggerに使用するパラメータを追加するには、IOperationFilterを使用するのはかなり簡単です。 c.OperationFilter<ODataParametersSwaggerDefinition>();を使用してSwagger Configurationで追加できます。私はいくつかのODataパラメータをAPIのすべてのIQueryableエンドポイントに追加するものを作成しました:

/// <summary> 
///  Add the supported odata parameters for IQueryable endpoints. 
/// </summary> 
public class ODataParametersSwaggerDefinition : IOperationFilter 
{ 
    private static readonly Type QueryableType = typeof(IQueryable); 

    /// <summary> 
    ///  Apply the filter to the operation. 
    /// </summary> 
    /// <param name="operation">The API operation to check.</param> 
    /// <param name="schemaRegistry">The swagger schema registry.</param> 
    /// <param name="apiDescription">The description of the api method.</param> 
    public void Apply(Operation operation, SchemaRegistry schemaRegistry, ApiDescription apiDescription) 
    { 
     var responseType = apiDescription.ResponseType(); 

     if (responseType.GetInterfaces().Any(i => i == QueryableType)) 
     { 
      operation.parameters.Add(new Parameter 
      { 
       name = "$filter", 
       description = "Filter the results using OData syntax.", 
       required = false, 
       type = "string", 
       @in = "query" 
      }); 

      operation.parameters.Add(new Parameter 
      { 
       name = "$orderby", 
       description = "Order the results using OData syntax.", 
       required = false, 
       type = "string", 
       @in = "query" 
      }); 

      operation.parameters.Add(new Parameter 
      { 
       name = "$skip", 
       description = "The number of results to skip.", 
       required = false, 
       type = "integer", 
       @in = "query" 
      }); 

      operation.parameters.Add(new Parameter 
      { 
       name = "$top", 
       description = "The number of results to return.", 
       required = false, 
       type = "integer", 
       @in = "query" 
      }); 

      operation.parameters.Add(new Parameter 
      { 
       name = "$count", 
       description = "Return the total count.", 
       required = false, 
       type = "boolean", 
       @in = "query" 
      }); 
     } 
    } 
} 
関連する問題