-
Notifications
You must be signed in to change notification settings - Fork 164
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Adds dependency injection support in non-model scenario #439
Open
kakone
wants to merge
9
commits into
OData:release-8.x
Choose a base branch
from
kakone:NonModelDependencyInjection
base: release-8.x
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
f7a6048
Adds dependency injection support in non-model scenario
c8013bd
Fixes failed tests
4a09031
Merge branch 'OData:main' into NonModelDependencyInjection
kakone f92320a
Renames EnableDependencyInjection method to ConfigureServiceCollection
b6ea3c6
Reorder using statements
7ede7fa
Updates PublicApi bsl files
e305e35
Adds a using in EnableConfigureServiceCollectionTest method
d8a675a
Merge from upstream
9c121bc
Applies PR remarks
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -271,12 +271,6 @@ public static IServiceProvider GetRouteServices(this HttpRequest request) | |
return requestContainer; | ||
} | ||
|
||
// if the prefixName == null, it's a non-model scenario | ||
if (request.ODataFeature().RoutePrefix == null) | ||
{ | ||
return null; | ||
} | ||
|
||
// HTTP routes will not have chance to call CreateRequestContainer. We have to call it. | ||
return request.CreateRouteServices(request.ODataFeature().RoutePrefix); | ||
} | ||
|
@@ -300,6 +294,12 @@ public static IServiceProvider CreateRouteServices(this HttpRequest request, str | |
} | ||
|
||
IServiceScope requestScope = request.CreateRequestScope(routePrefix); | ||
if (requestScope == null) | ||
{ | ||
// non-model scenario with dependency injection non enabled | ||
return null; | ||
} | ||
|
||
IServiceProvider requestContainer = requestScope.ServiceProvider; | ||
|
||
request.ODataFeature().RequestScope = requestScope; | ||
|
@@ -341,15 +341,17 @@ private static IServiceScope CreateRequestScope(this HttpRequest request, string | |
{ | ||
ODataOptions options = request.ODataOptions(); | ||
|
||
IServiceProvider rootContainer = options.GetRouteServices(routePrefix); | ||
IServiceScope scope = rootContainer.GetRequiredService<IServiceScopeFactory>().CreateScope(); | ||
|
||
// Bind scoping request into the OData container. | ||
if (!string.IsNullOrEmpty(routePrefix)) | ||
IServiceProvider rootContainer = options?.GetRouteServices(routePrefix); | ||
if (rootContainer == null) | ||
{ | ||
scope.ServiceProvider.GetRequiredService<HttpRequestScope>().HttpRequest = request; | ||
return null; | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. insert whiteline after {} block |
||
|
||
IServiceScope scope = rootContainer.GetRequiredService<IServiceScopeFactory>().CreateScope(); | ||
|
||
// Bind scoping request into the OData container. | ||
scope.ServiceProvider.GetRequiredService<HttpRequestScope>().HttpRequest = request; | ||
kakone marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
return scope; | ||
} | ||
|
||
|
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
2 changes: 2 additions & 0 deletions
2
test/Microsoft.AspNetCore.OData.E2E.Tests/Microsoft.AspNetCore.OData.E2E.Tests.csproj
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
31 changes: 31 additions & 0 deletions
31
test/Microsoft.AspNetCore.OData.E2E.Tests/NonEdm/CustomersController.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,31 @@ | ||
//----------------------------------------------------------------------------- | ||
// <copyright file="CustomersController.cs" company=".NET Foundation"> | ||
// Copyright (c) .NET Foundation and Contributors. All rights reserved. | ||
// See License.txt in the project root for license information. | ||
// </copyright> | ||
//------------------------------------------------------------------------------ | ||
|
||
using System.Linq; | ||
using Microsoft.AspNetCore.Mvc; | ||
using Microsoft.AspNetCore.OData.Query; | ||
|
||
namespace Microsoft.AspNetCore.OData.E2E.Tests.NonEdm | ||
{ | ||
[ApiController] | ||
[Route("api/[controller]")] | ||
public class CustomersController : ControllerBase | ||
{ | ||
[HttpGet] | ||
public IActionResult Get(ODataQueryOptions<Customer> options) | ||
{ | ||
return Ok(options.ApplyTo(NonEdmDbContext.GetCustomers().AsQueryable())); | ||
} | ||
|
||
[HttpGet("WithEnableQueryAttribute")] | ||
[EnableQuery] | ||
public IActionResult GetWithEnableQueryAttribute() | ||
{ | ||
return Ok(NonEdmDbContext.GetCustomers()); | ||
} | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
test/Microsoft.AspNetCore.OData.E2E.Tests/NonEdm/NonEdmDataModel.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,22 @@ | ||
//----------------------------------------------------------------------------- | ||
// <copyright file="NonEdmDataModel.cs" company=".NET Foundation"> | ||
// Copyright (c) .NET Foundation and Contributors. All rights reserved. | ||
// See License.txt in the project root for license information. | ||
// </copyright> | ||
//------------------------------------------------------------------------------ | ||
|
||
namespace Microsoft.AspNetCore.OData.E2E.Tests.NonEdm | ||
{ | ||
public class Customer | ||
{ | ||
public int Id { get; set; } | ||
public string Name { get; set; } | ||
public Gender Gender { get; set; } | ||
} | ||
|
||
public enum Gender | ||
{ | ||
Male = 1, | ||
Female = 2 | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
test/Microsoft.AspNetCore.OData.E2E.Tests/NonEdm/NonEdmDbContext.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,38 @@ | ||
//----------------------------------------------------------------------------- | ||
// <copyright file="NonEdmDbContext.cs" company=".NET Foundation"> | ||
// Copyright (c) .NET Foundation and Contributors. All rights reserved. | ||
// See License.txt in the project root for license information. | ||
// </copyright> | ||
//------------------------------------------------------------------------------ | ||
|
||
using System.Collections.Generic; | ||
using System.Linq; | ||
|
||
namespace Microsoft.AspNetCore.OData.E2E.Tests.NonEdm | ||
{ | ||
public class NonEdmDbContext | ||
{ | ||
private static IList<Customer> _customers; | ||
|
||
public static IList<Customer> GetCustomers() | ||
{ | ||
if (_customers == null) | ||
{ | ||
Generate(); | ||
} | ||
return _customers; | ||
} | ||
|
||
private static void Generate() | ||
{ | ||
_customers = Enumerable.Range(1, 10).Select(e => | ||
new Customer | ||
{ | ||
Id = e, | ||
Name = "Customer #" + e, | ||
Gender = e%2 == 0 ? Gender.Female : Gender.Male, | ||
}).ToList(); | ||
} | ||
} | ||
|
||
} |
73 changes: 73 additions & 0 deletions
73
test/Microsoft.AspNetCore.OData.E2E.Tests/NonEdm/NonEdmTests.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,73 @@ | ||
//----------------------------------------------------------------------------- | ||
// <copyright file="NonEdmTests.cs" company=".NET Foundation"> | ||
// Copyright (c) .NET Foundation and Contributors. All rights reserved. | ||
// See License.txt in the project root for license information. | ||
// </copyright> | ||
//------------------------------------------------------------------------------ | ||
|
||
using System.Linq; | ||
using System.Net.Http; | ||
using System.Threading.Tasks; | ||
using Microsoft.AspNetCore.OData.E2E.Tests.Extensions; | ||
using Microsoft.AspNetCore.OData.TestCommon; | ||
using Microsoft.Extensions.DependencyInjection; | ||
using Microsoft.OData.UriParser; | ||
using Newtonsoft.Json.Linq; | ||
using Xunit; | ||
|
||
namespace Microsoft.AspNetCore.OData.E2E.Tests.NonEdm | ||
{ | ||
public class NonEdmTests : WebApiTestBase<NonEdmTests> | ||
{ | ||
public NonEdmTests(WebApiTestFixture<NonEdmTests> fixture) | ||
: base(fixture) | ||
{ | ||
} | ||
|
||
protected static void UpdateConfigureServices(IServiceCollection services) | ||
{ | ||
services.ConfigureControllers(typeof(CustomersController)); | ||
services.AddControllers().AddOData(opt => | ||
{ | ||
opt.EnableQueryFeatures(); | ||
opt.AddRouteComponents(services => | ||
{ | ||
services.AddSingleton<ODataUriResolver>(sp => new StringAsEnumResolver() { EnableCaseInsensitive = true }); | ||
}); | ||
}); | ||
} | ||
|
||
[Fact] | ||
public async Task NonEdmFilterByEnumString() | ||
{ | ||
Assert.Equal(5, (await GetResponse<Customer[]>("$filter=Gender eq 'MaLe'")).Length); | ||
} | ||
|
||
[Fact] | ||
public async Task NonEdmFilterByEnumStringWithEnableQueryAttribute() | ||
{ | ||
Assert.Equal(5, (await GetResponse<Customer[]>("$filter=Gender eq 'MaLe'", "WithEnableQueryAttribute")).Length); | ||
} | ||
|
||
[Fact] | ||
public async Task NonEdmSumFilteredByEnumString() | ||
{ | ||
var response = await GetResponse<JArray>("$apply=filter(Gender eq 'female')/aggregate(Id with sum as Sum)"); | ||
Assert.Equal(30, response.Single()["Sum"].Value<int>()); | ||
} | ||
|
||
[Fact] | ||
public async Task NonEdmSelectTopFilteredByEnumString() | ||
{ | ||
var response = await GetResponse<Customer[]>("$filter(Gender eq 'female')&$orderby=Id desc&$select=Name&$top=1&$skip=1"); | ||
Assert.Equal("Customer #9", response.Single().Name); | ||
} | ||
|
||
private async Task<T> GetResponse<T>(string queryOptions, string method = null) | ||
{ | ||
using var response = await CreateClient().SendAsync( | ||
new HttpRequestMessage(HttpMethod.Get, $"api/Customers/{method ?? string.Empty}?{queryOptions}")); | ||
return await response.Content.ReadAsObject<T>(); | ||
} | ||
} | ||
} |
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
insert whiteline