Skip to content

Commit

Permalink
support ActionResult<IAsyncEnumerable<T>> (OData#1082)
Browse files Browse the repository at this point in the history
* support ActionResult<IAsyncEnumerable<Customer>>

* add tests

* update based on review comments

* update based on review comments
  • Loading branch information
ElizabethOkerio authored Nov 8, 2023
1 parent fb8c212 commit 7980e55
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 0 deletions.
14 changes: 14 additions & 0 deletions src/Microsoft.AspNetCore.OData/Query/EnableQueryAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,20 @@ public override void OnActionExecuted(ActionExecutedContext actionExecutedContex
if (statusCodeResult?.StatusCode == null || IsSuccessStatusCode(statusCodeResult.StatusCode.Value))
{
ObjectResult responseContent = actionExecutedContext.Result as ObjectResult;

ControllerActionDescriptor controllerActionDescriptor = actionDescriptor as ControllerActionDescriptor;
Type returnType = controllerActionDescriptor.MethodInfo.ReturnType;

if (returnType.IsGenericType && returnType.GetGenericTypeDefinition() == typeof(ActionResult<>))
{
returnType = returnType.GetGenericArguments().First();

if (returnType.IsGenericType && returnType.GetGenericTypeDefinition() == typeof(IAsyncEnumerable<>))
{
responseContent.DeclaredType = returnType;
}
}

if (responseContent != null)
{
// Get collection from SingleResult.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,13 @@ public IAsyncEnumerable<Customer> Get()
return _context.Customers.AsAsyncEnumerable();
}

[EnableQuery]
[HttpGet("v2/Customers")]
public ActionResult<IAsyncEnumerable<Customer>> CustomersDataNew()
{
return Ok(_context.Customers.AsAsyncEnumerable());
}

public async IAsyncEnumerable<Customer> CreateCollectionAsync<T>()
{
await Task.Delay(5);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ public override void ConfigureServices(IServiceCollection services)

services.AddControllers().AddOData(opt => opt.Count().Filter().Expand().Select().OrderBy().SetMaxTop(null)
.AddRouteComponents("v1", edmModel));

services.AddControllers().AddOData(opt => opt.Count().Filter().Expand().Select().OrderBy().SetMaxTop(null)
.AddRouteComponents("v2", edmModel));
}
}

Expand Down Expand Up @@ -84,5 +87,27 @@ public async Task UsingAsAsyncEnumerableWorksWithoutEFCore()
List<Customer> customers = JToken.Parse(await response.Content.ReadAsStringAsync())["value"].ToObject<List<Customer>>();
Assert.Equal(2, customers.Count);
}

[Fact]
public async Task UsingAsAsyncEnumerableWithActionResultWorks()
{
// Arrange
string queryUrl = "v2/Customers?$expand=Orders";
var expectedResult = "{\"@odata.context\":\"http://localhost/v2/$metadata#Customers(Orders())\",\"value\":[{\"Id\":1,\"Name\":\"Customer0\",\"Address\":{\"Name\":\"City1\",\"Street\":\"Street1\"},\"Orders\":[{\"Id\":1,\"Name\":\"Order2\",\"Price\":25},{\"Id\":2,\"Name\":\"Order21\",\"Price\":75}]},{\"Id\":2,\"Name\":\"Customer1\",\"Address\":{\"Name\":\"City0\",\"Street\":\"Street0\"},\"Orders\":[{\"Id\":3,\"Name\":\"Order4\",\"Price\":50},{\"Id\":4,\"Name\":\"Order41\",\"Price\":150}]},{\"Id\":3,\"Name\":\"Customer0\",\"Address\":{\"Name\":\"City1\",\"Street\":\"Street1\"},\"Orders\":[{\"Id\":5,\"Name\":\"Order6\",\"Price\":75},{\"Id\":6,\"Name\":\"Order61\",\"Price\":225}]}]}";
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, queryUrl);
request.Headers.Accept.Add(MediaTypeWithQualityHeaderValue.Parse("application/json"));

// Act
HttpResponseMessage response = await Client.SendAsync(request);

// Assert
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
var resultObject = await response.Content.ReadAsStringAsync();
Assert.Equal(expectedResult, resultObject);

List<Customer> customers = JToken.Parse(await response.Content.ReadAsStringAsync())["value"].ToObject<List<Customer>>();
Assert.Equal(3, customers.Count);
Assert.Equal(2, customers[0].Orders.Count);
}
}
}

0 comments on commit 7980e55

Please sign in to comment.