Skip to content

Commit f808208

Browse files
update changes based on review comments
1 parent fbb6c0a commit f808208

File tree

3 files changed

+45
-1
lines changed

3 files changed

+45
-1
lines changed

src/Microsoft.AspNetCore.OData/Query/Expressions/BinderExtensions.cs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,10 @@ public static IQueryable ApplyBind(this ISelectExpandBinder binder, IQueryable s
275275
throw Error.ArgumentNull(nameof(context));
276276
}
277277

278-
context.QueryProvider = source.Provider.GetType().Namespace;
278+
if (string.IsNullOrEmpty(context.QueryProvider))
279+
{
280+
context.QueryProvider = source.Provider.GetType().Namespace;
281+
}
279282

280283
Type elementType = context.ElementClrType;
281284

src/Microsoft.AspNetCore.OData/Query/Expressions/SelectExpandBinder.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -444,6 +444,7 @@ private Expression UpdateMemberInitExpression(Expression source, IEdmStructuredT
444444
if (QueryProvider != null &&
445445
QueryProvider == HandleNullPropagationOptionHelper.EntityFrameworkQueryProviderNamespace ||
446446
QueryProvider == HandleNullPropagationOptionHelper.ObjectContextQueryProviderNamespaceEF5 ||
447+
QueryProvider == HandleNullPropagationOptionHelper.ObjectContextQueryProviderNamespaceEF6 ||
447448
QueryProvider == HandleNullPropagationOptionHelper.ObjectContextQueryProviderNamespaceEFCore2)
448449
{
449450
Type elementType = source.Type;

test/Microsoft.AspNetCore.OData.Tests/Query/Expressions/SelectExpandBinderTest.cs

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,12 +203,52 @@ public void Bind_GeneratedExpression_ContainsExpandedObject()
203203
.ToDictionary(PropertyMapper)["Orders"] as IEnumerable<SelectExpandWrapper<QueryOrder>>;
204204
Assert.NotNull(innerOrders);
205205
SelectExpandWrapper<QueryOrder> partialOrder = innerOrders.Single();
206+
var tt = _queryable.First().Orders.First();
207+
var xx = _queryable.First();
206208
Assert.Same(_queryable.First().Orders.First(), partialOrder.Instance);
207209
object customer = partialOrder.Container.ToDictionary(PropertyMapper)["Customer"];
208210
SelectExpandWrapper<QueryCustomer> innerInnerCustomer = Assert.IsAssignableFrom<SelectExpandWrapper<QueryCustomer>>(customer);
209211
Assert.Same(_queryable.First(), innerInnerCustomer.Instance);
210212
}
211213

214+
[Theory]
215+
[InlineData(HandleNullPropagationOptionHelper.EntityFrameworkQueryProviderNamespace)]
216+
[InlineData(HandleNullPropagationOptionHelper.ObjectContextQueryProviderNamespaceEFCore2)]
217+
[InlineData(HandleNullPropagationOptionHelper.ObjectContextQueryProviderNamespaceEF5)]
218+
[InlineData(HandleNullPropagationOptionHelper.ObjectContextQueryProviderNamespaceEF6)]
219+
public void Bind_UsingEFQueryProvider_GeneratedExpression_ContainsExpandedObject(string queryProvider)
220+
{
221+
// Arrange
222+
SelectExpandQueryOption selectExpand = new SelectExpandQueryOption("Orders", "Orders,Orders($expand=Customer)", _context);
223+
224+
// Act
225+
SelectExpandBinder binder = new SelectExpandBinder();
226+
_queryBinderContext.QueryProvider = queryProvider;
227+
IQueryable queryable = binder.ApplyBind(_queryable, selectExpand.SelectExpandClause, _queryBinderContext);
228+
229+
// Assert
230+
IEnumerator enumerator = queryable.GetEnumerator();
231+
Assert.True(enumerator.MoveNext());
232+
var partialCustomer = Assert.IsAssignableFrom<SelectExpandWrapper<QueryCustomer>>(enumerator.Current);
233+
Assert.False(enumerator.MoveNext());
234+
Assert.Null(partialCustomer.Instance);
235+
Assert.Equal("Microsoft.AspNetCore.OData.Tests.Query.Expressions.QueryCustomer", partialCustomer.InstanceType);
236+
IEnumerable<SelectExpandWrapper<QueryOrder>> innerOrders = partialCustomer.Container
237+
.ToDictionary(PropertyMapper)["Orders"] as IEnumerable<SelectExpandWrapper<QueryOrder>>;
238+
Assert.NotNull(innerOrders);
239+
SelectExpandWrapper<QueryOrder> partialOrder = innerOrders.Single();
240+
241+
// We only write structural properties to the instance.
242+
// This means that navigation properties on the instance property will be null
243+
// when using any instance of EF query provider.
244+
Assert.Null(partialOrder.Instance.Customer);
245+
246+
object customer = partialOrder.Container.ToDictionary(PropertyMapper)["Customer"];
247+
SelectExpandWrapper<QueryCustomer> innerInnerCustomer = Assert.IsAssignableFrom<SelectExpandWrapper<QueryCustomer>>(customer);
248+
249+
Assert.Null(innerInnerCustomer.Instance.Orders);
250+
}
251+
212252
[Fact]
213253
public void Bind_GeneratedExpression_CheckNullObjectWithinChainProjectionByKey()
214254
{

0 commit comments

Comments
 (0)