Skip to content

Commit e6c1b42

Browse files
committed
remove SearchDerivedTypeForAutoExpand option in EnableQuery Attribute
1 parent c44ce55 commit e6c1b42

10 files changed

Lines changed: 35 additions & 202 deletions

File tree

OData/src/System.Web.OData/OData/EnableQueryAttribute.cs

Lines changed: 8 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -340,21 +340,6 @@ public int MaxOrderByNodeCount
340340
}
341341
}
342342

343-
/// <summary>
344-
/// Gets or sets a value indicating whether to search derived type when finding AutoExpand properties.
345-
/// </summary>
346-
public bool SearchDerivedTypeWhenAutoExpand
347-
{
348-
get
349-
{
350-
return _querySettings.SearchDerivedTypeWhenAutoExpand;
351-
}
352-
set
353-
{
354-
_querySettings.SearchDerivedTypeWhenAutoExpand = value;
355-
}
356-
}
357-
358343
/// <summary>
359344
/// Performs the query composition after action is executed. It first tries to retrieve the IQueryable from the
360345
/// returning response message. It then validates the query from uri based on the validation settings on
@@ -710,29 +695,18 @@ private bool ContainsAutoExpandProperty(object response, HttpRequestMessage requ
710695
{
711696
throw Error.InvalidOperation(SRResources.QueryGetModelMustNotReturnNull);
712697
}
713-
IEdmEntityType baseEntityType = model.GetEdmType(elementClrType) as IEdmEntityType;
714-
List<IEdmEntityType> entityTypes = new List<IEdmEntityType>();
715-
if (baseEntityType != null)
716-
{
717-
entityTypes.Add(baseEntityType);
718-
if (SearchDerivedTypeWhenAutoExpand)
719-
{
720-
entityTypes.AddRange(EdmLibHelpers.GetAllDerivedEntityTypes(baseEntityType, model));
721-
}
722698

723-
foreach (var entityType in entityTypes)
699+
IEdmEntityType entityType = model.GetEdmType(elementClrType) as IEdmEntityType;
700+
if (entityType != null)
701+
{
702+
var navigationProperties = entityType.NavigationProperties();
703+
if (navigationProperties != null)
724704
{
725-
var navigationProperties = entityType == baseEntityType
726-
? entityType.NavigationProperties()
727-
: entityType.DeclaredNavigationProperties();
728-
if (navigationProperties != null)
705+
foreach (var navigationProperty in navigationProperties)
729706
{
730-
foreach (var navigationProperty in navigationProperties)
707+
if (EdmLibHelpers.IsAutoExpand(navigationProperty, model))
731708
{
732-
if (EdmLibHelpers.IsAutoExpand(navigationProperty, model))
733-
{
734-
return true;
735-
}
709+
return true;
736710
}
737711
}
738712
}

OData/src/System.Web.OData/OData/Formatter/EdmLibHelpers.cs

Lines changed: 8 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -315,62 +315,25 @@ public static bool IsAutoExpand(IEdmProperty edmProperty, IEdmModel edmModel)
315315
return annotation == null ? false : annotation.Restrictions.AutoExpand;
316316
}
317317

318-
public static IEnumerable<IEdmEntityType> GetAllDerivedEntityTypes(
319-
IEdmEntityType entityType, IEdmModel edmModel)
320-
{
321-
List<IEdmEntityType> derivedEntityTypes = new List<IEdmEntityType>();
322-
if (entityType != null)
323-
{
324-
List<IEdmStructuredType> typeList = new List<IEdmStructuredType>();
325-
typeList.Add(entityType);
326-
while (typeList.Count > 0)
327-
{
328-
var head = typeList[0];
329-
derivedEntityTypes.Add(head as IEdmEntityType);
330-
var derivedTypes = edmModel.FindDirectlyDerivedTypes(head);
331-
if (derivedTypes != null)
332-
{
333-
typeList.AddRange(derivedTypes);
334-
}
335-
336-
typeList.RemoveAt(0);
337-
}
338-
}
339-
340-
derivedEntityTypes.RemoveAt(0);
341-
return derivedEntityTypes;
342-
}
343-
344318
public static IEnumerable<IEdmNavigationProperty> GetAutoExpandNavigationProperties(
345-
IEdmEntityType baseEntityType, IEdmModel edmModel, bool searchDerivedTypeWhenAutoExpand)
319+
IEdmEntityType entityType, IEdmModel edmModel)
346320
{
347321
List<IEdmNavigationProperty> autoExpandNavigationProperties = new List<IEdmNavigationProperty>();
348-
List<IEdmEntityType> entityTypes = new List<IEdmEntityType>();
349-
if (baseEntityType != null)
322+
if (entityType != null)
350323
{
351-
entityTypes.Add(baseEntityType);
352-
if (searchDerivedTypeWhenAutoExpand)
353-
{
354-
entityTypes.AddRange(GetAllDerivedEntityTypes(baseEntityType, edmModel));
355-
}
356-
357-
foreach (var entityType in entityTypes)
324+
var navigationProperties = entityType.NavigationProperties();
325+
if (navigationProperties != null)
358326
{
359-
var navigationProperties = entityType == baseEntityType
360-
? entityType.NavigationProperties()
361-
: entityType.DeclaredNavigationProperties();
362-
if (navigationProperties != null)
327+
foreach (var navigationProperty in navigationProperties)
363328
{
364-
foreach (var navigationProperty in navigationProperties)
329+
if (IsAutoExpand(navigationProperty, edmModel))
365330
{
366-
if (IsAutoExpand(navigationProperty, edmModel))
367-
{
368-
autoExpandNavigationProperties.Add(navigationProperty);
369-
}
331+
autoExpandNavigationProperties.Add(navigationProperty);
370332
}
371333
}
372334
}
373335
}
336+
374337
return autoExpandNavigationProperties;
375338
}
376339

OData/src/System.Web.OData/OData/Query/ODataQueryOptions.cs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -362,7 +362,7 @@ public virtual IQueryable ApplyTo(IQueryable query, ODataQuerySettings querySett
362362
result = Top.ApplyTo(result, querySettings);
363363
}
364364

365-
AddAutoExpandProperties(querySettings);
365+
AddAutoExpandProperties();
366366

367367
if (SelectExpand != null)
368368
{
@@ -426,7 +426,7 @@ public virtual object ApplyTo(object entity, ODataQuerySettings querySettings)
426426
throw Error.InvalidOperation(SRResources.NonSelectExpandOnSingleEntity);
427427
}
428428

429-
AddAutoExpandProperties(querySettings);
429+
AddAutoExpandProperties();
430430

431431
if (SelectExpand != null)
432432
{
@@ -586,9 +586,9 @@ internal virtual ETag GetETag(EntityTagHeaderValue etagHeaderValue)
586586
return Request.GetETag(etagHeaderValue);
587587
}
588588

589-
internal void AddAutoExpandProperties(ODataQuerySettings querySettings)
589+
internal void AddAutoExpandProperties()
590590
{
591-
var autoExpandRawValue = GetAutoExpandRawValue(querySettings.SearchDerivedTypeWhenAutoExpand);
591+
var autoExpandRawValue = GetAutoExpandRawValue();
592592
if (autoExpandRawValue != null && !autoExpandRawValue.Equals(RawValues.Expand))
593593
{
594594
IDictionary<string, string> queryParameters =
@@ -609,13 +609,13 @@ internal void AddAutoExpandProperties(ODataQuerySettings querySettings)
609609
}
610610
}
611611

612-
private string GetAutoExpandRawValue(bool discoverDerivedTypeWhenAutoExpand)
612+
private string GetAutoExpandRawValue()
613613
{
614614
var expandRawValue = RawValues.Expand;
615615
IEdmEntityType baseEntityType = Context.ElementType as IEdmEntityType;
616616
var autoExpandRawValue = String.Empty;
617617
var autoExpandNavigationProperties = EdmLibHelpers.GetAutoExpandNavigationProperties(baseEntityType,
618-
Context.Model, discoverDerivedTypeWhenAutoExpand);
618+
Context.Model);
619619

620620
foreach (var property in autoExpandNavigationProperties)
621621
{
@@ -746,7 +746,7 @@ private T ApplySelectExpand<T>(T entity, ODataQuerySettings querySettings)
746746
expandAvailable ? RawValues.Expand : null,
747747
SelectExpand.Context);
748748
}
749-
SelectExpand.SearchDerivedTypeWhenAutoExpand = querySettings.SearchDerivedTypeWhenAutoExpand;
749+
750750
SelectExpandClause processedClause = SelectExpand.ProcessLevels();
751751
SelectExpandQueryOption newSelectExpand = new SelectExpandQueryOption(
752752
SelectExpand.RawSelect,

OData/src/System.Web.OData/OData/Query/ODataQuerySettings.cs

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -96,10 +96,5 @@ public int? PageSize
9696
_pageSize = value;
9797
}
9898
}
99-
100-
/// <summary>
101-
/// Gets or sets a value indicating whether to search derived type when finding AutoExpand properties.
102-
/// </summary>
103-
public bool SearchDerivedTypeWhenAutoExpand { get; set; }
10499
}
105100
}

