From f854b821e6fbb5bc90ada9b45ead83262b7d9d84 Mon Sep 17 00:00:00 2001 From: Richard Beauchamp Date: Sun, 27 Dec 2015 04:12:17 -0800 Subject: [PATCH] Fix issue with custom routing. Closes #29. --- .../Swashbuckle.OData.NuGet.nuproj | 4 +-- ...stomNavigationPropertyRoutingConvention.cs | 13 +++++++--- .../ODataControllers/OrdersController.cs | 5 ++++ .../Fixtures/CustomRouteTests.cs | 26 +++++++++++++++++++ .../Descriptions/ODataSwaggerUtilities.cs | 3 ++- Swashbuckle.OData/Properties/AssemblyInfo.cs | 2 +- 6 files changed, 45 insertions(+), 8 deletions(-) diff --git a/Swashbuckle.OData.Nuget/Swashbuckle.OData.NuGet.nuproj b/Swashbuckle.OData.Nuget/Swashbuckle.OData.NuGet.nuproj index c9dd453..4c67521 100644 --- a/Swashbuckle.OData.Nuget/Swashbuckle.OData.NuGet.nuproj +++ b/Swashbuckle.OData.Nuget/Swashbuckle.OData.NuGet.nuproj @@ -25,12 +25,12 @@ Richard Beauchamp Extends Swashbuckle with OData v4 support! Extends Swashbuckle with OData v4 support! - Limits the swagger model-schema to the top-level entity + Fix issue with custom routes https://github.com/rbeauchamp/Swashbuckle.OData 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.8.0 + 2.8.1 diff --git a/Swashbuckle.OData.Sample/App_Start/CustomNavigationPropertyRoutingConvention.cs b/Swashbuckle.OData.Sample/App_Start/CustomNavigationPropertyRoutingConvention.cs index 2af5ef0..a6dcf46 100644 --- a/Swashbuckle.OData.Sample/App_Start/CustomNavigationPropertyRoutingConvention.cs +++ b/Swashbuckle.OData.Sample/App_Start/CustomNavigationPropertyRoutingConvention.cs @@ -13,10 +13,15 @@ public override string SelectAction(ODataPath odataPath, HttpControllerContext c { var controllerType = controllerContext.Controller.GetType(); - if (typeof (CustomersController) == controllerType) + if (typeof(CustomersController) == controllerType) { + if (odataPath.PathTemplate.Equals("~/entityset/key/navigation")) //POST OR GET + { + controllerContext.RouteData.Values["orderID"] = (odataPath.Segments[1] as KeyValuePathSegment).Value; + return controllerContext.Request.Method.ToString(); + } } - else if (typeof (OrdersController) == controllerType) + else if (typeof(OrdersController) == controllerType) { if (odataPath.PathTemplate.Equals("~/entityset/key/navigation")) //POST OR GET { @@ -41,8 +46,8 @@ public override string SelectController(ODataPath odataPath, HttpRequestMessage if (odataPath.PathTemplate.Contains("~/entityset/key/navigation")) { // Find controller. Controller should be last navigation property - return ODataSegmentKinds.Navigation == odataPath.Segments[odataPath.Segments.Count - 1].SegmentKind - ? odataPath.Segments[odataPath.Segments.Count - 1].ToString() + return ODataSegmentKinds.Navigation == odataPath.Segments[odataPath.Segments.Count - 1].SegmentKind + ? odataPath.Segments[odataPath.Segments.Count - 1].ToString() : odataPath.Segments[odataPath.Segments.Count - 2].ToString(); } return base.SelectController(odataPath, request); diff --git a/Swashbuckle.OData.Sample/ODataControllers/OrdersController.cs b/Swashbuckle.OData.Sample/ODataControllers/OrdersController.cs index 2891ce2..ddb862d 100644 --- a/Swashbuckle.OData.Sample/ODataControllers/OrdersController.cs +++ b/Swashbuckle.OData.Sample/ODataControllers/OrdersController.cs @@ -47,6 +47,11 @@ public async Task Post([FromODataUri] int customerId, Order o return Created(order); } + public async Task Delete([FromODataUri] int customerId, [FromODataUri] Guid orderID) + { + throw new NotImplementedException(); + } + /// /// Query the order by id /// diff --git a/Swashbuckle.OData.Tests/Fixtures/CustomRouteTests.cs b/Swashbuckle.OData.Tests/Fixtures/CustomRouteTests.cs index 6104708..7b411c8 100644 --- a/Swashbuckle.OData.Tests/Fixtures/CustomRouteTests.cs +++ b/Swashbuckle.OData.Tests/Fixtures/CustomRouteTests.cs @@ -41,6 +41,27 @@ public async Task It_allows_definition_of_custom_routes() } } + [Test] + public async Task It_allows_definition_of_custom_delete_routes() + { + using (WebApp.Start(HttpClientUtils.BaseAddress, builder => Configuration(builder, typeof(OrdersController)))) + { + // Arrange + var httpClient = HttpClientUtils.GetHttpClient(HttpClientUtils.BaseAddress); + + // Act + var swaggerDocument = await httpClient.GetJsonAsync("swagger/docs/v1"); + + // Assert + PathItem pathItem; + swaggerDocument.paths.TryGetValue("/odata/Customers({Id})/Orders({orderID})", out pathItem); + pathItem.Should().NotBeNull(); + pathItem.delete.Should().NotBeNull(); + + await ValidationUtils.ValidateSwaggerJson(); + } + } + private static void Configuration(IAppBuilder appBuilder, Type targetController) { var config = appBuilder.GetStandardHttpConfig(targetController); @@ -54,6 +75,11 @@ private static void Configuration(IAppBuilder appBuilder, Type targetController) .PathParameter("Id") .BodyParameter("order"); + config.AddCustomSwaggerRoute(customODataRoute, "/Customers({Id})/Orders({orderID})") + .Operation(HttpMethod.Delete) + .PathParameter("Id") + .PathParameter("orderID"); + config.EnsureInitialized(); } diff --git a/Swashbuckle.OData/Descriptions/ODataSwaggerUtilities.cs b/Swashbuckle.OData/Descriptions/ODataSwaggerUtilities.cs index 23c6f88..da6be70 100644 --- a/Swashbuckle.OData/Descriptions/ODataSwaggerUtilities.cs +++ b/Swashbuckle.OData/Descriptions/ODataSwaggerUtilities.cs @@ -685,7 +685,8 @@ internal static IList Parameter(this IList parameters, str { name = name, @in = kind, - description = description + description = description, + required = true }; if (kind != "body") diff --git a/Swashbuckle.OData/Properties/AssemblyInfo.cs b/Swashbuckle.OData/Properties/AssemblyInfo.cs index 0cc5f8f..65aeff7 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.8.0")] \ No newline at end of file +[assembly: AssemblyInformationalVersion("2.8.1")] \ No newline at end of file