Skip to content

Commit

Permalink
Fix issue with custom routing. Closes #29.
Browse files Browse the repository at this point in the history
  • Loading branch information
Richard Beauchamp committed Dec 27, 2015
1 parent 26fc61f commit f854b82
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 8 deletions.
4 changes: 2 additions & 2 deletions Swashbuckle.OData.Nuget/Swashbuckle.OData.NuGet.nuproj
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,12 @@
<Owners>Richard Beauchamp</Owners>
<Summary>Extends Swashbuckle with OData v4 support!</Summary>
<Description>Extends Swashbuckle with OData v4 support!</Description>
<ReleaseNotes>Limits the swagger model-schema to the top-level entity</ReleaseNotes>
<ReleaseNotes>Fix issue with custom routes</ReleaseNotes>
<ProjectUrl>https://github.com/rbeauchamp/Swashbuckle.OData</ProjectUrl>
<LicenseUrl>https://github.com/rbeauchamp/Swashbuckle.OData/blob/master/License.txt</LicenseUrl>
<Copyright>Copyright 2015</Copyright>
<Tags>Swashbuckle Swagger SwaggerUi OData Documentation Discovery Help WebApi AspNet AspNetWebApi Docs WebHost IIS</Tags>
<Version>2.8.0</Version>
<Version>2.8.1</Version>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\Swashbuckle.OData\Swashbuckle.OData.csproj" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand All @@ -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);
Expand Down
5 changes: 5 additions & 0 deletions Swashbuckle.OData.Sample/ODataControllers/OrdersController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@ public async Task<IHttpActionResult> Post([FromODataUri] int customerId, Order o
return Created(order);
}

public async Task<IHttpActionResult> Delete([FromODataUri] int customerId, [FromODataUri] Guid orderID)
{
throw new NotImplementedException();
}

/// <summary>
/// Query the order by id
/// </summary>
Expand Down
26 changes: 26 additions & 0 deletions Swashbuckle.OData.Tests/Fixtures/CustomRouteTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<SwaggerDocument>("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);
Expand All @@ -54,6 +75,11 @@ private static void Configuration(IAppBuilder appBuilder, Type targetController)
.PathParameter<int>("Id")
.BodyParameter<Order>("order");

config.AddCustomSwaggerRoute(customODataRoute, "/Customers({Id})/Orders({orderID})")
.Operation(HttpMethod.Delete)
.PathParameter<int>("Id")
.PathParameter<Guid>("orderID");

config.EnsureInitialized();
}

Expand Down
3 changes: 2 additions & 1 deletion Swashbuckle.OData/Descriptions/ODataSwaggerUtilities.cs
Original file line number Diff line number Diff line change
Expand Up @@ -685,7 +685,8 @@ internal static IList<Parameter> Parameter(this IList<Parameter> parameters, str
{
name = name,
@in = kind,
description = description
description = description,
required = true
};

if (kind != "body")
Expand Down
2 changes: 1 addition & 1 deletion Swashbuckle.OData/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,4 @@

[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]
[assembly: AssemblyInformationalVersion("2.8.0")]
[assembly: AssemblyInformationalVersion("2.8.1")]

0 comments on commit f854b82

Please sign in to comment.