OData/src/System.Web.OData/OData/Query/SelectExpandQueryOption.cs

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -170,11 +170,6 @@ public int LevelsMaxLiteralExpansionDepth
170170
}
171171
}
172172

173-
/// <summary>
174-
/// Gets or sets a value indicating whether to search derived type when finding AutoExpand properties.
175-
/// </summary>
176-
public bool SearchDerivedTypeWhenAutoExpand { get; set; }
177-
178173
/// <summary>
179174
/// Applies the $select and $expand query options to the given <see cref="IQueryable"/> using the given
180175
/// <see cref="ODataQuerySettings"/>.
@@ -389,12 +384,10 @@ private static IEnumerable<SelectItem> GetAutoExpandedNavigationSelectItems(
389384
IEdmModel model,
390385
string alreadyExpandedNavigationSourceName,
391386
IEdmNavigationSource navigationSource,
392-
bool isAllSelected,
393-
bool searchDerivedTypeWhenAutoExpand)
387+
bool isAllSelected)
394388
{
395389
var expandItems = new List<SelectItem>();
396-
var autoExpandNavigationProperties = EdmLibHelpers.GetAutoExpandNavigationProperties(baseEntityType, model,
397-
searchDerivedTypeWhenAutoExpand);
390+
var autoExpandNavigationProperties = EdmLibHelpers.GetAutoExpandNavigationProperties(baseEntityType, model);
398391
foreach (var navigationProperty in autoExpandNavigationProperties)
399392
{
400393
if (!alreadyExpandedNavigationSourceName.Equals(navigationProperty.Name))
@@ -422,8 +415,7 @@ private static IEnumerable<SelectItem> GetAutoExpandedNavigationSelectItems(
422415
model,
423416
alreadyExpandedNavigationSourceName,
424417
item.NavigationSource,
425-
true,
426-
searchDerivedTypeWhenAutoExpand);
418+
true);
427419
selectExpandClause = new SelectExpandClause(nestedSelectItems, true);
428420
item = new ExpandedNavigationSelectItem(expandPath, currentEdmNavigationSource,
429421
selectExpandClause);
@@ -512,8 +504,7 @@ private ExpandedNavigationSelectItem ProcessLevels(
512504
Context.Model,
513505
alreadyExpandedNavigationSourceName,
514506
expandItem.NavigationSource,
515-
selectExpandClause.AllSelected,
516-
SearchDerivedTypeWhenAutoExpand);
507+
selectExpandClause.AllSelected);
517508
bool hasAutoExpandInExpand = (autoExpandNavigationSelectItems.Count() != 0);
518509

