Skip to content

Commit

Permalink
Merge PR #801 and Fix SingleResult 500 error
Browse files Browse the repository at this point in the history
  • Loading branch information
VikingsFan committed Sep 12, 2016
1 parent 5a14de6 commit 49f06d1
Show file tree
Hide file tree
Showing 11 changed files with 177 additions and 9 deletions.
7 changes: 6 additions & 1 deletion OData/src/System.Web.OData/OData/EnableQueryAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -413,7 +413,7 @@ responseContent.Value is SingleResult ||
try
{
object queryResult = ExecuteQuery(responseContent.Value, request, actionDescriptor, queryContext);
if (queryResult == null && request.ODataProperties().Path == null)
if (queryResult == null && IsNonODataPathOrSingleResult(request, responseContent.Value))
{
// This is the case in which a regular OData service uses the EnableQuery attribute.
// For OData services ODataNullValueMessageHandler should be plugged in for the service
Expand Down Expand Up @@ -801,5 +801,10 @@ private bool ContainsAutoSelectExpandProperty(object response, HttpRequestMessag

return false;
}

private static bool IsNonODataPathOrSingleResult(HttpRequestMessage request, object value)
{
return request.ODataProperties().Path == null || value is SingleResult;
}
}
}
10 changes: 8 additions & 2 deletions OData/src/System.Web.OData/OData/Query/SkipQueryOption.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using System.Web.Http;
using System.Web.OData.Properties;
using System.Web.OData.Query.Validators;
using Microsoft.OData;
using Microsoft.OData.Edm;
using Microsoft.OData.UriParser;

