diff --git a/Swashbuckle.OData.Nuget/Swashbuckle.OData.NuGet.nuproj b/Swashbuckle.OData.Nuget/Swashbuckle.OData.NuGet.nuproj index f03b018..d8124c5 100644 --- a/Swashbuckle.OData.Nuget/Swashbuckle.OData.NuGet.nuproj +++ b/Swashbuckle.OData.Nuget/Swashbuckle.OData.NuGet.nuproj @@ -30,7 +30,7 @@ https://github.com/rbeauchamp/Swashbuckle.OData/blob/master/License.txt Copyright 2015 Swashbuckle Swagger SwaggerUi OData Documentation Discovery Help WebApi AspNet AspNetWebApi Docs WebHost IIS - 2.2.1 + 2.2.2 diff --git a/Swashbuckle.OData.Sample/App_Start/ODataConfig.cs b/Swashbuckle.OData.Sample/App_Start/ODataConfig.cs index bc4298a..7d0425f 100644 --- a/Swashbuckle.OData.Sample/App_Start/ODataConfig.cs +++ b/Swashbuckle.OData.Sample/App_Start/ODataConfig.cs @@ -21,6 +21,10 @@ public static void Register(HttpConfiguration config) config.MapODataServiceRoute("V1RouteVersioning", "odata/v1", GetModel()); controllerSelector.RouteVersionSuffixMapping.Add("V1RouteVersioning", "V1"); + // Define a versioned route that doesn't map to any controller + config.MapODataServiceRoute("odata/v2", "odata/v2", GetFakeModel()); + controllerSelector.RouteVersionSuffixMapping.Add("odata/v2", "V2"); + // Define a default non-versioned route config.MapODataServiceRoute("DefaultODataRoute", ODataRoutePrefix, GetModel()); } @@ -32,5 +36,12 @@ private static IEdmModel GetModel() builder.EntitySet("Orders"); return builder.GetEdmModel(); } + + private static IEdmModel GetFakeModel() + { + var builder = new ODataConventionModelBuilder(); + builder.EntitySet("FakeCustomers"); + return builder.GetEdmModel(); + } } } \ No newline at end of file diff --git a/Swashbuckle.OData.Tests/Fixtures/ODataSwaggerProviderTests.cs b/Swashbuckle.OData.Tests/Fixtures/ODataSwaggerProviderTests.cs index 122372e..1121b1d 100644 --- a/Swashbuckle.OData.Tests/Fixtures/ODataSwaggerProviderTests.cs +++ b/Swashbuckle.OData.Tests/Fixtures/ODataSwaggerProviderTests.cs @@ -45,6 +45,22 @@ public async Task It_supports_multiple_odata_routes() } } + [Test] + public async Task It_supports_odata_routes_that_dont_map_to_a_controller() + { + using (WebApp.Start(TestWebApiStartup.BaseAddress, appBuilder => new TestWebApiStartup().Configuration(appBuilder))) + { + // Arrange + var httpClient = HttpClientUtils.GetHttpClient(); + + // Act + var swaggerDocument = await httpClient.GetJsonAsync("swagger/docs/v1"); + + // Assert + swaggerDocument.Should().NotBeNull(); + } + } + [Test] public async Task It_explores_the_correct_controller() { diff --git a/Swashbuckle.OData/Descriptions/ODataApiExplorer.cs b/Swashbuckle.OData/Descriptions/ODataApiExplorer.cs index a8c6d02..8766bcb 100644 --- a/Swashbuckle.OData/Descriptions/ODataApiExplorer.cs +++ b/Swashbuckle.OData/Descriptions/ODataApiExplorer.cs @@ -116,21 +116,6 @@ private ApiDescription GetApiDescription(HttpMethod httpMethod, ODataRoute oData requestContext.Url = new UrlHelper(request); requestContext.VirtualPathRoot = perControllerConfig.VirtualPathRoot; - //var oDataPathRouteConstraint = GetODataPathRouteConstraint(oDataRoute); - - //var controllerContext = new HttpControllerContext - //{ - // Configuration = perControllerConfig, - // Request = request, - // RequestContext = requestContext, - // RouteData = request.GetRouteData(), - // ControllerDescriptor = httpControllerDescriptor - //}; - - //var actionMappings = perControllerConfig.Services.GetActionSelector().GetActionMapping(httpControllerDescriptor); - - //var action = GetActionName(oDataPathRouteConstraint, odataPath, controllerContext, actionMappings); - var controller = controllerDesciptor.CreateController(request); HttpActionDescriptor actionDescriptor; using (controller as IDisposable) @@ -363,28 +348,18 @@ private static string GenerateSampleQueryParameterValue(Parameter queryParameter private HttpControllerDescriptor GetControllerDesciptor(HttpRequestMessage request) { - return _config().Services.GetHttpControllerSelector().SelectController(request); - } - - /// - /// Selects the name of the controller to dispatch the request to. - /// - /// The o data path route constraint. - /// The OData path of the request. - /// The request. - /// - /// The name of the controller to dispatch to, or null if one cannot be resolved. - /// - private static string GetControllerName(ODataPathRouteConstraint oDataPathRouteConstraint, ODataPath path, HttpRequestMessage request) - { - return oDataPathRouteConstraint.RoutingConventions - .Select(routingConvention => routingConvention.SelectController(path, request)) - .FirstOrDefault(controllerName => controllerName != null); - } - - private static string GetActionName(ODataPathRouteConstraint oDataPathRouteConstraint, ODataPath path, HttpControllerContext controllerContext, ILookup actionMap) - { - return oDataPathRouteConstraint.RoutingConventions.Select(routingConvention => routingConvention.SelectAction(path, controllerContext, actionMap)).FirstOrDefault(action => action != null); + try + { + return _config().Services.GetHttpControllerSelector().SelectController(request); + } + catch (HttpResponseException ex) + { + if (ex.Response.StatusCode == HttpStatusCode.NotFound) + { + return null; + } + throw; + } } public static string FindMatchingAction(ILookup actionMap, params string[] targetActionNames) diff --git a/Swashbuckle.OData/Properties/AssemblyInfo.cs b/Swashbuckle.OData/Properties/AssemblyInfo.cs index 2e7fe92..76405fc 100644 --- a/Swashbuckle.OData/Properties/AssemblyInfo.cs +++ b/Swashbuckle.OData/Properties/AssemblyInfo.cs @@ -37,4 +37,4 @@ [assembly: AssemblyVersion("1.0.0.0")] [assembly: AssemblyFileVersion("1.0.0.0")] -[assembly: AssemblyInformationalVersion("2.2.1")] \ No newline at end of file +[assembly: AssemblyInformationalVersion("2.2.2")] \ No newline at end of file