diff options
author | youssefm <youssefm@microsoft.com> | 2012-10-13 00:47:28 +0400 |
---|---|---|
committer | youssefm <youssefm@microsoft.com> | 2012-10-13 00:47:28 +0400 |
commit | fc327be7e55cb283c8623caab5dabc85b74c35c7 (patch) | |
tree | f161ee4bc0af29b1d4613678718582ff24a62704 | |
parent | 04cef5495cf93ae8334ccad3f3a0d9d89a5eeeed (diff) |
Addressing more code review feedback for global query support
4 files changed, 39 insertions, 24 deletions
diff --git a/src/System.Web.Http.OData/Properties/SRResources.Designer.cs b/src/System.Web.Http.OData/Properties/SRResources.Designer.cs index f40dfa62..65cb751f 100644 --- a/src/System.Web.Http.OData/Properties/SRResources.Designer.cs +++ b/src/System.Web.Http.OData/Properties/SRResources.Designer.cs @@ -844,6 +844,15 @@ namespace System.Web.Http.OData.Properties { } /// <summary> + /// Looks up a localized string similar to Queries can not be applied to a response content of type '{0}'. The response content must be an ObjectContent.. + /// </summary> + internal static string QueryingRequiresObjectContent { + get { + return ResourceManager.GetString("QueryingRequiresObjectContent", resourceCulture); + } + } + + /// <summary> /// Looks up a localized string similar to Binding OData QueryNode of kind {0} is not supported by {1}.. /// </summary> internal static string QueryNodeBindingNotSupported { diff --git a/src/System.Web.Http.OData/Properties/SRResources.resx b/src/System.Web.Http.OData/Properties/SRResources.resx index 8344fa9c..af33bbd8 100644 --- a/src/System.Web.Http.OData/Properties/SRResources.resx +++ b/src/System.Web.Http.OData/Properties/SRResources.resx @@ -441,4 +441,7 @@ <data name="InvalidReturnTypeForQuerying" xml:space="preserve"> <value>The action '{0}' on controller '{1}' with return type '{2}' cannot support querying. Ensure the type of the returned content is IEnumerable, IQueryable, or a generic form of either interface.</value> </data> + <data name="QueryingRequiresObjectContent" xml:space="preserve"> + <value>Queries can not be applied to a response content of type '{0}'. The response content must be an ObjectContent.</value> + </data> </root>
\ No newline at end of file diff --git a/src/System.Web.Http.OData/QueryableAttribute.cs b/src/System.Web.Http.OData/QueryableAttribute.cs index dd804eca..c3bec8e4 100644 --- a/src/System.Web.Http.OData/QueryableAttribute.cs +++ b/src/System.Web.Http.OData/QueryableAttribute.cs @@ -121,31 +121,32 @@ namespace System.Web.Http if (response != null && response.IsSuccessStatusCode) { ObjectContent responseContent = response.Content as ObjectContent; - if (responseContent != null) + if (responseContent == null) { - ValidateReturnType(responseContent.ObjectType, actionDescriptor); + throw Error.InvalidOperation(SRResources.QueryingRequiresObjectContent, response.Content.GetType().FullName); + } + ValidateReturnType(responseContent.ObjectType, actionDescriptor); + + // Apply the query if there are any query options or if there is a result limit set + if (responseContent.Value != null && request.RequestUri != null && + (!String.IsNullOrWhiteSpace(request.RequestUri.Query) || _resultLimit.HasValue)) + { + ValidateQuery(request); - // Apply the query if there are any query options or if there is a result limit set - if (responseContent.Value != null && request.RequestUri != null && - (!String.IsNullOrWhiteSpace(request.RequestUri.Query) || _resultLimit.HasValue)) + try + { + IEnumerable query = responseContent.Value as IEnumerable; + Contract.Assert(query != null, "ValidateResponseContent should have ensured the responseContent implements IEnumerable"); + IQueryable queryResults = ExecuteQuery(query, request, configuration, actionDescriptor); + responseContent.Value = queryResults; + } + catch (ODataException e) { - ValidateQuery(request); - - try - { - IEnumerable query = responseContent.Value as IEnumerable; - Contract.Assert(query != null, "ValidateResponseContent should have ensured the responseContent implements IEnumerable"); - IQueryable queryResults = ExecuteQuery(query, request, configuration, actionDescriptor); - responseContent.Value = queryResults; - } - catch (ODataException e) - { - actionExecutedContext.Response = request.CreateErrorResponse( - HttpStatusCode.BadRequest, - SRResources.UriQueryStringInvalid, - e); - return; - } + actionExecutedContext.Response = request.CreateErrorResponse( + HttpStatusCode.BadRequest, + SRResources.UriQueryStringInvalid, + e); + return; } } } diff --git a/test/System.Web.Http.OData.Test/QueryableAttributeTests.cs b/test/System.Web.Http.OData.Test/QueryableAttributeTests.cs index c9f8150a..6b28cf5c 100644 --- a/test/System.Web.Http.OData.Test/QueryableAttributeTests.cs +++ b/test/System.Web.Http.OData.Test/QueryableAttributeTests.cs @@ -213,7 +213,7 @@ namespace System.Web.Http.OData } [Fact] - public void NonObjectContentResponse_DoesNotThrow() + public void NonObjectContentResponse_ThrowsInvalidOperationException() { // Arrange QueryableAttribute attribute = new QueryableAttribute(); @@ -229,7 +229,9 @@ namespace System.Web.Http.OData context.Response.Content = new StreamContent(new MemoryStream()); // Act & Assert - Assert.DoesNotThrow(() => attribute.OnActionExecuted(context)); + Assert.Throws<InvalidOperationException>( + () => attribute.OnActionExecuted(context), + "Queries can not be applied to a response content of type 'System.Net.Http.StreamContent'. The response content must be an ObjectContent."); } [Theory] |