Expand Down Expand Up @@ -93,10 +94,15 @@ public int Value
if (_value == null)
{
long? skipValue = _queryOptionParser.ParseSkip();

if (skipValue.HasValue)
if (skipValue.HasValue && skipValue > Int32.MaxValue)
{
Contract.Assert(skipValue.Value <= Int32.MaxValue);
throw new ODataException(Error.Format(
SRResources.SkipTopLimitExceeded,
Int32.MaxValue,
AllowedQueryOptions.Skip,
RawValue));
}

_value = (int?)skipValue;
Expand Down
9 changes: 7 additions & 2 deletions OData/src/System.Web.OData/OData/Query/TopQueryOption.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@

using System.Collections.Generic;
using System.Diagnostics.Contracts;
using System.Globalization;
using System.Linq;
using System.Web.Http;
using System.Web.OData.Properties;
using System.Web.OData.Query.Validators;
using Microsoft.OData;
using Microsoft.OData.Edm;
using Microsoft.OData.UriParser;

Expand Down Expand Up @@ -95,9 +95,14 @@ public int Value
{
long? topValue = _queryOptionParser.ParseTop();

if (topValue.HasValue)
if (topValue.HasValue && topValue > Int32.MaxValue)
{
Contract.Assert(topValue.Value <= Int32.MaxValue);
throw new ODataException(Error.Format(
SRResources.SkipTopLimitExceeded,
Int32.MaxValue,
AllowedQueryOptions.Skip,
RawValue));
}

_value = (int?)topValue;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
using System.Collections.Generic;
using System.Linq;
using System.Web.Http;
using System.Web.OData;

namespace WebStack.QA.Test.OData.SingleResultTest
{
public class CustomersController : ODataController
{
private readonly SingleResultContext _db = new SingleResultContext();

[EnableQuery]
public SingleResult<Customer> Get(int key)
{
ResetDataSource();
var db = new SingleResultContext();
return SingleResult.Create(db.Customers.Where(c => c.Id == key));
}

public void Generate()
{
for (int i = 1; i < 10; i++)
{
var customer = new Customer
{
Id = i,
Orders = new List<Order>
{
new Order
{
Id = i,
}
}
};

_db.Customers.Add(customer);
}

_db.SaveChanges();
}

private void ResetDataSource()
{
if (_db.Database.Exists())
{
_db.Database.Delete();
_db.Database.Create();
}

Generate();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
using System.Collections.Generic;
using System.Data.Entity;

namespace WebStack.QA.Test.OData.SingleResultTest
{
public class SingleResultContext : DbContext
{
public static string ConnectionString =
@"Data Source=(LocalDb)\v11.0;Integrated Security=True;Initial Catalog=SingleResultTest";

public SingleResultContext()
: base(ConnectionString)
{
}

public DbSet<Customer> Customers { get; set; }
}

public class Customer
{
public int Id { get; set; }

public List<Order> Orders { get; set; }
}

public class Order
{
public int Id { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using System.Web.Http;
using System.Web.OData.Builder;
using Microsoft.OData.Edm;

namespace WebStack.QA.Test.OData.SingleResultTest
{
public class SingleResultEdmModel
{
public static IEdmModel GetEdmModel(HttpConfiguration configuration)
{
var builder = new ODataConventionModelBuilder(configuration);
builder.EntitySet<Customer>("Customers");
builder.EntitySet<Order>("Orders");
return builder.GetEdmModel();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Web.Http;
using System.Web.Http.Dispatcher;
using System.Web.OData.Extensions;
using Newtonsoft.Json.Linq;
using Nuwa;
using WebStack.QA.Test.OData.Common;
using Xunit;

namespace WebStack.QA.Test.OData.SingleResultTest
{
public class SingleResultTests : ODataTestBase
{
private const string BaseUrl = "{0}/singleresult/Customers";

[NuwaConfiguration]
public static void UpdateConfiguration(HttpConfiguration configuration)
{
configuration.Services.Replace(
typeof(IAssembliesResolver),
new TestAssemblyResolver(typeof (CustomersController)));
configuration.IncludeErrorDetailPolicy = IncludeErrorDetailPolicy.Always;
configuration.Formatters.JsonFormatter.SerializerSettings.ReferenceLoopHandling =
Newtonsoft.Json.ReferenceLoopHandling.Ignore;
configuration.Count().Filter().OrderBy().Expand().MaxTop(null);
configuration.MapODataServiceRoute("singleresult", "singleresult",
SingleResultEdmModel.GetEdmModel(configuration));
}

[Fact]
public void EmptySingleResultReturnsNotFound()
{
// Arrange
string queryUrl = string.Format(BaseUrl + "(100)", BaseAddress);
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, queryUrl);
request.Headers.Accept.Add(MediaTypeWithQualityHeaderValue.Parse("application/json;odata.metadata=none"));
HttpClient client = new HttpClient();

// Act
HttpResponseMessage response = client.SendAsync(request).Result;

// Assert
Assert.Equal(HttpStatusCode.NotFound, response.StatusCode);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,10 @@
<Compile Include="Routing\DynamicProperties\DynamicPropertiesController.cs" />
<Compile Include="Routing\DynamicProperties\DynamicPropertiesModel.cs" />
<Compile Include="Routing\DynamicProperties\DynamicPropertiesTest.cs" />
<Compile Include="SingleResult\SingleResultController.cs" />
<Compile Include="SingleResult\SingleResultDataModel.cs" />
<Compile Include="SingleResult\SingleResultEdmModel.cs" />
<Compile Include="SingleResult\SingleResultTests.cs" />
<Compile Include="Swagger\SwaggerController.cs" />
<Compile Include="Swagger\SwaggerMetadataTest.cs" />
<Compile Include="Swagger\SwaggerPathHandler.cs" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -995,7 +995,7 @@ public void OnActionExecuted_SingleResult_WithEmptyQueryResult_SetsNotFoundRespo
}

[Fact]
public void OnActionExecuted_SingleResult_WithEmptyQueryResult_SetsContentNullIfODataPathIsPresent()
public void OnActionExecuted_SingleResult_WithEmptyQueryResult_SetsNotFound()
{
// Arrange
var customers = Enumerable.Empty<Customer>().AsQueryable();
Expand All @@ -1011,9 +1011,7 @@ public void OnActionExecuted_SingleResult_WithEmptyQueryResult_SetsContentNullIf
attribute.OnActionExecuted(actionExecutedContext);

// Assert
ObjectContent content = actionExecutedContext.Response.Content as ObjectContent;
Assert.NotNull(content);
Assert.Null(content.Value);
Assert.Equal(HttpStatusCode.NotFound, actionExecutedContext.Response.StatusCode);
}

[Fact]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ public void Value_Returns_ParsedSkipValue(string skipValue, int expectedValue)
[InlineData("''")]
[InlineData(" ")]
[InlineData("-1")]
[InlineData("6926906880")]
public void Value_ThrowsODataException_ForInvalidValues(string skipValue)
{
var model = new ODataModelBuilder().Add_Customer_EntityType().Add_Customers_EntitySet().GetEdmModel();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ public void CanConstructValidFilterQuery(string topValue)
[InlineData("''")]
[InlineData(" ")]
[InlineData("-1")]
[InlineData("6926906880")]
public void ApplyInvalidSkipQueryThrows(string topValue)
{
var model = new ODataModelBuilder().Add_Customer_EntityType().Add_Customers_EntitySet().GetEdmModel();
Expand Down

0 comments on commit 49f06d1

Please sign in to comment.