Skip to content

Commit

Permalink
Merge pull request #84 from mjolka/issue_71
Browse files Browse the repository at this point in the history
Add OData route prefix to path template in AttributeRouteStrategy
  • Loading branch information
Richard Beauchamp committed Mar 29, 2016
2 parents 7a3c54b + cbcf079 commit bf2e8a5
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 1 deletion.
61 changes: 61 additions & 0 deletions Swashbuckle.OData.Tests/Fixtures/RoutePrefixTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,17 @@
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Threading.Tasks;
using System.Web.Http;
using System.Web.OData;
using System.Web.OData.Builder;
using System.Web.OData.Extensions;
using System.Web.OData.Routing;
using FluentAssertions;
using Microsoft.OData.Edm;
using Microsoft.Owin.Hosting;
using NUnit.Framework;
using Owin;
using Swashbuckle.Swagger;

namespace Swashbuckle.OData.Tests
{
Expand All @@ -32,6 +35,29 @@ public async Task It_handles_a_null_route_prefix()
}
}

[TestCase("/Pins")]
[TestCase("/Pins({key})")]
[TestCase("/Pins/Default.Archived()")]
[TestCase("/Foo()")]
public async Task It_handles_an_odata_route_prefix_attribute(string path)
{
using (WebApp.Start(HttpClientUtils.BaseAddress, appBuilder => Configuration(appBuilder, typeof(RoutePrefixedPinsController))))
{
// Arrange
var httpClient = HttpClientUtils.GetHttpClient(HttpClientUtils.BaseAddress);

// Act
var swaggerDocument = await httpClient.GetJsonAsync<SwaggerDocument>("swagger/docs/v1");

// Assert
PathItem pathItem;
swaggerDocument.paths.TryGetValue(path, out pathItem);
pathItem.Should().NotBeNull();

await ValidationUtils.ValidateSwaggerJson();
}
}

private static void Configuration(IAppBuilder appBuilder, Type targetController)
{
var config = appBuilder.GetStandardHttpConfig(targetController);
Expand All @@ -47,6 +73,10 @@ private static IEdmModel GetEdmModel()

builder.EntitySet<Pin>("Pins");

builder.Function("Foo").Returns<int>();

builder.EntityType<Pin>().Collection.Function("Archived").ReturnsCollection<Pin>();

return builder.GetEdmModel();
}
}
Expand All @@ -68,4 +98,35 @@ public IQueryable<Pin> GetPins()
return Enumerable.Empty<Pin>().AsQueryable();
}
}

[ODataRoutePrefix("Pins")]
public class RoutePrefixedPinsController : ODataController
{
[EnableQuery]
[ODataRoute]
public IQueryable<Pin> GetPins()
{
return Enumerable.Empty<Pin>().AsQueryable();
}

[EnableQuery]
[ODataRoute("({key})")]
public SingleResult<Pin> GetPin([FromODataUri] long key)
{
return SingleResult.Create(Enumerable.Empty<Pin>().AsQueryable());
}

[EnableQuery]
[ODataRoute("Default.Archived")]
public IQueryable<Pin> GetArchived()
{
return Enumerable.Empty<Pin>().AsQueryable();
}

[ODataRoute("/Foo")]
public int GetFoo()
{
return 0;
}
}
}
36 changes: 35 additions & 1 deletion Swashbuckle.OData/Descriptions/AttributeRouteStrategy.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System;
using System.Collections.Generic;
using System.Diagnostics.Contracts;
using System.Linq;
Expand Down Expand Up @@ -49,13 +50,46 @@ private static ODataActionDescriptor GetODataActionDescriptorFromAttributeRoute(
Contract.Requires(oDataRoute != null);
Contract.Ensures(Contract.Result<ODataActionDescriptor>() != null);

var odataRoutePrefixAttribute = actionDescriptor.ControllerDescriptor.GetCustomAttributes<ODataRoutePrefixAttribute>()?.FirstOrDefault();
var odataRouteAttribute = actionDescriptor.GetCustomAttributes<ODataRouteAttribute>()?.FirstOrDefault();

Contract.Assume(odataRouteAttribute != null);
var pathTemplate = HttpUtility.UrlDecode(oDataRoute.GetRoutePrefix().AppendPathSegment(odataRouteAttribute.PathTemplate));
var pathTemplate = HttpUtility.UrlDecode(oDataRoute.GetRoutePrefix().AppendPathSegment(GetODataPathTemplate(odataRoutePrefixAttribute?.Prefix, odataRouteAttribute.PathTemplate)));
Contract.Assume(pathTemplate != null);

return new ODataActionDescriptor(actionDescriptor, oDataRoute, pathTemplate, CreateHttpRequestMessage(actionDescriptor, oDataRoute, httpConfig));
}

private static string GetODataPathTemplate(string prefix, string pathTemplate)
{
if (pathTemplate.StartsWith("/", StringComparison.Ordinal))
{
return pathTemplate.Substring(1);
}

if (string.IsNullOrEmpty(prefix))
{
return pathTemplate;
}

if (prefix.StartsWith("/", StringComparison.Ordinal))
{
prefix = prefix.Substring(1);
}

if (string.IsNullOrEmpty(pathTemplate))
{
return prefix;
}

if (pathTemplate.StartsWith("(", StringComparison.Ordinal))
{
return prefix + pathTemplate;
}

return prefix + "/" + pathTemplate;
}

private static HttpRequestMessage CreateHttpRequestMessage(HttpActionDescriptor actionDescriptor, ODataRoute oDataRoute, HttpConfiguration httpConfig)
{
Contract.Requires(httpConfig != null);
Expand Down

0 comments on commit bf2e8a5

Please sign in to comment.