Skip to content

Commit caf1090

Browse files
committed
Minor
1 parent eedc7e0 commit caf1090

File tree

7 files changed

+285
-34
lines changed

7 files changed

+285
-34
lines changed

AutoMapper.AspNetCore.OData.EF6/QueryableExtensions.cs

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ public static class QueryableExtensions
2323
/// <param name="options"></param>
2424
/// <param name="handleNullPropagation"></param>
2525
/// <returns></returns>
26+
[Obsolete("\"Use ICollection<TModel> Get<TModel, TData>(this IQueryable<TData> query, IMapper mapper, ODataQueryOptions<TModel> options, QuerySettings querySettings)\"")]
2627
public static ICollection<TModel> Get<TModel, TData>(this IQueryable<TData> query, IMapper mapper, ODataQueryOptions<TModel> options, HandleNullPropagationOption handleNullPropagation = HandleNullPropagationOption.Default)
2728
where TModel : class
2829
=> Task.Run(async () => await query.GetAsync(mapper, options, handleNullPropagation)).Result;
@@ -41,20 +42,6 @@ public static ICollection<TModel> Get<TModel, TData>(this IQueryable<TData> quer
4142
where TModel : class
4243
=> Task.Run(async () => await query.GetAsync(mapper, options, querySettings)).Result;
4344

44-
/// <summary>
45-
/// GetAsync
46-
/// </summary>
47-
/// <typeparam name="TModel"></typeparam>
48-
/// <typeparam name="TData"></typeparam>
49-
/// <param name="query"></param>
50-
/// <param name="mapper"></param>
51-
/// <param name="options"></param>
52-
/// <param name="handleNullPropagation"></param>
53-
/// <returns></returns>
54-
public static async Task<ICollection<TModel>> GetAsync<TModel, TData>(this IQueryable<TData> query, IMapper mapper, ODataQueryOptions<TModel> options, HandleNullPropagationOption handleNullPropagation = HandleNullPropagationOption.Default)
55-
where TModel : class
56-
=> await query.GetAsync(mapper, options, new QuerySettings { ODataSettings = new ODataSettings { HandleNullPropagation = handleNullPropagation } });
57-
5845
/// <summary>
5946
/// GetAsync
6047
/// </summary>
@@ -85,6 +72,21 @@ public static async Task<ICollection<TModel>> GetAsync<TModel, TData>(this IQuer
8572
return collection;
8673
}
8774

75+
/// <summary>
76+
/// GetAsync
77+
/// </summary>
78+
/// <typeparam name="TModel"></typeparam>
79+
/// <typeparam name="TData"></typeparam>
80+
/// <param name="query"></param>
81+
/// <param name="mapper"></param>
82+
/// <param name="options"></param>
83+
/// <param name="handleNullPropagation"></param>
84+
/// <returns></returns>
85+
[Obsolete("\"Use Task<ICollection<TModel>> GetAsync<TModel, TData>(this IQueryable<TData> query, IMapper mapper, ODataQueryOptions<TModel> options, QuerySettings querySettings = null)\"")]
86+
public static async Task<ICollection<TModel>> GetAsync<TModel, TData>(this IQueryable<TData> query, IMapper mapper, ODataQueryOptions<TModel> options, HandleNullPropagationOption handleNullPropagation = HandleNullPropagationOption.Default)
87+
where TModel : class
88+
=> await query.GetAsync(mapper, options, new QuerySettings { ODataSettings = new ODataSettings { HandleNullPropagation = handleNullPropagation } });
89+
8890
/// <summary>
8991
/// GetQueryAsync
9092
/// </summary>
@@ -95,6 +97,7 @@ public static async Task<ICollection<TModel>> GetAsync<TModel, TData>(this IQuer
9597
/// <param name="options"></param>
9698
/// <param name="handleNullPropagation"></param>
9799
/// <returns></returns>
100+
[Obsolete("\"Use Task<IQueryable<TModel>> GetQueryAsync<TModel, TData>(this IQueryable<TData> query, IMapper mapper, ODataQueryOptions<TModel> options, QuerySettings querySettings = null)\"")]
98101
public static async Task<IQueryable<TModel>> GetQueryAsync<TModel, TData>(this IQueryable<TData> query, IMapper mapper, ODataQueryOptions<TModel> options, HandleNullPropagationOption handleNullPropagation = HandleNullPropagationOption.Default)
99102
where TModel : class
100103
=> await query.GetQueryAsync(mapper, options, new QuerySettings { ODataSettings = new ODataSettings { HandleNullPropagation = handleNullPropagation } });

AutoMapper.AspNetCore.OData.EFCore/LinqExtensions.cs

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,11 @@ private static Expression GetLongCountMethod(ParameterExpression param, Expressi
5757
/// </summary>
5858
/// <typeparam name="T"></typeparam>
5959
/// <param name="options"></param>
60+
/// <param name="oDataSettings"></param>
6061
/// <returns></returns>
6162
public static Expression<Func<IQueryable<T>, IQueryable<T>>> GetQueryableExpression<T>(this ODataQueryOptions<T> options, ODataSettings oDataSettings = null)
6263
{
63-
if (options.OrderBy == null && options.Top == null && oDataSettings?.PageSize == null)
64+
if (NoQueryableMethod(options, oDataSettings))
6465
return null;
6566

6667
ParameterExpression param = Expression.Parameter(typeof(IQueryable<T>), "q");
@@ -71,32 +72,42 @@ public static Expression<Func<IQueryable<T>, IQueryable<T>>> GetQueryableExpress
7172
);
7273
}
7374

74-
[Obsolete("Use \"public static Expression GetOrderByMethod<T>(this Expression expression, ODataQueryOptions<T> options)\" instead.")]
75+
[Obsolete("Use \"Expression GetOrderByMethod<T>(this Expression expression, ODataQueryOptions<T> options, ODataSettings oDataSettings = null)\" instead.")]
7576
public static Expression GetOrderByMethod<T>(this ODataQueryOptions<T> options, Expression expression)
7677
=> expression.GetOrderByMethod<T>(options);
7778

7879
public static Expression GetOrderByMethod<T>(this Expression expression, ODataQueryOptions<T> options, ODataSettings oDataSettings = null)
7980
{
80-
if (options.OrderBy == null && options.Top == null && oDataSettings?.PageSize == null)
81+
if (NoQueryableMethod(options, oDataSettings))
8182
return null;
8283

8384
return expression.GetQueryableMethod
8485
(
8586
options.OrderBy?.OrderByClause,
8687
typeof(T),
8788
options.Skip?.Value,
88-
GetPageSize(options.Top)
89+
GetPageSize()
8990
);
9091

91-
int? GetPageSize(TopQueryOption topQueryOption)
92+
int? GetPageSize()
9293
{
93-
if (topQueryOption?.Value < oDataSettings?.PageSize)
94-
return topQueryOption.Value;
94+
if (oDataSettings?.PageSize == null && options.Top == null)
95+
return null;
9596

96-
return oDataSettings?.PageSize ?? topQueryOption?.Value;
97+
if (options.Top == null)
98+
return oDataSettings.PageSize;
99+
else if (oDataSettings?.PageSize == null)
100+
return options.Top.Value;
101+
102+
return options.Top.Value < oDataSettings.PageSize
103+
? options.Top.Value
104+
: oDataSettings.PageSize;
97105
}
98106
}
99107

108+
private static bool NoQueryableMethod(ODataQueryOptions options, ODataSettings oDataSettings)
109+
=> options.OrderBy == null && options.Top == null && oDataSettings?.PageSize == null;
110+
100111
public static Expression GetQueryableMethod(this Expression expression, OrderByClause orderByClause, Type type, int? skip, int? top)
101112
{
102113
if (orderByClause == null && !top.HasValue)

AutoMapper.AspNetCore.OData.EFCore/QueryableExtensions.cs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ namespace AutoMapper.AspNet.OData
1414
{
1515
public static class QueryableExtensions
1616
{
17+
[Obsolete("\"Use ICollection<TModel> Get<TModel, TData>(this IQueryable<TData> query, IMapper mapper, ODataQueryOptions<TModel> options, QuerySettings querySettings = null)\"")]
1718
public static ICollection<TModel> Get<TModel, TData>(this IQueryable<TData> query, IMapper mapper, ODataQueryOptions<TModel> options, HandleNullPropagationOption handleNullPropagation = HandleNullPropagationOption.False)
1819
where TModel : class
1920
=> Task.Run(async () => await query.GetAsync(mapper, options, handleNullPropagation)).Result;
@@ -22,10 +23,6 @@ public static ICollection<TModel> Get<TModel, TData>(this IQueryable<TData> quer
2223
where TModel : class
2324
=> Task.Run(async () => await query.GetAsync(mapper, options, querySettings)).Result;
2425

25-
public static async Task<ICollection<TModel>> GetAsync<TModel, TData>(this IQueryable<TData> query, IMapper mapper, ODataQueryOptions<TModel> options, HandleNullPropagationOption handleNullPropagation = HandleNullPropagationOption.False)
26-
where TModel : class
27-
=> await query.GetAsync(mapper, options, new QuerySettings { ODataSettings = new ODataSettings { HandleNullPropagation = handleNullPropagation } });
28-
2926
public static async Task<ICollection<TModel>> GetAsync<TModel, TData>(this IQueryable<TData> query, IMapper mapper, ODataQueryOptions<TModel> options, QuerySettings querySettings = null)
3027
where TModel : class
3128
{
@@ -44,6 +41,12 @@ public static async Task<ICollection<TModel>> GetAsync<TModel, TData>(this IQuer
4441
return await query.GetAsync(mapper, filter, queryableExpression, includeExpressions);
4542
}
4643

44+
[Obsolete("\"Use Task<ICollection<TModel>> GetAsync<TModel, TData>(this IQueryable<TData> query, IMapper mapper, ODataQueryOptions<TModel> options, QuerySettings querySettings = null)\"")]
45+
public static async Task<ICollection<TModel>> GetAsync<TModel, TData>(this IQueryable<TData> query, IMapper mapper, ODataQueryOptions<TModel> options, HandleNullPropagationOption handleNullPropagation = HandleNullPropagationOption.False)
46+
where TModel : class
47+
=> await query.GetAsync(mapper, options, new QuerySettings { ODataSettings = new ODataSettings { HandleNullPropagation = handleNullPropagation } });
48+
49+
[Obsolete("\"Use Task<IQueryable<TModel>> GetQueryAsync<TModel, TData>(this IQueryable<TData> query, IMapper mapper, ODataQueryOptions<TModel> options, QuerySettings querySettings = null)\"")]
4750
public static async Task<IQueryable<TModel>> GetQueryAsync<TModel, TData>(this IQueryable<TData> query, IMapper mapper, ODataQueryOptions<TModel> options, HandleNullPropagationOption handleNullPropagation = HandleNullPropagationOption.False)
4851
where TModel : class
4952
=> await query.GetQueryAsync(mapper, options, new QuerySettings { ODataSettings = new ODataSettings { HandleNullPropagation = handleNullPropagation } });

AutoMapper.OData.EF6.Tests/GetQueryTests.cs

Lines changed: 120 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -401,6 +401,108 @@ void Test(ICollection<CoreBuilding> collection)
401401
}
402402
}
403403

404+
[Fact]
405+
public async void BuildingWithoutTopAndPageSize()
406+
{
407+
Test(await Get<CoreBuilding, TBuilding>("/corebuilding"));
408+
409+
void Test(ICollection<CoreBuilding> collection)
410+
{
411+
Assert.Equal(5, collection.Count);
412+
}
413+
}
414+
415+
[Fact]
416+
public async void BuildingWithTopOnly()
417+
{
418+
Test(await Get<CoreBuilding, TBuilding>("/corebuilding?$top=3"));
419+
420+
void Test(ICollection<CoreBuilding> collection)
421+
{
422+
Assert.Equal(3, collection.Count);
423+
}
424+
}
425+
426+
[Fact]
427+
public async void BuildingWithPageSizeOnly()
428+
{
429+
Test(await Get<CoreBuilding, TBuilding>("/corebuilding", pageSize: 2));
430+
431+
void Test(ICollection<CoreBuilding> collection)
432+
{
433+
Assert.Equal(2, collection.Count);
434+
}
435+
}
436+
437+
[Fact]
438+
public async void BuildingWithTopAndSmallerPageSize()
439+
{
440+
Test(await Get<CoreBuilding, TBuilding>("/corebuilding?$top=3", pageSize: 2));
441+
442+
void Test(ICollection<CoreBuilding> collection)
443+
{
444+
Assert.Equal(2, collection.Count);
445+
}
446+
}
447+
448+
[Fact]
449+
public async void BuildingWithTopAndLargerPageSize()
450+
{
451+
Test(await Get<CoreBuilding, TBuilding>("/corebuilding?$top=3", pageSize: 4));
452+
453+
void Test(ICollection<CoreBuilding> collection)
454+
{
455+
Assert.Equal(3, collection.Count);
456+
}
457+
}
458+
459+
[Fact]
460+
public async void BuildingWithTopAndSmallerPageSizeNextLink()
461+
{
462+
int pageSize = 2;
463+
string query = "/corebuilding?$top=3";
464+
ODataQueryOptions<CoreBuilding> options = ODataHelpers.GetODataQueryOptions<CoreBuilding>
465+
(
466+
query,
467+
serviceProvider,
468+
serviceProvider.GetRequiredService<IRouteBuilder>()
469+
);
470+
471+
Test(await Get<CoreBuilding, TBuilding>(query, options, null, pageSize));
472+
473+
void Test(ICollection<CoreBuilding> collection)
474+
{
475+
Assert.Equal(2, collection.Count);
476+
477+
Uri nextPageLink = options.Request.ODataFeature().NextLink;
478+
Assert.NotNull(nextPageLink);
479+
Assert.Equal("localhost:16324/corebuilding?$top=1&$skip=2", nextPageLink.AbsoluteUri);
480+
Assert.Contains("$top=1", nextPageLink.Query);
481+
Assert.Contains("$skip=2", nextPageLink.Query);
482+
}
483+
}
484+
485+
[Fact]
486+
public async void BuildingWithTopAndLargerPageSizeNextLink()
487+
{
488+
int pageSize = 4;
489+
string query = "/corebuilding?$top=3";
490+
ODataQueryOptions<CoreBuilding> options = ODataHelpers.GetODataQueryOptions<CoreBuilding>
491+
(
492+
query,
493+
serviceProvider,
494+
serviceProvider.GetRequiredService<IRouteBuilder>()
495+
);
496+
497+
Test(await Get<CoreBuilding, TBuilding>(query, options, null, pageSize));
498+
499+
void Test(ICollection<CoreBuilding> collection)
500+
{
501+
Assert.Equal(3, collection.Count);
502+
Assert.Null(options.Request.ODataFeature().NextLink);
503+
}
504+
}
505+
404506
[Fact]
405507
public async void OpsTenantOrderByCountOfReference()
406508
{
@@ -661,13 +763,28 @@ async Task<IQueryable<TModel>> DoGet(IMapper mapper)
661763
}
662764
}
663765

664-
private async Task<ICollection<TModel>> Get<TModel, TData>(string query, ODataQueryOptions<TModel> options = null, QuerySettings querySettings = null) where TModel : class where TData : class
665-
=> await Get
766+
private async Task<ICollection<TModel>> Get<TModel, TData>(string query, ODataQueryOptions<TModel> options = null, QuerySettings querySettings = null, int? pageSize = null) where TModel : class where TData : class
767+
{
768+
return await Get
666769
(
667770
query,
668771
serviceProvider.GetRequiredService<TestDbContext>().Set<TData>(),
669772
options,
670-
querySettings
773+
GetQuerySettings()
671774
);
775+
776+
QuerySettings GetQuerySettings()
777+
{
778+
if (querySettings == null)
779+
return new QuerySettings { ODataSettings = new ODataSettings { PageSize = pageSize } };
780+
781+
if (querySettings.ODataSettings == null)
782+
querySettings.ODataSettings = new ODataSettings { PageSize = pageSize };
783+
else
784+
querySettings.ODataSettings.PageSize = pageSize;
785+
786+
return querySettings;
787+
}
788+
}
672789
}
673790
}

AutoMapper.OData.EF6.Tests/GetTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -385,7 +385,7 @@ void Test(ICollection<CoreBuilding> collection)
385385

386386
Uri nextPageLink = options.Request.ODataFeature().NextLink;
387387
Assert.NotNull(nextPageLink);
388-
Assert.Equal(nextPageLink, options.Request.GetNextPageLink(pageSize));
388+
Assert.Equal("localhost:16324/corebuilding?$top=1&$skip=2", nextPageLink.AbsoluteUri);
389389
Assert.Contains("$top=1", nextPageLink.Query);
390390
Assert.Contains("$skip=2", nextPageLink.Query);
391391
}

0 commit comments

Comments
 (0)