Skip to content

Commit 9b91ced

Browse files
Chris Martinezcommonsensesoftware
authored andcommitted
Do not override global settings when visiting EnableQueryAttribute. Fixes #488
1 parent cc4ceeb commit 9b91ced

File tree

4 files changed

+75
-21
lines changed

4 files changed

+75
-21
lines changed

samples/aspnetcore/SwaggerODataSample/Startup.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,10 +43,16 @@ public void ConfigureServices( IServiceCollection services )
4343

4444
// configure query options (which cannot otherwise be configured by OData conventions)
4545
options.QueryOptions.Controller<V2.PeopleController>()
46-
.Action( c => c.Get( default ) ).Allow( Skip | Count ).AllowTop( 100 );
46+
.Action( c => c.Get( default ) )
47+
.Allow( Skip | Count )
48+
.AllowTop( 100 )
49+
.AllowOrderBy( "firstName", "lastName" );
4750

4851
options.QueryOptions.Controller<V3.PeopleController>()
49-
.Action( c => c.Get( default ) ).Allow( Skip | Count ).AllowTop( 100 );
52+
.Action( c => c.Get( default ) )
53+
.Allow( Skip | Count )
54+
.AllowTop( 100 )
55+
.AllowOrderBy( "firstName", "lastName" );
5056
} );
5157
services.AddTransient<IConfigureOptions<SwaggerGenOptions>, ConfigureSwaggerOptions>();
5258
services.AddSwaggerGen(

samples/webapi/SwaggerODataWebApiSample/Startup.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,10 +74,16 @@ public void Configuration( IAppBuilder builder )
7474

7575
// configure query options (which cannot otherwise be configured by OData conventions)
7676
options.QueryOptions.Controller<V2.PeopleController>()
77-
.Action( c => c.Get( default ) ).Allow( Skip | Count ).AllowTop( 100 );
77+
.Action( c => c.Get( default ) )
78+
.Allow( Skip | Count )
79+
.AllowTop( 100 )
80+
.AllowOrderBy( "firstName", "lastName" );
7881

7982
options.QueryOptions.Controller<V3.PeopleController>()
80-
.Action( c => c.Get( default ) ).Allow( Skip | Count ).AllowTop( 100 );
83+
.Action( c => c.Get( default ) )
84+
.Allow( Skip | Count )
85+
.AllowTop( 100 )
86+
.AllowOrderBy( "firstName", "lastName" );
8187
} );
8288

