-
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.
Don't map OData path parameters to ODataQueryOptions. Fixes #28.
- Loading branch information
Richard Beauchamp
committed
Dec 30, 2015
1 parent
a139e32
commit 62f164c
Showing
8 changed files
with
135 additions
and
5 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
110 changes: 110 additions & 0 deletions
110
Swashbuckle.OData.Tests/ODataQueryOptions/ODataQueryOptionsTests.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,110 @@ | ||
using System; | ||
using System.Collections.Concurrent; | ||
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 FluentAssertions; | ||
using Microsoft.OData.Edm; | ||
using Microsoft.Owin.Hosting; | ||
using NUnit.Framework; | ||
using Owin; | ||
using Swashbuckle.Swagger; | ||
using System.Web.OData.Query; | ||
|
||
namespace Swashbuckle.OData.Tests.ODataQueryOptions | ||
{ | ||
[TestFixture] | ||
public class ODataQueryOptionsTests | ||
{ | ||
[Test] | ||
public async Task It_supports_controller_with_single_get_method_with_odataqueryoptions() | ||
{ | ||
using (WebApp.Start(HttpClientUtils.BaseAddress, appBuilder => Configuration(appBuilder, typeof(Products3Controller)))) | ||
{ | ||
// Arrange | ||
var httpClient = HttpClientUtils.GetHttpClient(HttpClientUtils.BaseAddress); | ||
// Verify that the OData route in the test controller is valid | ||
var products = await httpClient.GetJsonAsync<ODataResponse<Product3>>("/odata/Products3"); | ||
products.Should().NotBeNull(); | ||
products.Value.Count.Should().Be(100); | ||
var product = await httpClient.GetJsonAsync<ODataResponse<Product3>>("/odata/Products3(1)"); | ||
product.Should().NotBeNull(); | ||
product.Value.Count.Should().Be(100); | ||
|
||
// Act | ||
var swaggerDocument = await httpClient.GetJsonAsync<SwaggerDocument>("swagger/docs/v1"); | ||
|
||
// Assert | ||
PathItem pathItem; | ||
swaggerDocument.paths.TryGetValue("/odata/Products3", out pathItem); | ||
pathItem.Should().NotBeNull(); | ||
pathItem.get.Should().NotBeNull(); | ||
|
||
PathItem pathItem2; | ||
swaggerDocument.paths.TryGetValue("/odata/Products3({Id})", out pathItem2); | ||
pathItem.Should().NotBeNull(); | ||
pathItem.get.Should().NotBeNull(); | ||
|
||
await ValidationUtils.ValidateSwaggerJson(); | ||
} | ||
} | ||
|
||
private static void Configuration(IAppBuilder appBuilder, Type targetController) | ||
{ | ||
var config = appBuilder.GetStandardHttpConfig(targetController); | ||
|
||
// Define a route to a controller class that contains functions | ||
config.MapODataServiceRoute("FromUriArrayRoute", "odata", GetEdmModel()); | ||
|
||
config.EnsureInitialized(); | ||
} | ||
|
||
private static IEdmModel GetEdmModel() | ||
{ | ||
ODataModelBuilder builder = new ODataConventionModelBuilder(); | ||
|
||
builder.EntitySet<Product3>("Products3"); | ||
|
||
return builder.GetEdmModel(); | ||
} | ||
} | ||
|
||
public class Product3 | ||
{ | ||
[Key] | ||
public int Id { get; set; } | ||
|
||
public string Name { get; set; } | ||
|
||
public double Price { get; set; } | ||
} | ||
|
||
public class Products3Controller : ODataController | ||
{ | ||
private static readonly ConcurrentDictionary<int, Product3> Data; | ||
|
||
static Products3Controller() | ||
{ | ||
Data = new ConcurrentDictionary<int, Product3>(); | ||
var rand = new Random(); | ||
|
||
Enumerable.Range(0, 100).Select(i => new Product3 | ||
{ | ||
Id = i, | ||
Name = "Product " + i, | ||
Price = rand.NextDouble() * 1000 | ||
}).ToList().ForEach(p => Data.TryAdd(p.Id, p)); | ||
} | ||
|
||
[EnableQuery] | ||
public Task<IHttpActionResult> Get(ODataQueryOptions<Product3> queryOptions) | ||
{ | ||
var results = (IQueryable<Product3>)queryOptions.ApplyTo(Data.Values.AsQueryable()); | ||
return Task.FromResult((IHttpActionResult)Ok(results)); | ||
} | ||
} | ||
} |
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
18 changes: 18 additions & 0 deletions
18
Swashbuckle.OData/Descriptions/HttpParameterDescriptorExtensions.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,18 @@ | ||
using System.Diagnostics.Contracts; | ||
using System.Web.Http.Controllers; | ||
|
||
namespace Swashbuckle.OData.Descriptions | ||
{ | ||
internal static class HttpParameterDescriptorExtensions | ||
{ | ||
public static bool IsODataQueryOptions(this HttpParameterDescriptor parameterDescriptor) | ||
{ | ||
Contract.Requires(parameterDescriptor != null); | ||
|
||
var parameterType = parameterDescriptor.ParameterType; | ||
Contract.Assume(parameterType != null); | ||
|
||
return parameterType.Name == "ODataQueryOptions`1"; | ||
} | ||
} | ||
} |
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