Skip to content

Commit

Permalink
Add DisableWhenSelectPresent for AutoExpand attribute #750
Browse files Browse the repository at this point in the history
  • Loading branch information
VikingsFan committed Sep 13, 2016
1 parent 49f06d1 commit 5427761
Show file tree
Hide file tree
Showing 14 changed files with 152 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,9 @@ namespace System.Web.OData.Builder
[AttributeUsage(AttributeTargets.Property | AttributeTargets.Class)]
public sealed class AutoExpandAttribute : Attribute
{
/// <summary>
/// Gets or sets whether the automatic expand will be disabled if there is a $select specify by client.
/// </summary>
public bool DisableWhenSelectPresent { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ public override void Apply(NavigationPropertyConfiguration edmProperty,

if (!edmProperty.AddedExplicitly)
{
edmProperty.AutomaticallyExpand();
AutoExpandAttribute autoExpandAttribute = attribute as AutoExpandAttribute;
edmProperty.AutomaticallyExpand(autoExpandAttribute.DisableWhenSelectPresent);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,12 @@ public override void Apply(StructuralTypeConfiguration edmTypeConfiguration, ODa
}

EntityTypeConfiguration entityTypeConfiguration = edmTypeConfiguration as EntityTypeConfiguration;
AutoExpandAttribute autoExpandAttribute = attribute as AutoExpandAttribute;
foreach (var property in entityTypeConfiguration.NavigationProperties)
{
if (!property.AddedExplicitly)
{
property.AutomaticallyExpand();
property.AutomaticallyExpand(autoExpandAttribute.DisableWhenSelectPresent);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -149,9 +149,13 @@ public NavigationPropertyConfiguration NonContained()
/// <summary>
/// Marks the navigation property is automatic expanded.
/// </summary>
public NavigationPropertyConfiguration AutomaticallyExpand()
/// <param name="disableWhenSelectIsPresent">If set to <c>true</c> then automatic expand will be disabled
/// if there is a $select specify by client.</param>
/// <returns></returns>
public NavigationPropertyConfiguration AutomaticallyExpand(bool disableWhenSelectIsPresent)
{
AutoExpand = true;
DisableAutoExpandWhenSelectIsPresent = disableWhenSelectIsPresent;
return this;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,11 @@ public bool IsRestricted
/// </summary>
public bool AutoExpand { get; set; }

/// <summary>
/// Gets or sets whether the automatic expand will be disabled if there is a $select specify by client.
/// </summary>
public bool DisableAutoExpandWhenSelectIsPresent { get; set; }

/// <summary>
/// Gets or sets whether the property is nonfilterable. default is false.
/// </summary>
Expand Down
12 changes: 7 additions & 5 deletions OData/src/System.Web.OData/OData/Formatter/EdmLibHelpers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -405,13 +405,14 @@ public static bool IsAutoSelect(IEdmProperty property, IEdmProperty pathProperty
return false;
}

public static bool IsAutoExpand(IEdmProperty navigationProperty, IEdmProperty pathProperty,
IEdmStructuredType pathStructuredType, IEdmModel edmModel, ModelBoundQuerySettings querySettings = null)
public static bool IsAutoExpand(IEdmProperty navigationProperty,
IEdmProperty pathProperty, IEdmStructuredType pathStructuredType, IEdmModel edmModel,
bool isSelectPresent = false, ModelBoundQuerySettings querySettings = null)
{
QueryableRestrictionsAnnotation annotation = GetPropertyRestrictions(navigationProperty, edmModel);
if (annotation != null && annotation.Restrictions.AutoExpand)
{
return true;
return !annotation.Restrictions.DisableAutoExpandWhenSelectIsPresent || !isSelectPresent;
}

if (querySettings == null)
Expand All @@ -429,7 +430,7 @@ public static bool IsAutoExpand(IEdmProperty navigationProperty, IEdmProperty pa

public static IEnumerable<IEdmNavigationProperty> GetAutoExpandNavigationProperties(
IEdmProperty pathProperty, IEdmStructuredType pathStructuredType, IEdmModel edmModel,
ModelBoundQuerySettings querySettings = null)
bool isSelectPresent = false, ModelBoundQuerySettings querySettings = null)
{
List<IEdmNavigationProperty> autoExpandNavigationProperties = new List<IEdmNavigationProperty>();
IEdmEntityType baseEntityType = pathStructuredType as IEdmEntityType;
Expand All @@ -449,7 +450,8 @@ public static IEnumerable<IEdmNavigationProperty> GetAutoExpandNavigationPropert
autoExpandNavigationProperties.AddRange(
navigationProperties.Where(
navigationProperty =>
IsAutoExpand(navigationProperty, pathProperty, entityType, edmModel, querySettings)));
IsAutoExpand(navigationProperty, pathProperty, entityType, edmModel,
isSelectPresent, querySettings)));
}
}
}
Expand Down
3 changes: 2 additions & 1 deletion OData/src/System.Web.OData/OData/Query/ODataQueryOptions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -686,7 +686,8 @@ private string GetAutoExpandRawValue()
IEdmEntityType baseEntityType = Context.TargetStructuredType as IEdmEntityType;
var autoExpandRawValue = String.Empty;
var autoExpandNavigationProperties = EdmLibHelpers.GetAutoExpandNavigationProperties(
Context.TargetProperty, Context.TargetStructuredType, Context.Model);
Context.TargetProperty, Context.TargetStructuredType, Context.Model,
!String.IsNullOrEmpty(RawValues.Select));

foreach (var property in autoExpandNavigationProperties)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -378,7 +378,7 @@ private void GetAutoSelectExpandItems(
}

var autoExpandNavigationProperties = EdmLibHelpers.GetAutoExpandNavigationProperties(null, baseEntityType,
model, modelBoundQuerySettings);
model, !isAllSelected, modelBoundQuerySettings);

foreach (var navigationProperty in autoExpandNavigationProperties)
{
Expand Down
6 changes: 6 additions & 0 deletions OData/src/System.Web.OData/OData/QueryableRestrictions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ public QueryableRestrictions(PropertyConfiguration propertyConfiguration)
NotNavigable = propertyConfiguration.NotNavigable;
NotExpandable = propertyConfiguration.NotExpandable;
NotCountable = propertyConfiguration.NotCountable;
DisableAutoExpandWhenSelectIsPresent = propertyConfiguration.DisableAutoExpandWhenSelectIsPresent;
_autoExpand = propertyConfiguration.AutoExpand;
}

Expand Down Expand Up @@ -85,5 +86,10 @@ public bool AutoExpand
get { return !NotExpandable && _autoExpand; }
set { _autoExpand = value; }
}

/// <summary>
/// If set to <c>true</c> then automatic expand will be disabled if there is a $select specify by client.
/// </summary>
public bool DisableAutoExpandWhenSelectIsPresent { get; set; }
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -187,16 +187,49 @@ public SingleResult<NormalOrder> Get(int key)

public void Generate()
{
var order = new DerivedOrder
var order2 = new DerivedOrder
{
Id = 2,
OrderDetail = new OrderDetail
{
Id = 3,
Description = "OrderDetail"
},
NotShownDetail = new OrderDetail
{
Id = 4,
Description = "NotShownOrderDetail4"
}
};

var order1 = new DerivedOrder
{
Id = 1,
OrderDetail = new OrderDetail
{
Id = 1,
Description = "OrderDetail"
},
NotShownDetail = new OrderDetail
{
Id = 2,
Description = "NotShownOrderDetail2"
}
};
_db.NormalOrders.Add(order);

var order3 = new DerivedOrder2
{
Id = 3,
NotShownDetail = new OrderDetail
{
Id = 5,
Description = "NotShownOrderDetail4"
}
};

order2.LinkOrder = order1;
_db.NormalOrders.Add(order2);
_db.NormalOrders.Add(order3);
_db.SaveChanges();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,23 @@ public class VipOrder : SpecialOrder
public class NormalOrder
{
public int Id { get; set; }

public NormalOrder LinkOrder { get; set; }
}

public class DerivedOrder : NormalOrder
{
[AutoExpand]
public OrderDetail OrderDetail { get; set; }

[AutoExpand(DisableWhenSelectPresent = true)]
public OrderDetail NotShownDetail { get; set; }
}

[AutoExpand(DisableWhenSelectPresent = true)]
public class DerivedOrder2 : NormalOrder
{
public OrderDetail NotShownDetail { get; set; }
}

public class OrderDetail
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ public static IEdmModel GetEdmModel(HttpConfiguration configuration)
builder.EntitySet<ChoiceOrder>("OrderChoices");
builder.EntitySet<NormalOrder>("NormalOrders");
builder.EntityType<DerivedOrder>();
builder.EntityType<DerivedOrder2>();
builder.EntitySet<OrderDetail>("OrderDetails");
builder.EntitySet<People>("People");
IEdmModel model = builder.GetEdmModel();
return model;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,71 @@ public void DerivedAutoExpandNavigationPropertyTest(string url)
Assert.Contains("OrderDetail", result);
}

[Theory]
[InlineData("{0}/autoexpand/NormalOrders?$select=Id", true)]
[InlineData("{0}/autoexpand/NormalOrders", false)]
[InlineData("{0}/autoexpand/NormalOrders(2)?$select=Id", true)]
[InlineData("{0}/autoexpand/NormalOrders(2)", false)]
[InlineData("{0}/autoexpand/NormalOrders(3)?$select=Id", true)]
[InlineData("{0}/autoexpand/NormalOrders(3)", false)]
public void DisableAutoExpandWhenSelectIsPresentTest(string url, bool isSelectPresent)
{
// Arrange
string queryUrl = string.Format(url, BaseAddress);
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, queryUrl);
request.Headers.Accept.Add(MediaTypeWithQualityHeaderValue.Parse("application/json;odata.metadata=none"));
HttpClient client = new HttpClient();
HttpResponseMessage response;

// Act
response = client.SendAsync(request).Result;

// Assert
Assert.NotNull(response);
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
Assert.NotNull(response.Content);
string result = response.Content.ReadAsStringAsync().Result;
if (isSelectPresent)
{
Assert.DoesNotContain("NotShownOrderDetail4", result);
}
else
{
Assert.Contains("NotShownOrderDetail4", result);
}
}

