Skip to content

Commit

Permalink
Fix 1571 : Custom OData Actions do not respect 204 return values
Browse files Browse the repository at this point in the history
Fixed the null reference exception happens on
response.Content.GetType().FullName
Fix for both v3 and v4.
  • Loading branch information
congysu committed May 30, 2014
1 parent 6c741f7 commit d252143
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/System.Web.Http.OData/OData/EnableQueryAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -376,7 +376,7 @@ public override void OnActionExecuted(HttpActionExecutedContext actionExecutedCo

HttpResponseMessage response = actionExecutedContext.Response;

if (response != null && response.IsSuccessStatusCode)
if (response != null && response.IsSuccessStatusCode && response.Content != null)
{
ObjectContent responseContent = response.Content as ObjectContent;
if (responseContent == null)
Expand Down
2 changes: 1 addition & 1 deletion src/System.Web.OData/OData/EnableQueryAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,7 @@ public override void OnActionExecuted(HttpActionExecutedContext actionExecutedCo

HttpResponseMessage response = actionExecutedContext.Response;

if (response != null && response.IsSuccessStatusCode)
if (response != null && response.IsSuccessStatusCode && response.Content != null)
{
ObjectContent responseContent = response.Content as ObjectContent;
if (responseContent == null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -369,6 +369,35 @@ public void NonObjectContentResponse_ThrowsArgumentException()
"Queries can not be applied to a response content of type 'System.Net.Http.StreamContent'. The response content must be an ObjectContent.");
}

[Fact]
public void NullContentResponse_DoesNotThrow()
{
// Arrange
EnableQueryAttribute attribute = new EnableQueryAttribute();
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, "http://localhost/Customer?$skip=1");
HttpConfiguration config = new HttpConfiguration();
request.SetConfiguration(config);
HttpControllerContext controllerContext = new HttpControllerContext(
config,
new HttpRouteData(new HttpRoute()),
request);
HttpControllerDescriptor controllerDescriptor = new HttpControllerDescriptor(
new HttpConfiguration(),
"CustomerHighLevel",
typeof(CustomerHighLevelController));
HttpActionDescriptor actionDescriptor = new ReflectedHttpActionDescriptor(
controllerDescriptor,
typeof(CustomerHighLevelController).GetMethod("GetIEnumerableOfCustomer"));
HttpActionContext actionContext = new HttpActionContext(controllerContext, actionDescriptor);
HttpActionExecutedContext context = new HttpActionExecutedContext(actionContext, null)
{
Response = new HttpResponseMessage(HttpStatusCode.OK) { Content = null }
};

// Act & Assert
Assert.DoesNotThrow(() => attribute.OnActionExecuted(context));
}

[Theory]
[InlineData("$top=1")]
[InlineData("$skip=1")]
Expand Down
29 changes: 29 additions & 0 deletions test/System.Web.OData.Test/OData/Query/EnableQueryAttributeTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,35 @@ public void NonObjectContentResponse_ThrowsArgumentException()
"Queries can not be applied to a response content of type 'System.Net.Http.StreamContent'. The response content must be an ObjectContent.");
}

[Fact]
public void NullContentResponse_DoesNotThrow()
{
// Arrange
EnableQueryAttribute attribute = new EnableQueryAttribute();
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, "http://localhost/Customer?$skip=1");
HttpConfiguration config = new HttpConfiguration();
request.SetConfiguration(config);
HttpControllerContext controllerContext = new HttpControllerContext(
config,
new HttpRouteData(new HttpRoute()),
request);
HttpControllerDescriptor controllerDescriptor = new HttpControllerDescriptor(
new HttpConfiguration(),
"CustomerHighLevel",
typeof(CustomerHighLevelController));
HttpActionDescriptor actionDescriptor = new ReflectedHttpActionDescriptor(
controllerDescriptor,
typeof(CustomerHighLevelController).GetMethod("GetIEnumerableOfCustomer"));
HttpActionContext actionContext = new HttpActionContext(controllerContext, actionDescriptor);
HttpActionExecutedContext context = new HttpActionExecutedContext(actionContext, null)
{
Response = new HttpResponseMessage(HttpStatusCode.OK) { Content = null }
};

// Act & Assert
Assert.DoesNotThrow(() => attribute.OnActionExecuted(context));
}

[Theory]
[InlineData("$top=1")]
[InlineData("$skip=1")]
Expand Down

0 comments on commit d252143

Please sign in to comment.