Skip to content

Commit 61d1584

Browse files
Refactor int? where 'null = no limit' and '> 0 = some'
1 parent 972edb7 commit 61d1584

File tree

5 files changed

+51
-27
lines changed

5 files changed

+51
-27
lines changed

src/Common/src/Common.OData.ApiExplorer/Conventions/DefaultODataQueryOptionDescriptionProvider.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -214,15 +214,15 @@ protected virtual string DescribeTop( ODataQueryOptionDescriptionContext context
214214
throw new ArgumentNullException( nameof( context ) );
215215
}
216216

217-
if ( !context.MaxTop.HasValue || context.MaxTop.Value <= 0 )
217+
if ( context.MaxTop.NoLimitOrNone() )
218218
{
219219
return ODataExpSR.TopQueryOptionDesc;
220220
}
221221

222222
return GetOrCreateBuilder()
223223
.Append( ODataExpSR.TopQueryOptionDesc )
224224
.Append( Space )
225-
.AppendFormat( CurrentCulture, ODataExpSR.MaxValueDesc, context.MaxTop.Value )
225+
.AppendFormat( CurrentCulture, ODataExpSR.MaxValueDesc, context.MaxTop )
226226
.ToString();
227227
}
228228

@@ -238,15 +238,15 @@ protected virtual string DescribeSkip( ODataQueryOptionDescriptionContext contex
238238
throw new ArgumentNullException( nameof( context ) );
239239
}
240240

241-
if ( !context.MaxSkip.HasValue || context.MaxSkip.Value <= 0 )
241+
if ( context.MaxSkip.NoLimitOrNone() )
242242
{
243243
return ODataExpSR.SkipQueryOptionDesc;
244244
}
245245

246246
return GetOrCreateBuilder()
247247
.Append( ODataExpSR.SkipQueryOptionDesc )
248248
.Append( Space )
249-
.AppendFormat( CurrentCulture, ODataExpSR.MaxValueDesc, context.MaxSkip.Value )
249+
.AppendFormat( CurrentCulture, ODataExpSR.MaxValueDesc, context.MaxSkip )
250250
.ToString();
251251
}
252252

src/Common/src/Common.OData.ApiExplorer/Conventions/ODataAttributeVisitor.cs

Lines changed: 27 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -90,51 +90,52 @@ private void VisitEnableQuery( IReadOnlyList<EnableQueryAttribute> attributes )
9090
AllowedQueryOptions = attribute.AllowedQueryOptions;
9191