8389
configuration.EnableSwagger(

src/Common.OData.ApiExplorer/AspNet.OData/Builder/ODataAttributeVisitor.cs

Lines changed: 58 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ void VisitEnableQuery( IReadOnlyList<EnableQueryAttribute> attributes )
129129

130130
if ( attribute.AllowedArithmeticOperators == AllowedArithmeticOperators.None )
131131
{
132-
attribute.AllowedArithmeticOperators = AllowedArithmeticOperators.None;
132+
context.AllowedArithmeticOperators = AllowedArithmeticOperators.None;
133133
}
134134
else
135135
{
@@ -138,7 +138,7 @@ void VisitEnableQuery( IReadOnlyList<EnableQueryAttribute> attributes )
138138

139139
if ( attribute.AllowedFunctions == AllowedFunctions.None )
140140
{
141-
attribute.AllowedFunctions = AllowedFunctions.None;
141+
context.AllowedFunctions = AllowedFunctions.None;
142142
}
143143
else
144144
{
@@ -147,7 +147,7 @@ void VisitEnableQuery( IReadOnlyList<EnableQueryAttribute> attributes )
147147

148148
if ( attribute.AllowedLogicalOperators == AllowedLogicalOperators.None )
149149
{
150-
attribute.AllowedLogicalOperators = AllowedLogicalOperators.None;
150+
context.AllowedLogicalOperators = AllowedLogicalOperators.None;
151151
}
152152
else
153153
{
@@ -156,7 +156,7 @@ void VisitEnableQuery( IReadOnlyList<EnableQueryAttribute> attributes )
156156

157157
if ( attribute.AllowedQueryOptions == AllowedQueryOptions.None )
158158
{
159-
attribute.AllowedQueryOptions = AllowedQueryOptions.None;
159+
AllowedQueryOptions = AllowedQueryOptions.None;
160160
}
161161
else
162162
{
@@ -250,7 +250,12 @@ void VisitCount( ModelBoundQuerySettings querySettings )
250250
{
251251
Contract.Requires( querySettings != null );
252252

253-
if ( querySettings.Countable == true )
253+
if ( !querySettings.Countable.HasValue )
254+
{
255+
return;
256+
}
257+
258+
if ( querySettings.Countable.Value )
254259
{
255260
AllowedQueryOptions |= Count;
256261
}
@@ -348,22 +353,59 @@ void Visit<TSetting>(
348353

349354
foreach ( var property in allowedProperties )
350355
{
351-
queryableProperties.Add( property );
356+
if ( !queryableProperties.Contains( property, comparer ) )
357+
{
358+
queryableProperties.Add( property );
359+
}
360+
}
361+
}
362+
363+
bool IsSelectEnabled( ModelBoundQuerySettings querySettings )
364+
{
365+
if ( !querySettings.DefaultSelectType.HasValue )
366+
{
367+
return AllowedQueryOptions.HasFlag( Select ) ||
368+
querySettings.SelectConfigurations.Any( p => p.Value != Disabled );
369+
}
370+
371+
return querySettings.DefaultSelectType.Value != Disabled ||
372+
querySettings.SelectConfigurations.Any( p => p.Value != Disabled );
373+
}
374+
375+
bool IsExpandEnabled( ModelBoundQuerySettings querySettings )
376+
{
377+
if ( !querySettings.DefaultExpandType.HasValue )
378+
{
379+
return AllowedQueryOptions.HasFlag( Expand ) ||
380+
querySettings.ExpandConfigurations.Any( p => p.Value.ExpandType != Disabled );
352381
}
382+
383+
return querySettings.DefaultExpandType.Value != Disabled ||
384+
querySettings.ExpandConfigurations.Any( p => p.Value.ExpandType != Disabled );
353385
}
354386

355-
static bool IsSelectEnabled( ModelBoundQuerySettings querySettings ) =>
356-
( querySettings.DefaultSelectType != null && querySettings.DefaultSelectType.Value != Disabled ) ||
357-
querySettings.SelectConfigurations.Any( p => p.Value != Disabled );
387+
bool IsFilterEnabled( ModelBoundQuerySettings querySettings )
388+
{
389+
if ( !querySettings.DefaultEnableFilter.HasValue )
390+
{
391+
return AllowedQueryOptions.HasFlag( Filter ) ||
392+
querySettings.FilterConfigurations.Any( p => p.Value );
393+
}
358394

359-
static bool IsExpandEnabled( ModelBoundQuerySettings querySettings ) =>
360-
( querySettings.DefaultExpandType != null && querySettings.DefaultExpandType.Value != Disabled ) ||
361-
querySettings.ExpandConfigurations.Any( p => p.Value.ExpandType != Disabled );
395+
return querySettings.DefaultEnableFilter.Value ||
396+
querySettings.FilterConfigurations.Any( p => p.Value );
397+
}
362398

363-
static bool IsFilterEnabled( ModelBoundQuerySettings querySettings ) =>
364-
querySettings.DefaultEnableFilter == true || querySettings.FilterConfigurations.Any( p => p.Value );
399+
bool IsOrderByEnabled( ModelBoundQuerySettings querySettings )
400+
{
401+
if ( !querySettings.DefaultEnableOrderBy.HasValue )
402+
{
403+
return AllowedQueryOptions.HasFlag( OrderBy ) ||
404+
querySettings.OrderByConfigurations.Any( p => p.Value );
405+
}
365406

366-
static bool IsOrderByEnabled( ModelBoundQuerySettings querySettings ) =>
367-
querySettings.DefaultEnableOrderBy == true || querySettings.OrderByConfigurations.Any( p => p.Value );
407+
return querySettings.DefaultEnableOrderBy.Value ||
408+
querySettings.OrderByConfigurations.Any( p => p.Value );
409+
}
368410
}
369411
}

src/Common.OData.ApiExplorer/AspNet.OData/Builder/ODataQueryOptionDescriptionContext.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ protected internal ODataQueryOptionDescriptionContext( ODataValidationSettings v
3939
}
4040

4141
/// <summary>
42-
/// Gets or sets the allowed arthmetic operators.
42+
/// Gets or sets the allowed arithmetic operators.
4343
/// </summary>
4444
/// <value>One or more of the <see cref="AllowedArithmeticOperators"/> values.</value>
4545
public AllowedArithmeticOperators AllowedArithmeticOperators { get; set; }

0 commit comments

Comments
 (0)