519510
while (level > 0)

OData/test/E2ETest/WebStack.QA.Test.OData/AutoExpand/AutoExpandController.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,15 @@ public class CustomersController : ODataController
88
{
99
private readonly AutoExpandContext _db = new AutoExpandContext();
1010

11-
[EnableQuery(SearchDerivedTypeWhenAutoExpand = true)]
11+
[EnableQuery]
1212
public IQueryable<Customer> Get()
1313
{
1414
ResetDataSource();
1515
var db = new AutoExpandContext();
1616
return db.Customers;
1717
}
1818

19-
[EnableQuery(SearchDerivedTypeWhenAutoExpand = true)]
19+
[EnableQuery]
2020
public SingleResult<Customer> Get(int key)
2121
{
2222
ResetDataSource();
@@ -169,7 +169,7 @@ public class NormalOrdersController : ODataController
169169
{
170170
private readonly AutoExpandContext _db = new AutoExpandContext();
171171

172-
[EnableQuery(SearchDerivedTypeWhenAutoExpand = true)]
172+
[EnableQuery]
173173
public IQueryable<NormalOrder> Get()
174174
{
175175
ResetDataSource();

OData/test/E2ETest/WebStack.QA.Test.OData/AutoExpand/AutoExpandTests.cs

Lines changed: 0 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -132,87 +132,6 @@ public void LevelsWithAutoExpandInDifferentNavigationProperty(string level, int
132132
Assert.Null(friend["Friend"]);
133133
}
134134

135-
[Fact]
136-
public void QueryForAnEntryIncludeTheDerivedAutoExpandNavigationProperty()
137-
{
138-
// Arrange
139-
string queryUrl = string.Format("{0}/autoexpand/Customers(8)", BaseAddress);
140-
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, queryUrl);
141-
request.Headers.Accept.Add(MediaTypeWithQualityHeaderValue.Parse("application/json;odata.metadata=none"));
142-
HttpClient client = new HttpClient();
143-
HttpResponseMessage response;
144-
145-
// Act
146-
response = client.SendAsync(request).Result;
147-
148-
// Assert
149-
Assert.NotNull(response);
150-
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
151-
Assert.NotNull(response.Content);
152-
153-
var customer = response.Content.ReadAsAsync<JObject>().Result;
154-
VerifyOrderAndChoiceOrder(customer, special: true);
155-
156-
// level one
157-
JObject friend = customer["Friend"] as JObject;
158-
VerifyOrderAndChoiceOrder(friend);
159-
160-
// level two
161-
Assert.Null(friend["Friend"]);
162-
}
163-
164-
[Fact]
165-
public void QueryForAnEntryIncludeTheMultiDerivedAutoExpandNavigationProperty()
166-
{
167-
// Arrange
168-
string queryUrl = string.Format("{0}/autoexpand/Customers(9)", BaseAddress);
169-
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, queryUrl);
170-
request.Headers.Accept.Add(MediaTypeWithQualityHeaderValue.Parse("application/json;odata.metadata=none"));
171-
HttpClient client = new HttpClient();
172-
HttpResponseMessage response;
173-
174-
// Act
175-
response = client.SendAsync(request).Result;
176-
177-
// Assert
178-
Assert.NotNull(response);
179-
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
180-
Assert.NotNull(response.Content);
181-
182-
var customer = response.Content.ReadAsAsync<JObject>().Result;
183-
VerifyOrderAndChoiceOrder(customer, special: true, vip: true);
184-
185-
// level one
186-
JObject friend = customer["Friend"] as JObject;
187-
VerifyOrderAndChoiceOrder(friend, special: true);
188-
189-
// level two
190-
Assert.Null(friend["Friend"]);
191-
}
192-
193-
[Theory]
194-
[InlineData("{0}/autoexpand/NormalOrders", true)]
195-
[InlineData("{0}/autoexpand/NormalOrders(1)", false)]
196-
public void DerivedAutoExpandNavigationPropertyTest(string url, bool searchDerivedType)
197-
{
198-
// Arrange
199-
string queryUrl = string.Format(url, BaseAddress);
200-
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, queryUrl);
201-
request.Headers.Accept.Add(MediaTypeWithQualityHeaderValue.Parse("application/json;odata.metadata=none"));
202-
HttpClient client = new HttpClient();
203-
HttpResponseMessage response;
204-
205-
// Act
206-
response = client.SendAsync(request).Result;
207-
208-
// Assert
209-
Assert.NotNull(response);
210-
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
211-
Assert.NotNull(response.Content);
212-
string result = response.Content.ReadAsStringAsync().Result;
213-
Assert.Equal(searchDerivedType, result.Contains("OrderDetail"));
214-
}
215-
216135
private static void VerifyOrderAndChoiceOrder(JObject customer, bool special = false, bool vip = false)
217136
{
218137
JObject order = customer["Order"] as JObject;

OData/test/UnitTest/System.Web.OData.Test/OData/Query/ODataQuerySettingsTest.cs

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -60,14 +60,5 @@ public void EnableConstantParameterization_Property_RoundTrips()
6060
o => o.EnableConstantParameterization,
6161
expectedDefaultValue: true);
6262
}
63-
64-
[Fact]
65-
public void DiscoverDerivedTypeWhenAutoExpand_Property_RoundTrips()
66-
{
67-
Assert.Reflection.BooleanProperty<ODataQuerySettings>(
68-
new ODataQuerySettings(),
69-
o => o.SearchDerivedTypeWhenAutoExpand,
70-
expectedDefaultValue: false);
71-
}
7263
}
7364
}