9292
if ( attribute.MaxAnyAllExpressionDepth != @default.MaxAnyAllExpressionDepth )
93-
{
94-
if ( context.MaxAnyAllExpressionDepth == @default.MaxAnyAllExpressionDepth )
9593
{
9694
context.MaxAnyAllExpressionDepth = attribute.MaxAnyAllExpressionDepth;
9795
}
9896

99-
if ( context.MaxExpansionDepth == @default.MaxExpansionDepth )
97+
if ( attribute.MaxExpansionDepth != @default.MaxExpansionDepth )
10098
{
10199
context.MaxExpansionDepth = attribute.MaxExpansionDepth;
102100
}
103101

104-
if ( context.MaxNodeCount == @default.MaxNodeCount )
102+
if ( attribute.MaxNodeCount != @default.MaxNodeCount )
105103
{
106104
context.MaxNodeCount = attribute.MaxNodeCount;
107105
}
108106

109-
if ( context.MaxOrderByNodeCount == @default.MaxOrderByNodeCount )
107+
if ( attribute.MaxOrderByNodeCount != @default.MaxOrderByNodeCount )
110108
{
111109
context.MaxOrderByNodeCount = attribute.MaxOrderByNodeCount;
112110
}
113111

114-
if ( context.MaxSkip != @default.MaxSkip )
112+
if ( attribute.MaxSkip != @default.MaxSkip )
115113
{
116114
context.MaxSkip = attribute.MaxSkip;
117115
}
118116

119-
if ( context.MaxTop != @default.MaxTop )
117+
if ( attribute.MaxTop != @default.MaxTop )
120118
{
121119
context.MaxTop = attribute.MaxTop;
122120
}
123121

124-
if ( !string.IsNullOrEmpty( attribute.AllowedOrderByProperties ) )
122+
if ( string.IsNullOrEmpty( attribute.AllowedOrderByProperties ) )
125123
{
126-
var properties = attribute.AllowedOrderByProperties.Split( new[] { ',' }, RemoveEmptyEntries );
127-
var allowedOrderByProperties = context.AllowedOrderByProperties;
128-
var comparer = StringComparer.OrdinalIgnoreCase;
124+
continue;
125+
}
129126

130-
for ( var j = 0; j < properties.Length; j++ )
131-
{
132-
var property = properties[j].Trim();
127+
var properties = attribute.AllowedOrderByProperties.Split( new[] { ',' }, RemoveEmptyEntries );
128+
var allowedOrderByProperties = context.AllowedOrderByProperties;
129+
var comparer = StringComparer.OrdinalIgnoreCase;
130+
131+
for ( var j = 0; j < properties.Length; j++ )
132+
{
133+
var property = properties[j].Trim();
133134

134-
if ( !string.IsNullOrEmpty( property ) && !allowedOrderByProperties.Contains( property, comparer ) )
135-
{
136-
allowedOrderByProperties.Add( property );
137-
}
135+
if ( !string.IsNullOrEmpty( property ) &&
136+
!allowedOrderByProperties.Contains( property, comparer ) )
137+
{
138+
allowedOrderByProperties.Add( property );
138139
}
139140
}
140141
}
@@ -218,10 +219,16 @@ private void VisitCount( ModelBoundQuerySettings querySettings )
218219

219220
private void VisitMaxTop( ModelBoundQuerySettings querySettings )
220221
{
221-
if ( querySettings.MaxTop != null && querySettings.MaxTop.Value > 0 )
222+
if ( querySettings.MaxTop.Unset() )
222223
{
223-
context.MaxTop = querySettings.MaxTop;
224+
return;
224225
}
226+
227+
context.MaxTop = querySettings.MaxTop;
228+
229+
// calling the Page() configuration sets MaxTop and PageSize,
230+
// which is implied to enable $top and $skip
231+
AllowedQueryOptions |= Skip | Top;
225232
}
226233

227234
private void Visit<TSetting>(

src/Common/src/Common.OData.ApiExplorer/Conventions/ODataValidationSettingsConvention.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ private AllowedQueryOptions GetQueryOptions( DefaultQuerySettings settings, ODat
174174
queryOptions |= Select;
175175
}
176176

177-
if ( settings.MaxTop != null && settings.MaxTop.Value > 0 )
177+
if ( settings.MaxTop.NoLimitOrSome() )
178178
{
179179
context.MaxTop = settings.MaxTop;
180180
}

src/Common/src/Common.OData.ApiExplorer/Conventions/ODataValidationSettingsExtensions.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,12 @@ internal static void CopyFrom( this ODataValidationSettings original, ODataValid
2121
original.MaxNodeCount = source.MaxNodeCount;
2222
original.MaxOrderByNodeCount = source.MaxOrderByNodeCount;
2323

24-
if ( source.MaxSkip != null )
24+
if ( source.MaxSkip.NoLimitOrNone() )
2525
{
2626
original.MaxSkip = source.MaxSkip;
2727
}
2828

29-
if ( source.MaxTop != null )
29+
if ( source.MaxTop.NoLimitOrSome() )
3030
{
3131
original.MaxTop = source.MaxTop;
3232
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Copyright (c) .NET Foundation and contributors. All rights reserved.
2+
3+
namespace System;
4+
5+
using System.Runtime.CompilerServices;
6+
7+
internal static class NullableExtensions
8+
{
9+
[MethodImpl( MethodImplOptions.AggressiveInlining )]
10+
public static bool Unset( this int? value ) => value.HasValue && value.Value == 0;
11+
12+
[MethodImpl( MethodImplOptions.AggressiveInlining )]
13+
public static bool NoLimitOrSome( this int? value ) => !value.HasValue || value.Value > 0;
14+
15+
[MethodImpl( MethodImplOptions.AggressiveInlining )]
16+
public static bool NoLimitOrNone( this int? value ) => !value.HasValue || value.Value <= 0;
17+
}

0 commit comments

Comments
 (0)