[Theory]
[InlineData("{0}/autoexpand/NormalOrders(2)?$expand=LinkOrder($select=Id)", true)]
[InlineData("{0}/autoexpand/NormalOrders(2)?$expand=LinkOrder", false)]
public void DisableAutoExpandWhenSelectIsPresentDollarExpandTest(string url, bool isSelectPresent)
{
// Arrange
string queryUrl = string.Format(url, BaseAddress);
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, queryUrl);
request.Headers.Accept.Add(MediaTypeWithQualityHeaderValue.Parse("application/json;odata.metadata=none"));
HttpClient client = new HttpClient();
HttpResponseMessage response;

// Act
response = client.SendAsync(request).Result;

// Assert
Assert.NotNull(response);
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
Assert.NotNull(response.Content);
string result = response.Content.ReadAsStringAsync().Result;
Assert.Contains("NotShownOrderDetail4", result);
if (isSelectPresent)
{
Assert.DoesNotContain("NotShownOrderDetail2", result);
}
else
{
Assert.Contains("NotShownOrderDetail2", result);
}
}

private static void VerifyOrderAndChoiceOrder(JObject customer, bool special = false, bool vip = false)
{
JObject order = customer["Order"] as JObject;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -482,6 +482,7 @@ public class System.Web.OData.QueryableRestrictions {
public QueryableRestrictions (PropertyConfiguration propertyConfiguration)

bool AutoExpand { public get; public set; }
bool DisableAutoExpandWhenSelectIsPresent { public get; public set; }
bool NonFilterable { public get; public set; }
bool NotCountable { public get; public set; }
bool NotExpandable { public get; public set; }
Expand Down Expand Up @@ -956,6 +957,7 @@ public abstract class System.Web.OData.Builder.PropertyConfiguration {
bool AddedExplicitly { public get; public set; }
bool AutoExpand { public get; public set; }
StructuralTypeConfiguration DeclaringType { public get; }
bool DisableAutoExpandWhenSelectIsPresent { public get; public set; }
bool IsRestricted { public get; }
PropertyKind Kind { public abstract get; }
string Name { public get; public set; }
Expand Down Expand Up @@ -1446,7 +1448,7 @@ public class System.Web.OData.Builder.NavigationPropertyConfiguration : Property
System.Collections.Generic.IEnumerable`1[[System.Reflection.PropertyInfo]] PrincipalProperties { public get; }
System.Type RelatedClrType { public virtual get; }

public NavigationPropertyConfiguration AutomaticallyExpand ()
public NavigationPropertyConfiguration AutomaticallyExpand (bool disableWhenSelectIsPresent)
public NavigationPropertyConfiguration CascadeOnDelete ()
public NavigationPropertyConfiguration CascadeOnDelete (bool cascade)
public NavigationPropertyConfiguration Contained ()
Expand Down Expand Up @@ -1611,6 +1613,8 @@ AttributeUsageAttribute(),
]
public sealed class System.Web.OData.Builder.AutoExpandAttribute : System.Attribute, _Attribute {
public AutoExpandAttribute ()

bool DisableWhenSelectPresent { public get; public set; }
}

[
Expand Down

0 comments on commit 5427761

Please sign in to comment.