-
Notifications
You must be signed in to change notification settings - Fork 96
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #26 from rbeauchamp/feature/21
Provide support for custom routes
- Loading branch information
Showing
67 changed files
with
2,118 additions
and
666 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
Swashbuckle.OData.Sample/App_Start/CustomNavigationPropertyRoutingConvention.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
using System.Linq; | ||
using System.Net.Http; | ||
using System.Web.Http.Controllers; | ||
using System.Web.OData.Routing; | ||
using System.Web.OData.Routing.Conventions; | ||
using SwashbuckleODataSample.ODataControllers; | ||
|
||
namespace SwashbuckleODataSample | ||
{ | ||
public class CustomNavigationPropertyRoutingConvention : EntitySetRoutingConvention | ||
{ | ||
public override string SelectAction(ODataPath odataPath, HttpControllerContext controllerContext, ILookup<string, HttpActionDescriptor> actionMap) | ||
{ | ||
var controllerType = controllerContext.Controller.GetType(); | ||
|
||
if (typeof (CustomersController) == controllerType) | ||
{ | ||
} | ||
else if (typeof (OrdersController) == controllerType) | ||
{ | ||
if (odataPath.PathTemplate.Equals("~/entityset/key/navigation")) //POST OR GET | ||
{ | ||
controllerContext.RouteData.Values["customerID"] = (odataPath.Segments[1] as KeyValuePathSegment).Value; | ||
return controllerContext.Request.Method.ToString(); | ||
} | ||
if (odataPath.PathTemplate.Equals("~/entityset/key/navigation/key")) //PATCH OR DELETE | ||
{ | ||
controllerContext.RouteData.Values["customerID"] = (odataPath.Segments[1] as KeyValuePathSegment).Value; | ||
|
||
controllerContext.RouteData.Values["key"] = (odataPath.Segments[3] as KeyValuePathSegment).Value; | ||
return controllerContext.Request.Method.ToString(); | ||
} | ||
} | ||
|
||
return base.SelectAction(odataPath, controllerContext, actionMap); | ||
} | ||
|
||
public override string SelectController(ODataPath odataPath, HttpRequestMessage request) | ||
{ | ||
// We use always use the last naviation as the controller vs. the initial entityset | ||
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() | ||
: odataPath.Segments[odataPath.Segments.Count - 2].ToString(); | ||
} | ||
return base.SelectController(odataPath, request); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
using System.Threading.Tasks; | ||
using FluentAssertions; | ||
using Microsoft.Owin.Hosting; | ||
using NUnit.Framework; | ||
using Swashbuckle.OData.Tests.WebHost; | ||
using Swashbuckle.Swagger; | ||
using SwashbuckleODataSample; | ||
|
||
namespace Swashbuckle.OData.Tests | ||
{ | ||
[TestFixture] | ||
public class CustomRouteTests | ||
{ | ||
[Test] | ||
public async Task It_allows_definition_of_custom_routes() | ||
{ | ||
using (WebApp.Start(TestWebApiStartup.BaseAddress, appBuilder => new TestWebApiStartup().Configuration(appBuilder))) | ||
{ | ||
// Arrange | ||
var httpClient = HttpClientUtils.GetHttpClient(TestWebApiStartup.BaseAddress, ODataConfig.ODataRoutePrefix); | ||
|
||
// Act | ||
var swaggerDocument = await httpClient.GetJsonAsync<SwaggerDocument>("swagger/docs/v1"); | ||
|
||
// Assert | ||
PathItem pathItem; | ||
swaggerDocument.paths.TryGetValue("/odata/Customers({Id})/Orders", out pathItem); | ||
pathItem.Should().NotBeNull(); | ||
pathItem.post.Should().NotBeNull(); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.