OData/test/UnitTest/System.Web.OData.Test/OData/Query/SelectExpandQueryOptionTest.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -616,7 +616,7 @@ public void ProcessLevelsCorrectly_WithMaxLevelsAndAutoExpand()
616616
model.FindDeclaredType("System.Web.OData.TestCommon.Models.AutoExpandCustomer"));
617617
var request = new HttpRequestMessage(HttpMethod.Get, "http://test?$expand=Friend($levels=max)");
618618
var queryOption = new ODataQueryOptions(context, request);
619-
queryOption.AddAutoExpandProperties(new ODataQuerySettings());
619+
queryOption.AddAutoExpandProperties();
620620
var selectExpand = queryOption.SelectExpand;
621621

622622
// Act
@@ -685,7 +685,7 @@ public void ProcessLevelsCorrectly_WithAutoExpand()
685685
model.FindDeclaredType("System.Web.OData.TestCommon.Models.AutoExpandCustomer"));
686686
var request = new HttpRequestMessage(HttpMethod.Get, "http://test");
687687
var queryOption = new ODataQueryOptions(context, request);
688-
queryOption.AddAutoExpandProperties(new ODataQuerySettings());
688+
queryOption.AddAutoExpandProperties();
689689
var selectExpand = queryOption.SelectExpand;
690690

691691
// Act

0 commit comments

Comments
 (0)