Skip to content

Commit

Permalink
Enable model based query setting
Browse files Browse the repository at this point in the history
  • Loading branch information
xuzhg committed Aug 16, 2021
1 parent 703c494 commit ffec083
Show file tree
Hide file tree
Showing 25 changed files with 1,108 additions and 96 deletions.
10 changes: 3 additions & 7 deletions src/Microsoft.OData.ModelBuilder/Attributes/ExpandAttribute.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.

using Microsoft.OData.ModelBuilder.Config;
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;

namespace Microsoft.OData.ModelBuilder
{
Expand All @@ -12,11 +12,8 @@ namespace Microsoft.OData.ModelBuilder
/// correlate to OData's $expand query option settings.
/// </summary>
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Class, AllowMultiple = true)]
[SuppressMessage("Microsoft.Design", "CA1019:DefineAccessorsForAttributeArguments",
Justification = "Don't want those argument to be retrievable")]
public sealed class ExpandAttribute : Attribute
{
#if false
private readonly Dictionary<string, ExpandConfiguration> _expandConfigurations = new Dictionary<string, ExpandConfiguration>();
private SelectExpandType _expandType;
private SelectExpandType? _defaultExpandType;
Expand All @@ -29,7 +26,7 @@ public sealed class ExpandAttribute : Attribute
public ExpandAttribute()
{
_defaultExpandType = SelectExpandType.Allowed;
// _defaultMaxDepth = ODataValidationSettings.DefaultMaxExpansionDepth;
_defaultMaxDepth = QueryConfiguration.DefaultMaxExpansionDepth;
}

/// <summary>
Expand All @@ -45,7 +42,7 @@ public ExpandAttribute(params string[] properties)
_expandConfigurations.Add(property, new ExpandConfiguration
{
ExpandType = SelectExpandType.Allowed,
MaxDepth = ODataValidationSettings.DefaultMaxExpansionDepth
MaxDepth = QueryConfiguration.DefaultMaxExpansionDepth
});
}
}
Expand Down Expand Up @@ -133,6 +130,5 @@ internal int? DefaultMaxDepth
_defaultMaxDepth = value;
}
}
#endif
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;

