Skip to content

Commit

Permalink
Merge pull request #14 from rbeauchamp/feature/12
Browse files Browse the repository at this point in the history
Support OData routes that don't map to a controller
  • Loading branch information
Richard Beauchamp committed Dec 9, 2015
2 parents 2b259ff + 3030c6a commit b7b3184
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 39 deletions.
2 changes: 1 addition & 1 deletion Swashbuckle.OData.Nuget/Swashbuckle.OData.NuGet.nuproj
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
<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.2.1</Version>
<Version>2.2.2</Version>
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="..\Swashbuckle.OData\Swashbuckle.OData.csproj" />
Expand Down
11 changes: 11 additions & 0 deletions Swashbuckle.OData.Sample/App_Start/ODataConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
Expand All @@ -32,5 +36,12 @@ private static IEdmModel GetModel()
builder.EntitySet<Order>("Orders");
return builder.GetEdmModel();
}

private static IEdmModel GetFakeModel()
{
var builder = new ODataConventionModelBuilder();
builder.EntitySet<Customer>("FakeCustomers");
return builder.GetEdmModel();
}
}
}
16 changes: 16 additions & 0 deletions Swashbuckle.OData.Tests/Fixtures/ODataSwaggerProviderTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<SwaggerDocument>("swagger/docs/v1");

// Assert
swaggerDocument.Should().NotBeNull();
}
}

[Test]
public async Task It_explores_the_correct_controller()
{
Expand Down
49 changes: 12 additions & 37 deletions Swashbuckle.OData/Descriptions/ODataApiExplorer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -363,28 +348,18 @@ private static string GenerateSampleQueryParameterValue(Parameter queryParameter

private HttpControllerDescriptor GetControllerDesciptor(HttpRequestMessage request)
{
return _config().Services.GetHttpControllerSelector().SelectController(request);
}

/// <summary>
/// Selects the name of the controller to dispatch the request to.
/// </summary>
/// <param name="oDataPathRouteConstraint">The o data path route constraint.</param>
/// <param name="path">The OData path of the request.</param>
/// <param name="request">The request.</param>
/// <returns>
/// The name of the controller to dispatch to, or <c>null</c> if one cannot be resolved.
/// </returns>
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<string, HttpActionDescriptor> 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<string, HttpActionDescriptor> actionMap, params string[] targetActionNames)
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.2.1")]
[assembly: AssemblyInformationalVersion("2.2.2")]

0 comments on commit b7b3184

Please sign in to comment.