namespace Microsoft.OData.ModelBuilder
Expand All @@ -13,8 +12,6 @@ namespace Microsoft.OData.ModelBuilder
/// correlate to OData's $filter query option settings.
/// </summary>
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Property, AllowMultiple = true)]
[SuppressMessage("Microsoft.Design", "CA1019:DefineAccessorsForAttributeArguments",
Justification = "Don't want those argument to be retrievable")]
public sealed class FilterAttribute : Attribute
{
private bool? _defaultEnableFilter;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;

namespace Microsoft.OData.ModelBuilder
Expand All @@ -13,8 +12,6 @@ namespace Microsoft.OData.ModelBuilder
/// correlate to OData's $orderby query option settings.
/// </summary>
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Property, AllowMultiple = true)]
[SuppressMessage("Microsoft.Design", "CA1019:DefineAccessorsForAttributeArguments",
Justification = "Don't want those argument to be retrievable")]
public sealed class OrderByAttribute : Attribute
{
private bool? _defaultEnableOrderBy;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Linq;

namespace Microsoft.OData.ModelBuilder
Expand All @@ -13,8 +12,6 @@ namespace Microsoft.OData.ModelBuilder
/// correlate to OData's $select query option settings.
/// </summary>
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Class, AllowMultiple = true)]
[SuppressMessage("Microsoft.Design", "CA1019:DefineAccessorsForAttributeArguments",
Justification = "Don't want those argument to be retrievable")]
public sealed class SelectAttribute : Attribute
{
private readonly Dictionary<string, SelectExpandType> _selectConfigurations = new Dictionary<string, SelectExpandType>();
Expand Down
150 changes: 150 additions & 0 deletions src/Microsoft.OData.ModelBuilder/Config/QueryConfiguration.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.

using System.Collections.Generic;

namespace Microsoft.OData.ModelBuilder.Config
{
/// <summary>
/// Query configuration which contains <see cref="ModelBoundQuerySettings"/>.
/// </summary>
public class QueryConfiguration
{
internal const int DefaultMaxExpansionDepth = 2;

private ModelBoundQuerySettings _querySettings;

/// <summary>
/// Gets or sets the <see cref="ModelBoundQuerySettings"/>.
/// </summary>
public ModelBoundQuerySettings ModelBoundQuerySettings
{
get
{
return _querySettings;
}
set
{
_querySettings = value;
}
}

/// <summary>
/// Sets the Countable in <see cref="ModelBoundQuerySettings"/>.
/// </summary>
public virtual void SetCount(bool enableCount)
{
GetModelBoundQuerySettingsOrDefault().Countable = enableCount;
}

/// <summary>
/// Sets the MaxTop in <see cref="ModelBoundQuerySettings"/>.
/// </summary>
public virtual void SetMaxTop(int? maxTop)
{
GetModelBoundQuerySettingsOrDefault().MaxTop = maxTop;
}

/// <summary>
/// Sets the PageSize in <see cref="ModelBoundQuerySettings"/>.
/// </summary>
public virtual void SetPageSize(int? pageSize)
{
GetModelBoundQuerySettingsOrDefault().PageSize = pageSize;
}

/// <summary>
/// Sets the ExpandConfigurations in <see cref="ModelBoundQuerySettings"/>.
/// </summary>
public virtual void SetExpand(IEnumerable<string> properties, int? maxDepth, SelectExpandType expandType)
{
GetModelBoundQuerySettingsOrDefault();
if (properties == null)
{
ModelBoundQuerySettings.DefaultExpandType = expandType;
ModelBoundQuerySettings.DefaultMaxDepth = maxDepth ?? DefaultMaxExpansionDepth;
}
else
{
foreach (var property in properties)
{
ModelBoundQuerySettings.ExpandConfigurations[property] = new ExpandConfiguration
{
ExpandType = expandType,
MaxDepth = maxDepth ?? DefaultMaxExpansionDepth
};
}
}
}

/// <summary>
/// Sets the SelectConfigurations in <see cref="ModelBoundQuerySettings"/>.
/// </summary>
public virtual void SetSelect(IEnumerable<string> properties, SelectExpandType selectType)
{
GetModelBoundQuerySettingsOrDefault();
if (properties == null)
{
ModelBoundQuerySettings.DefaultSelectType = selectType;
}
else
{
foreach (var property in properties)
{
ModelBoundQuerySettings.SelectConfigurations[property] = selectType;
}
}
}

/// <summary>
/// Sets the OrderByConfigurations in <see cref="ModelBoundQuerySettings"/>.
/// </summary>
public virtual void SetOrderBy(IEnumerable<string> properties, bool enableOrderBy)
{
GetModelBoundQuerySettingsOrDefault();
if (properties == null)
{
ModelBoundQuerySettings.DefaultEnableOrderBy = enableOrderBy;
}
else
{
foreach (var property in properties)
{
ModelBoundQuerySettings.OrderByConfigurations[property] = enableOrderBy;
}
}
}

/// <summary>
/// Sets the FilterConfigurations in <see cref="ModelBoundQuerySettings"/>.
/// </summary>
public virtual void SetFilter(IEnumerable<string> properties, bool enableFilter)
{
GetModelBoundQuerySettingsOrDefault();
if (properties == null)
{
ModelBoundQuerySettings.DefaultEnableFilter = enableFilter;
}
else
{
foreach (var property in properties)
{
ModelBoundQuerySettings.FilterConfigurations[property] = enableFilter;
}
}
}

/// <summary>
/// Gets the <see cref="ModelBoundQuerySettings"/> or create it depends on the default settings.
/// </summary>
internal ModelBoundQuerySettings GetModelBoundQuerySettingsOrDefault()
{
if (_querySettings == null)
{
_querySettings = new ModelBoundQuerySettings(ModelBoundQuerySettings.DefaultModelBoundQuerySettings);
}

return _querySettings;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public QueryableRestrictions(PropertyConfiguration propertyConfiguration)
public bool NotFilterable { get; set; }

/// <summary>
/// Gets or sets whether the property is nonfilterable. default is false.
/// Gets or sets whether the property is non-filterable. default is false.
/// </summary>
public bool NonFilterable
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public override void Apply(PropertyConfiguration edmProperty,
{
throw Error.ArgumentNull("edmProperty");
}
/*

if (!edmProperty.AddedExplicitly)
{
CountAttribute countAttribute = attribute as CountAttribute;
Expand All @@ -33,7 +33,7 @@ public override void Apply(PropertyConfiguration edmProperty,
{
edmProperty.QueryConfiguration.GetModelBoundQuerySettingsOrDefault().Countable = true;
}
}*/
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public override void Apply(StructuralTypeConfiguration edmTypeConfiguration, ODa
if (!edmTypeConfiguration.AddedExplicitly)
{
CountAttribute countAttribute = attribute as CountAttribute;
/*

if (countAttribute.Disabled)
{
edmTypeConfiguration.QueryConfiguration.GetModelBoundQuerySettingsOrDefault().Countable = false;
Expand All @@ -43,7 +43,6 @@ public override void Apply(StructuralTypeConfiguration edmTypeConfiguration, ODa
{
edmTypeConfiguration.QueryConfiguration.GetModelBoundQuerySettingsOrDefault().Countable = true;
}
*/
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Licensed under the MIT License. See License.txt in the project root for license information.

using System;
using Microsoft.OData.ModelBuilder.Config;

namespace Microsoft.OData.ModelBuilder.Conventions.Attributes
{
Expand All @@ -25,7 +26,7 @@ public override void Apply(PropertyConfiguration edmProperty,
if (!edmProperty.AddedExplicitly)
{
ExpandAttribute expandAttribute = attribute as ExpandAttribute;
/*

ModelBoundQuerySettings querySettings = edmProperty.QueryConfiguration.GetModelBoundQuerySettingsOrDefault();
if (querySettings.ExpandConfigurations.Count == 0)
{
Expand All @@ -43,9 +44,8 @@ public override void Apply(PropertyConfiguration edmProperty,
if (expandAttribute.ExpandConfigurations.Count == 0)
{
querySettings.DefaultExpandType = expandAttribute.DefaultExpandType;
querySettings.DefaultMaxDepth = expandAttribute.DefaultMaxDepth ?? ODataValidationSettings.DefaultMaxExpansionDepth;
querySettings.DefaultMaxDepth = expandAttribute.DefaultMaxDepth ?? QueryConfiguration.DefaultMaxExpansionDepth;
}
*/
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Licensed under the MIT License. See License.txt in the project root for license information.

using System;
using Microsoft.OData.ModelBuilder.Config;

namespace Microsoft.OData.ModelBuilder.Conventions.Attributes
{
Expand Down Expand Up @@ -34,13 +35,12 @@ public override void Apply(StructuralTypeConfiguration edmTypeConfiguration, ODa
if (!edmTypeConfiguration.AddedExplicitly)
{
ExpandAttribute expandAttribute = attribute as ExpandAttribute;
/*

ModelBoundQuerySettings querySettings =
edmTypeConfiguration.QueryConfiguration.GetModelBoundQuerySettingsOrDefault();
if (querySettings.ExpandConfigurations.Count == 0)
{
querySettings.CopyExpandConfigurations(
expandAttribute.ExpandConfigurations);
querySettings.CopyExpandConfigurations(expandAttribute.ExpandConfigurations);
}
else
{
Expand All @@ -54,9 +54,8 @@ public override void Apply(StructuralTypeConfiguration edmTypeConfiguration, ODa
if (expandAttribute.ExpandConfigurations.Count == 0)
{
querySettings.DefaultExpandType = expandAttribute.DefaultExpandType;
querySettings.DefaultMaxDepth = expandAttribute.DefaultMaxDepth ?? ODataValidationSettings.DefaultMaxExpansionDepth;
querySettings.DefaultMaxDepth = expandAttribute.DefaultMaxDepth ?? QueryConfiguration.DefaultMaxExpansionDepth;
}
*/
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
// Licensed under the MIT License. See License.txt in the project root for license information.

using System;
using Microsoft.OData.ModelBuilder.Config;

namespace Microsoft.OData.ModelBuilder.Conventions.Attributes
{
Expand All @@ -25,7 +26,7 @@ public override void Apply(PropertyConfiguration edmProperty,
if (!edmProperty.AddedExplicitly)
{
FilterAttribute filterAttribute = attribute as FilterAttribute;
/*

ModelBoundQuerySettings querySettings = edmProperty.QueryConfiguration.GetModelBoundQuerySettingsOrDefault();
if (querySettings.FilterConfigurations.Count == 0)
{
Expand All @@ -43,7 +44,7 @@ public override void Apply(PropertyConfiguration edmProperty,
if (filterAttribute.FilterConfigurations.Count == 0)
{
querySettings.DefaultEnableFilter = filterAttribute.DefaultEnableFilter;
}*/
}
}
}
}
Expand Down
Loading

0 comments on commit ffec083

Please sign in to comment.