-
Notifications
You must be signed in to change notification settings - Fork 467
Expand file tree
/
Copy pathSelectExpandBinder.cs
More file actions
1265 lines (1082 loc) · 60 KB
/
SelectExpandBinder.cs
File metadata and controls
1265 lines (1082 loc) · 60 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Diagnostics.Contracts;
using System.Linq;
using System.Linq.Expressions;
using System.Reflection;
using Microsoft.AspNet.OData.Common;
using Microsoft.AspNet.OData.Formatter;
using Microsoft.OData;
using Microsoft.OData.Edm;
using Microsoft.OData.UriParser;
namespace Microsoft.AspNet.OData.Query.Expressions
{
/// <summary>
/// Applies the given <see cref="SelectExpandQueryOption"/> to the given <see cref="IQueryable"/>.
/// </summary>
[SuppressMessage("Microsoft.Maintainability", "CA1506:AvoidExcessiveClassCoupling", Justification = "Class coupling acceptable.")]
internal class SelectExpandBinder
{
private ODataQueryContext _context;
private IEdmModel _model;
private ODataQuerySettings _settings;
private string _modelID;
private DataSourceProviderKind _dataSourceProviderKind;
public SelectExpandBinder(ODataQuerySettings settings, ODataQueryContext context)
{
Contract.Assert(settings != null);
Contract.Assert(context != null);
Contract.Assert(context.Model != null);
Contract.Assert(settings.HandleNullPropagation != HandleNullPropagationOption.Default);
_context = context;
_model = _context.Model;
_modelID = ModelContainer.GetModelID(_model);
_settings = settings;
_dataSourceProviderKind = DataSourceProviderKind.Unknown;
}
public static IQueryable Bind(IQueryable queryable, ODataQuerySettings settings, SelectExpandQueryOption selectExpandQuery)
{
Contract.Assert(queryable != null);
Contract.Assert(selectExpandQuery != null);
SelectExpandBinder binder = new SelectExpandBinder(settings, selectExpandQuery.Context);
return binder.Bind(queryable, selectExpandQuery);
}
public static object Bind(object entity, ODataQuerySettings settings, SelectExpandQueryOption selectExpandQuery)
{
Contract.Assert(entity != null);
Contract.Assert(selectExpandQuery != null);
SelectExpandBinder binder = new SelectExpandBinder(settings, selectExpandQuery.Context);
return binder.Bind(entity, selectExpandQuery);
}
private object Bind(object entity, SelectExpandQueryOption selectExpandQuery)
{
_dataSourceProviderKind = DataSourceProviderKind.InMemory;
// Needn't to verify the input, that's done at upper level.
LambdaExpression projectionLambda = GetProjectionLambda(selectExpandQuery);
// TODO: cache this ?
return projectionLambda.Compile().DynamicInvoke(entity);
}
private IQueryable Bind(IQueryable queryable, SelectExpandQueryOption selectExpandQuery)
{
_dataSourceProviderKind = queryable.GetDataSourceProviderKind();
// Needn't to verify the input, that's done at upper level.
Type elementType = selectExpandQuery.Context.ElementClrType;
LambdaExpression projectionLambda = GetProjectionLambda(selectExpandQuery);
MethodInfo selectMethod = ExpressionHelperMethods.QueryableSelectGeneric.MakeGenericMethod(elementType, projectionLambda.Body.Type);
return selectMethod.Invoke(null, new object[] { queryable, projectionLambda }) as IQueryable;
}
private LambdaExpression GetProjectionLambda(SelectExpandQueryOption selectExpandQuery)
{
Type elementType = selectExpandQuery.Context.ElementClrType;
IEdmNavigationSource navigationSource = selectExpandQuery.Context.NavigationSource;
ParameterExpression source = Expression.Parameter(elementType, "$it");
// expression looks like -> new Wrapper { Instance = source , Properties = "...", Container = new PropertyContainer { ... } }
Expression projectionExpression = ProjectElement(source, selectExpandQuery.SelectExpandClause, _context.ElementType as IEdmStructuredType, navigationSource);
// expression looks like -> source => new Wrapper { Instance = source .... }
LambdaExpression projectionLambdaExpression = Expression.Lambda(projectionExpression, source);
return projectionLambdaExpression;
}
internal Expression ProjectAsWrapper(Expression source, SelectExpandClause selectExpandClause,
IEdmStructuredType structuredType, IEdmNavigationSource navigationSource, OrderByClause orderByClause = null,
long? topOption = null,
long? skipOption = null,
int? modelBoundPageSize = null)
{
Type elementType;
if (TypeHelper.IsCollection(source.Type, out elementType))
{
// new CollectionWrapper<ElementType> { Instance = source.Select(s => new Wrapper { ... }) };
return ProjectCollection(source, elementType, selectExpandClause, structuredType, navigationSource, orderByClause,
topOption,
skipOption,
modelBoundPageSize);
}
else
{
// new Wrapper { v1 = source.property ... }
return ProjectElement(source, selectExpandClause, structuredType, navigationSource);
}
}
internal Expression CreatePropertyNameExpression(IEdmStructuredType elementType, IEdmProperty property, Expression source)
{
Contract.Assert(elementType != null);
Contract.Assert(property != null);
Contract.Assert(source != null);
IEdmStructuredType declaringType = property.DeclaringType;
// derived property using cast
if (elementType != declaringType)
{
Type originalType = EdmLibHelpers.GetClrType(elementType, _model);
Type castType = EdmLibHelpers.GetClrType(declaringType, _model);
if (castType == null)
{
throw new ODataException(Error.Format(SRResources.MappingDoesNotContainResourceType, declaringType.FullTypeName()));
}
if (!castType.IsAssignableFrom(originalType))
{
// Expression
// source is navigationPropertyDeclaringType ? propertyName : null
return Expression.Condition(
test: Expression.TypeIs(source, castType),
ifTrue: Expression.Constant(property.Name),
ifFalse: Expression.Constant(null, typeof(string)));
}
}
// Expression
// "propertyName"
return Expression.Constant(property.Name);
}
internal Expression CreatePropertyValueExpression(IEdmStructuredType elementType, IEdmProperty property, Expression source, FilterClause filterClause)
{
Contract.Assert(elementType != null);
Contract.Assert(property != null);
Contract.Assert(source != null);
// Expression: source = source as propertyDeclaringType
if (elementType != property.DeclaringType)
{
Type castType = EdmLibHelpers.GetClrType(property.DeclaringType, _model);
if (castType == null)
{
throw new ODataException(Error.Format(SRResources.MappingDoesNotContainResourceType, property.DeclaringType.FullTypeName()));
}
source = Expression.TypeAs(source, castType);
}
// Expression: source.Property
string propertyName = EdmLibHelpers.GetClrPropertyName(property, _model);
PropertyInfo propertyInfo = source.Type.GetProperty(propertyName);
Expression propertyValue = Expression.Property(source, propertyInfo);
Type nullablePropertyType = TypeHelper.ToNullable(propertyValue.Type);
Expression nullablePropertyValue = ExpressionHelpers.ToNullable(propertyValue);
if (filterClause != null)
{
bool isCollection = property.Type.IsCollection();
IEdmTypeReference edmElementType = (isCollection ? property.Type.AsCollection().ElementType() : property.Type);
Type clrElementType = EdmLibHelpers.GetClrType(edmElementType, _model);
if (clrElementType == null)
{
throw new ODataException(Error.Format(SRResources.MappingDoesNotContainResourceType, edmElementType.FullName()));
}
Expression filterResult = nullablePropertyValue;
ODataQuerySettings querySettings = new ODataQuerySettings()
{
HandleNullPropagation = HandleNullPropagationOption.True,
};
if (isCollection)
{
Expression filterSource = nullablePropertyValue;
// TODO: Implement proper support for $select/$expand after $apply
Expression filterPredicate = FilterBinder.Bind(null, filterClause, clrElementType, _context, querySettings);
filterResult = Expression.Call(
ExpressionHelperMethods.EnumerableWhereGeneric.MakeGenericMethod(clrElementType),
filterSource,
filterPredicate);
nullablePropertyType = filterResult.Type;
}
else if (_settings.HandleReferenceNavigationPropertyExpandFilter)
{
LambdaExpression filterLambdaExpression = FilterBinder.Bind(null, filterClause, clrElementType, _context, querySettings) as LambdaExpression;
if (filterLambdaExpression == null)
{
throw new ODataException(Error.Format(SRResources.ExpandFilterExpressionNotLambdaExpression, property.Name, "LambdaExpression"));
}
ParameterExpression filterParameter = filterLambdaExpression.Parameters.First();
Expression predicateExpression = new ReferenceNavigationPropertyExpandFilterVisitor(filterParameter, nullablePropertyValue).Visit(filterLambdaExpression.Body);
// create expression similar to: 'predicateExpression == true ? nullablePropertyValue : null'
filterResult = Expression.Condition(
test: predicateExpression,
ifTrue: nullablePropertyValue,
ifFalse: Expression.Constant(value: null, type: nullablePropertyType));
}
if (_settings.HandleNullPropagation == HandleNullPropagationOption.True)
{
// create expression similar to: 'nullablePropertyValue == null ? null : filterResult'
nullablePropertyValue = Expression.Condition(
test: Expression.Equal(nullablePropertyValue, Expression.Constant(value: null)),
ifTrue: Expression.Constant(value: null, type: nullablePropertyType),
ifFalse: filterResult);
}
else
{
nullablePropertyValue = filterResult;
}
}
if (_settings.HandleNullPropagation == HandleNullPropagationOption.True)
{
// create expression similar to: 'source == null ? null : propertyValue'
propertyValue = Expression.Condition(
test: Expression.Equal(source, Expression.Constant(value: null)),
ifTrue: Expression.Constant(value: null, type: nullablePropertyType),
ifFalse: nullablePropertyValue);
}
else
{
// need to cast this to nullable as EF would fail while materializing if the property is not nullable and source is null.
propertyValue = nullablePropertyValue;
}
return propertyValue;
}
// Generates the expression
// source => new Wrapper { Instance = source, Container = new PropertyContainer { ..expanded properties.. } }
internal Expression ProjectElement(Expression source, SelectExpandClause selectExpandClause, IEdmStructuredType structuredType, IEdmNavigationSource navigationSource)
{
Contract.Assert(source != null);
// If it's not a structural type, just return the source.
if (structuredType == null)
{
return source;
}
Type elementType = source.Type;
Type wrapperType = typeof(SelectExpandWrapper<>).MakeGenericType(elementType);
List<MemberAssignment> wrapperTypeMemberAssignments = new List<MemberAssignment>();
PropertyInfo wrapperProperty;
Expression wrapperPropertyValueExpression;
bool isInstancePropertySet = false;
bool isTypeNamePropertySet = false;
bool isContainerPropertySet = false;
// Initialize property 'ModelID' on the wrapper class.
// source = new Wrapper { ModelID = 'some-guid-id' }
wrapperProperty = wrapperType.GetProperty("ModelID");
wrapperPropertyValueExpression = _settings.EnableConstantParameterization ?
LinqParameterContainer.Parameterize(typeof(string), _modelID) :
Expression.Constant(_modelID);
wrapperTypeMemberAssignments.Add(Expression.Bind(wrapperProperty, wrapperPropertyValueExpression));
if (IsSelectAll(selectExpandClause))
{
// Initialize property 'Instance' on the wrapper class
wrapperProperty = wrapperType.GetProperty("Instance");
wrapperTypeMemberAssignments.Add(Expression.Bind(wrapperProperty, source));
wrapperProperty = wrapperType.GetProperty("UseInstanceForProperties");
wrapperTypeMemberAssignments.Add(Expression.Bind(wrapperProperty, Expression.Constant(true)));
isInstancePropertySet = true;
}
else
{
// Initialize property 'TypeName' on the wrapper class as we don't have the instance.
Expression typeName = CreateTypeNameExpression(source, structuredType, _model);
if (typeName != null)
{
isTypeNamePropertySet = true;
wrapperProperty = wrapperType.GetProperty("InstanceType");
wrapperTypeMemberAssignments.Add(Expression.Bind(wrapperProperty, typeName));
}
}
// Initialize the property 'Container' on the wrapper class
// source => new Wrapper { Container = new PropertyContainer { .... } }
if (selectExpandClause != null)
{
IDictionary<IEdmStructuralProperty, PathSelectItem> propertiesToInclude;
IDictionary<IEdmNavigationProperty, ExpandedReferenceSelectItem> propertiesToExpand;
ISet<IEdmStructuralProperty> autoSelectedProperties;
bool isContainDynamicPropertySelection = GetSelectExpandProperties(_model, structuredType, navigationSource, selectExpandClause,
out propertiesToInclude,
out propertiesToExpand,
out autoSelectedProperties);
bool isSelectingOpenTypeSegments = isContainDynamicPropertySelection || IsSelectAllOnOpenType(selectExpandClause, structuredType);
if (propertiesToExpand != null || propertiesToInclude != null || autoSelectedProperties != null || isSelectingOpenTypeSegments)
{
Expression propertyContainerCreation =
BuildPropertyContainer(source, structuredType, propertiesToExpand, propertiesToInclude, autoSelectedProperties, isSelectingOpenTypeSegments);
if (propertyContainerCreation != null)
{
wrapperProperty = wrapperType.GetProperty("Container");
Contract.Assert(wrapperProperty != null);
wrapperTypeMemberAssignments.Add(Expression.Bind(wrapperProperty, propertyContainerCreation));
isContainerPropertySet = true;
}
}
}
Type wrapperGenericType = GetWrapperGenericType(isInstancePropertySet, isTypeNamePropertySet, isContainerPropertySet);
wrapperType = wrapperGenericType.MakeGenericType(elementType);
return Expression.MemberInit(Expression.New(wrapperType), wrapperTypeMemberAssignments);
}
/// <summary>
/// Gets the $select and $expand properties from the given <see cref="SelectExpandClause"/>
/// </summary>
/// <param name="model">The Edm model.</param>
/// <param name="structuredType">The current structural type.</param>
/// <param name="navigationSource">The current navigation source.</param>
/// <param name="selectExpandClause">The given select and expand clause.</param>
/// <param name="propertiesToInclude">The out properties to include at current level, could be null.</param>
/// <param name="propertiesToExpand">The out properties to expand at current level, could be null.</param>
/// <param name="autoSelectedProperties">The out auto selected properties to include at current level, could be null.</param>
/// <returns>true if the select contains dynamic property selection, false if it's not.</returns>
internal static bool GetSelectExpandProperties(IEdmModel model, IEdmStructuredType structuredType, IEdmNavigationSource navigationSource,
SelectExpandClause selectExpandClause,
out IDictionary<IEdmStructuralProperty, PathSelectItem> propertiesToInclude,
out IDictionary<IEdmNavigationProperty, ExpandedReferenceSelectItem> propertiesToExpand,
out ISet<IEdmStructuralProperty> autoSelectedProperties)
{
Contract.Assert(selectExpandClause != null);
// Properties to be included includes all the properties selected or in the middle of a $select and $expand path.
// for example: "$expand=abc/xyz/nav", "abc" and "xyz" are the middle properties that should be included.
// meanwhile, "nav" is the property that should be expanded.
// If it's a type cast path, for example: $select=NS.TypeCast/abc, "abc" should be included also.
propertiesToInclude = null;
propertiesToExpand = null;
autoSelectedProperties = null;
bool isSelectContainsDynamicProperty = false;
var currentLevelPropertiesInclude = new Dictionary<IEdmStructuralProperty, SelectExpandIncludedProperty>();
foreach (SelectItem selectItem in selectExpandClause.SelectedItems)
{
// $expand=...
ExpandedReferenceSelectItem expandedItem = selectItem as ExpandedReferenceSelectItem;
if (expandedItem != null)
{
ProcessExpandedItem(expandedItem, navigationSource, currentLevelPropertiesInclude, ref propertiesToExpand);
continue;
}
// $select=...
PathSelectItem pathItem = selectItem as PathSelectItem;
if (pathItem != null)
{
if (ProcessSelectedItem(pathItem, navigationSource, currentLevelPropertiesInclude))
{
isSelectContainsDynamicProperty = true;
}
continue;
}
// Skip processing the "WildcardSelectItem and NamespaceQualifiedWildcardSelectItem"
// ODL now doesn't support "$select=property/*" and "$select=property/NS.*"
}
if (!IsSelectAll(selectExpandClause))
{
// We should include the keys if it's an entity.
IEdmEntityType entityType = structuredType as IEdmEntityType;
if (entityType != null)
{
foreach (IEdmStructuralProperty keyProperty in entityType.Key())
{
if (!currentLevelPropertiesInclude.Keys.Contains(keyProperty))
{
if (autoSelectedProperties == null)
{
autoSelectedProperties = new HashSet<IEdmStructuralProperty>();
}
autoSelectedProperties.Add(keyProperty);
}
}
}
// We should add concurrency properties, if not added
if (navigationSource != null && model != null)
{
IEnumerable<IEdmStructuralProperty> concurrencyProperties = model.GetConcurrencyProperties(navigationSource);
foreach (IEdmStructuralProperty concurrencyProperty in concurrencyProperties)
{
if (structuredType.Properties().Any(p => p == concurrencyProperty))
{
if (!currentLevelPropertiesInclude.Keys.Contains(concurrencyProperty))
{
if (autoSelectedProperties == null)
{
autoSelectedProperties = new HashSet<IEdmStructuralProperty>();
}
autoSelectedProperties.Add(concurrencyProperty);
}
}
}
}
}
if (currentLevelPropertiesInclude.Any())
{
propertiesToInclude = new Dictionary<IEdmStructuralProperty, PathSelectItem>();
foreach (var propertiesInclude in currentLevelPropertiesInclude)
{
propertiesToInclude[propertiesInclude.Key] = propertiesInclude.Value == null ? null : propertiesInclude.Value.ToPathSelectItem();
}
}
return isSelectContainsDynamicProperty;
}
/// <summary>
/// Process the <see cref="ExpandedReferenceSelectItem"/>.
/// </summary>
/// <param name="expandedItem">The expaned item.</param>
/// <param name="navigationSource">The navigation source.</param>
/// <param name="currentLevelPropertiesInclude">The current level properties included.</param>
/// <param name="propertiesToExpand">out/ref, the property expanded.</param>
private static void ProcessExpandedItem(ExpandedReferenceSelectItem expandedItem,
IEdmNavigationSource navigationSource,
IDictionary<IEdmStructuralProperty, SelectExpandIncludedProperty> currentLevelPropertiesInclude,
ref IDictionary<IEdmNavigationProperty, ExpandedReferenceSelectItem> propertiesToExpand)
{
Contract.Assert(expandedItem != null && expandedItem.PathToNavigationProperty != null);
Contract.Assert(currentLevelPropertiesInclude != null);
// Verify and process the $expand=... path.
IList<ODataPathSegment> remainingSegments;
ODataPathSegment firstNonTypeSegment = expandedItem.PathToNavigationProperty.GetFirstNonTypeCastSegment(out remainingSegments);
// for $expand=NS.SubType/Nav, we don't care about the leading type segment, because with or without the type segment
// the "nav" property value expression should be built into the property container.
PropertySegment firstStructuralPropertySegment = firstNonTypeSegment as PropertySegment;
if (firstStructuralPropertySegment != null)
{
// for example: $expand=abc/nav, the remaining segments should never be null because at least the last navigation segment is there.
Contract.Assert(remainingSegments != null);
SelectExpandIncludedProperty newPropertySelectItem;
if (!currentLevelPropertiesInclude.TryGetValue(firstStructuralPropertySegment.Property, out newPropertySelectItem))
{
newPropertySelectItem = new SelectExpandIncludedProperty(firstStructuralPropertySegment, navigationSource);
currentLevelPropertiesInclude[firstStructuralPropertySegment.Property] = newPropertySelectItem;
}
newPropertySelectItem.AddSubExpandItem(remainingSegments, expandedItem);
}
else
{
// for example: $expand=nav, if we couldn't find a structural property in the path, it means we get the last navigation segment.
// So, the remaing segments should be null and the last segment should be "NavigationPropertySegment".
Contract.Assert(remainingSegments == null);
NavigationPropertySegment firstNavigationPropertySegment = firstNonTypeSegment as NavigationPropertySegment;
Contract.Assert(firstNavigationPropertySegment != null);
// Needn't add this navigation property into the include property.
// Because this navigation property will be included separately.
if (propertiesToExpand == null)
{
propertiesToExpand = new Dictionary<IEdmNavigationProperty, ExpandedReferenceSelectItem>();
}
propertiesToExpand[firstNavigationPropertySegment.NavigationProperty] = expandedItem;
}
}
/// <summary>
/// Process the <see cref="PathSelectItem"/>.
/// </summary>
/// <param name="pathSelectItem">The selected item.</param>
/// <param name="navigationSource">The navigation source.</param>
/// <param name="currentLevelPropertiesInclude">The current level properties included.</param>
/// <returns>true if it's dynamic property selection, false if it's not.</returns>
private static bool ProcessSelectedItem(PathSelectItem pathSelectItem,
IEdmNavigationSource navigationSource,
IDictionary<IEdmStructuralProperty, SelectExpandIncludedProperty> currentLevelPropertiesInclude)
{
Contract.Assert(pathSelectItem != null && pathSelectItem.SelectedPath != null);
Contract.Assert(currentLevelPropertiesInclude != null);
// Verify and process the $select path
IList<ODataPathSegment> remainingSegments;
ODataPathSegment firstNonTypeSegment = pathSelectItem.SelectedPath.GetFirstNonTypeCastSegment(out remainingSegments);
// for $select=NS.SubType/Property, we don't care about the leading type segment, because with or without the type segment
// the "Property" property value expression should be built into the property container.
PropertySegment firstSturucturalPropertySegment = firstNonTypeSegment as PropertySegment;
if (firstSturucturalPropertySegment != null)
{
// $select=abc/..../xyz
SelectExpandIncludedProperty newPropertySelectItem;
if (!currentLevelPropertiesInclude.TryGetValue(firstSturucturalPropertySegment.Property, out newPropertySelectItem))
{
newPropertySelectItem = new SelectExpandIncludedProperty(firstSturucturalPropertySegment, navigationSource);
currentLevelPropertiesInclude[firstSturucturalPropertySegment.Property] = newPropertySelectItem;
}
newPropertySelectItem.AddSubSelectItem(remainingSegments, pathSelectItem);
}
else
{
// If we can't find a PropertySegment, the $select path maybe selecting an operation, a navigation or dynamic property.
// And the remaing segments should be null.
Contract.Assert(remainingSegments == null);
// For operation (action/function), needn't process it.
// For navigation property, needn't process it here.
// For dynamic property, let's test the last segment for this path select item.
if (firstNonTypeSegment is DynamicPathSegment)
{
return true;
}
}
return false;
}
// To test whether the currect selection is SelectAll on an open type
private static bool IsSelectAllOnOpenType(SelectExpandClause selectExpandClause, IEdmStructuredType structuredType)
{
if (structuredType == null || !structuredType.IsOpen)
{
return false;
}
if (IsSelectAll(selectExpandClause))
{
return true;
}
return false;
}
private Expression CreateTotalCountExpression(Expression source, bool? countOption)
{
Expression countExpression = Expression.Constant(null, typeof(long?));
if (countOption == null || !countOption.Value)
{
return countExpression;
}
Type elementType;
if (!TypeHelper.IsCollection(source.Type, out elementType))
{
return countExpression;
}
// call Count() method.
countExpression = ExpressionHelpers.Count(source, elementType);
if (_settings.HandleNullPropagation == HandleNullPropagationOption.True)
{
// source == null ? null : countExpression
return Expression.Condition(
test: Expression.Equal(source, Expression.Constant(null)),
ifTrue: Expression.Constant(null, typeof(long?)),
ifFalse: ExpressionHelpers.ToNullable(countExpression));
}
else
{
return countExpression;
}
}
private Expression BuildPropertyContainer(Expression source, IEdmStructuredType structuredType,
IDictionary<IEdmNavigationProperty, ExpandedReferenceSelectItem> propertiesToExpand,
IDictionary<IEdmStructuralProperty, PathSelectItem> propertiesToInclude,
ISet<IEdmStructuralProperty> autoSelectedProperties,
bool isSelectingOpenTypeSegments)
{
IList<NamedPropertyExpression> includedProperties = new List<NamedPropertyExpression>();
if (propertiesToExpand != null)
{
foreach (var propertyToExpand in propertiesToExpand)
{
// $expand=abc or $expand=abc/$ref or $expand=abc/$count
BuildExpandedProperty(source, structuredType, propertyToExpand.Key, propertyToExpand.Value, includedProperties);
}
}
if (propertiesToInclude != null)
{
foreach (var propertyToInclude in propertiesToInclude)
{
// $select=abc($select=...,$filter=...,$compute=...)....
BuildSelectedProperty(source, structuredType, propertyToInclude.Key, propertyToInclude.Value, includedProperties);
}
}
if (autoSelectedProperties != null)
{
foreach (IEdmStructuralProperty propertyToInclude in autoSelectedProperties)
{
Expression propertyName = CreatePropertyNameExpression(structuredType, propertyToInclude, source);
Expression propertyValue = CreatePropertyValueExpression(structuredType, propertyToInclude, source, filterClause: null);
includedProperties.Add(new NamedPropertyExpression(propertyName, propertyValue)
{
AutoSelected = true
});
}
}
if (isSelectingOpenTypeSegments)
{
BuildDynamicProperty(source, structuredType, includedProperties);
}
// create a property container that holds all these property names and values.
return PropertyContainer.CreatePropertyContainer(includedProperties);
}
/// <summary>
/// Build the navigation property <see cref="IEdmNavigationProperty"/> into the included properties.
/// The property name is the navigation property name.
/// The property value is the navigation property value from the source and applied the nested query options.
/// </summary>
/// <param name="source">The source contains the navigation property.</param>
/// <param name="structuredType">The structured type or its derived type contains the navigation property.</param>
/// <param name="navigationProperty">The expanded navigation property.</param>
/// <param name="expandedItem">The expanded navigation select item. It may contain the neste query options.</param>
/// <param name="includedProperties">The container to hold the created property.</param>
internal void BuildExpandedProperty(Expression source, IEdmStructuredType structuredType,
IEdmNavigationProperty navigationProperty, ExpandedReferenceSelectItem expandedItem,
IList<NamedPropertyExpression> includedProperties)
{
Contract.Assert(source != null);
Contract.Assert(structuredType != null);
Contract.Assert(navigationProperty != null);
Contract.Assert(expandedItem != null);
Contract.Assert(includedProperties != null);
IEdmEntityType edmEntityType = navigationProperty.ToEntityType();
ModelBoundQuerySettings querySettings = EdmLibHelpers.GetModelBoundQuerySettings(navigationProperty, edmEntityType, _model);
// TODO: Process $apply and $compute in the $expand here, will support later.
// $apply=...; $compute=...
// Expression:
// "navigation property name"
Expression propertyName = CreatePropertyNameExpression(structuredType, navigationProperty, source);
// Expression:
// source.NavigationProperty
Expression propertyValue = CreatePropertyValueExpression(structuredType, navigationProperty, source, expandedItem.FilterOption);
// Sub select and expand could be null if the expanded navigation property is not further projected or expanded.
SelectExpandClause subSelectExpandClause = GetOrCreateSelectExpandClause(navigationProperty, expandedItem);
Expression nullCheck = GetNullCheckExpression(navigationProperty, propertyValue, subSelectExpandClause);
Expression countExpression = CreateTotalCountExpression(propertyValue, expandedItem.CountOption);
int? modelBoundPageSize = querySettings == null ? null : querySettings.PageSize;
if(expandedItem is ExpandedCountSelectItem)
{
Type elementType;
if (TypeHelper.IsCollection(propertyValue.Type, out elementType))
{
propertyValue = ExpressionHelpers.Count(propertyValue, elementType);
}
}
else
{
propertyValue = ProjectAsWrapper(propertyValue, subSelectExpandClause, edmEntityType, expandedItem.NavigationSource,
expandedItem.OrderByOption, // $orderby=...
expandedItem.TopOption, // $top=...
expandedItem.SkipOption, // $skip=...
modelBoundPageSize);
}
NamedPropertyExpression propertyExpression = new NamedPropertyExpression(propertyName, propertyValue);
if (subSelectExpandClause != null)
{
if (!navigationProperty.Type.IsCollection())
{
propertyExpression.NullCheck = nullCheck;
}
else if (_settings.PageSize.HasValue)
{
propertyExpression.PageSize = _settings.PageSize.Value;
}
else
{
if (querySettings != null && querySettings.PageSize.HasValue)
{
propertyExpression.PageSize = querySettings.PageSize.Value;
}
}
propertyExpression.TotalCount = countExpression;
propertyExpression.CountOption = expandedItem.CountOption;
}
includedProperties.Add(propertyExpression);
}
/// <summary>
/// Build the structural property <see cref="IEdmStructuralProperty"/> into the included properties.
/// The property name is the structural property name.
/// The property value is the structural property value from the source and applied the nested query options.
/// </summary>
/// <param name="source">The source contains the structural property.</param>
/// <param name="structuredType">The structured type or its derived type contains the structural property.</param>
/// <param name="structuralProperty">The selected structural property.</param>
/// <param name="pathSelectItem">The selected item. It may contain the neste query options and could be null.</param>
/// <param name="includedProperties">The container to hold the created property.</param>
internal void BuildSelectedProperty(Expression source, IEdmStructuredType structuredType,
IEdmStructuralProperty structuralProperty, PathSelectItem pathSelectItem,
IList<NamedPropertyExpression> includedProperties)
{
Contract.Assert(source != null);
Contract.Assert(structuredType != null);
Contract.Assert(structuralProperty != null);
Contract.Assert(includedProperties != null);
// // Expression:
// "navigation property name"
Expression propertyName = CreatePropertyNameExpression(structuredType, structuralProperty, source);
// Expression:
// source.NavigationProperty
Expression propertyValue;
if (pathSelectItem == null)
{
propertyValue = CreatePropertyValueExpression(structuredType, structuralProperty, source, filterClause: null);
includedProperties.Add(new NamedPropertyExpression(propertyName, propertyValue));
return;
}
SelectExpandClause subSelectExpandClause = pathSelectItem.SelectAndExpand;
// TODO: Process $compute in the $select ahead.
// $compute=...
propertyValue = CreatePropertyValueExpression(structuredType, structuralProperty, source, pathSelectItem.FilterOption);
Type propertyValueType = propertyValue.Type;
if (propertyValueType == typeof(char[]) || propertyValueType == typeof(byte[]))
{
includedProperties.Add(new NamedPropertyExpression(propertyName, propertyValue));
return;
}
// EF5 and EF6 don't support comparing complex objects to null, and will throw an exception similar to following if it is
// attempted:
// "Cannot compare elements of type 'xxxx'. Only primitive types, enumeration types and entity types are supported."
// EFCore has changed its implementation so the null check executes as expected.
Expression nullCheck = null;
if (_dataSourceProviderKind != DataSourceProviderKind.EFClassic)
{
nullCheck = GetNullCheckExpression(structuralProperty, propertyValue, subSelectExpandClause);
}
Expression countExpression = CreateTotalCountExpression(propertyValue, pathSelectItem.CountOption);
// be noted: the property structured type could be null, because the property maybe not a complex property.
IEdmStructuredType propertyStructuredType = structuralProperty.Type.ToStructuredType();
ModelBoundQuerySettings querySettings = null;
if (propertyStructuredType != null)
{
querySettings = EdmLibHelpers.GetModelBoundQuerySettings(structuralProperty, propertyStructuredType, _context.Model);
}
int? modelBoundPageSize = querySettings == null ? null : querySettings.PageSize;
propertyValue = ProjectAsWrapper(propertyValue, subSelectExpandClause, structuralProperty.Type.ToStructuredType(), pathSelectItem.NavigationSource,
pathSelectItem.OrderByOption, // $orderby=...
pathSelectItem.TopOption, // $top=...
pathSelectItem.SkipOption, // $skip=...
modelBoundPageSize);
NamedPropertyExpression propertyExpression = new NamedPropertyExpression(propertyName, propertyValue);
if (subSelectExpandClause != null)
{
if (!structuralProperty.Type.IsCollection())
{
propertyExpression.NullCheck = nullCheck;
}
else if (_settings.PageSize.HasValue)
{
propertyExpression.PageSize = _settings.PageSize.Value;
}
else
{
if (querySettings != null && querySettings.PageSize.HasValue)
{
propertyExpression.PageSize = querySettings.PageSize.Value;
}
}
propertyExpression.TotalCount = countExpression;
propertyExpression.CountOption = pathSelectItem.CountOption;
}
includedProperties.Add(propertyExpression);
}
/// <summary>
/// Build the dynamic properties into the included properties.
/// </summary>
/// <param name="source">The source contains the dynamic property.</param>
/// <param name="structuredType">The structured type contains the dynamic property.</param>
/// <param name="includedProperties">The container to hold the created property.</param>
internal void BuildDynamicProperty(Expression source, IEdmStructuredType structuredType,
IList<NamedPropertyExpression> includedProperties)
{
Contract.Assert(source != null);
Contract.Assert(structuredType != null);
Contract.Assert(includedProperties != null);
PropertyInfo dynamicPropertyDictionary = EdmLibHelpers.GetDynamicPropertyDictionary(structuredType, _model);
if (dynamicPropertyDictionary != null)
{
Expression propertyName = Expression.Constant(dynamicPropertyDictionary.Name);
Expression propertyValue = Expression.Property(source, dynamicPropertyDictionary.Name);
Expression nullablePropertyValue = ExpressionHelpers.ToNullable(propertyValue);
if (_settings.HandleNullPropagation == HandleNullPropagationOption.True)
{
// source == null ? null : propertyValue
propertyValue = Expression.Condition(
test: Expression.Equal(source, Expression.Constant(value: null)),
ifTrue: Expression.Constant(value: null, type: TypeHelper.ToNullable(propertyValue.Type)),
ifFalse: nullablePropertyValue);
}
else
{
propertyValue = nullablePropertyValue;
}
includedProperties.Add(new NamedPropertyExpression(propertyName, propertyValue));
}
}
private static SelectExpandClause GetOrCreateSelectExpandClause(IEdmNavigationProperty navigationProperty, ExpandedReferenceSelectItem expandedItem)
{
// for normal $expand=....
ExpandedNavigationSelectItem expandNavigationSelectItem = expandedItem as ExpandedNavigationSelectItem;
if (expandNavigationSelectItem != null)
{
return expandNavigationSelectItem.SelectAndExpand;
}
// for $expand=.../$count, return null since we cannot have a select/expand after $count segment
ExpandedCountSelectItem expandedCountSelectItem = expandedItem as ExpandedCountSelectItem;
if (expandedCountSelectItem != null)
{
return null;
}
// for $expand=..../$ref, just includes the keys properties
IList<SelectItem> selectItems = new List<SelectItem>();
foreach (IEdmStructuralProperty keyProperty in navigationProperty.ToEntityType().Key())
{
selectItems.Add(new PathSelectItem(new ODataSelectPath(new PropertySegment(keyProperty))));
}
return new SelectExpandClause(selectItems, false);
}
private Expression AddOrderByQueryForSource(Expression source, OrderByClause orderbyClause, Type elementType)
{
if (orderbyClause != null)
{
// TODO: Implement proper support for $select/$expand after $apply
ODataQuerySettings querySettings = new ODataQuerySettings()
{
HandleNullPropagation = HandleNullPropagationOption.True,
};
LambdaExpression orderByExpression = FilterBinder.Bind(null, orderbyClause, elementType, _context, querySettings);
source = ExpressionHelpers.OrderBy(source, orderByExpression, elementType, orderbyClause.Direction);
OrderByClause thenBy = orderbyClause.ThenBy;
while (thenBy != null)
{
orderByExpression = FilterBinder.Bind(null, thenBy, elementType, _context, querySettings);
source = ExpressionHelpers.OrderBy(source, orderByExpression, elementType, orderbyClause.Direction, true);
thenBy = thenBy.ThenBy;
}
}
return source;
}
private static Expression GetNullCheckExpression(IEdmStructuralProperty propertyToInclude, Expression propertyValue,
SelectExpandClause projection)
{
if (projection == null || propertyToInclude.Type.IsCollection())
{
return null;
}
if (IsSelectAll(projection) && propertyToInclude.Type.IsComplex())
{
// for Collections (Primitive, Enum, Complex collection), that's check above.
return Expression.Equal(propertyValue, Expression.Constant(null));
}
return null;
}
private Expression GetNullCheckExpression(IEdmNavigationProperty propertyToExpand, Expression propertyValue,
SelectExpandClause projection)
{
if (projection == null || propertyToExpand.Type.IsCollection())
{
return null;
}
if (IsSelectAll(projection) || !propertyToExpand.ToEntityType().Key().Any())
{
return Expression.Equal(propertyValue, Expression.Constant(null));
}
Expression keysNullCheckExpression = null;
foreach (var key in propertyToExpand.ToEntityType().Key())
{
var propertyValueExpression = CreatePropertyValueExpression(propertyToExpand.ToEntityType(), key, propertyValue, filterClause: null);
var keyExpression = Expression.Equal(
propertyValueExpression,
Expression.Constant(null, propertyValueExpression.Type));
keysNullCheckExpression = keysNullCheckExpression == null
? keyExpression
: Expression.And(keysNullCheckExpression, keyExpression);
}
return keysNullCheckExpression;
}
// new CollectionWrapper<ElementType> { Instance = source.Select((ElementType element) => new Wrapper { }) }
[SuppressMessage("Microsoft.Maintainability", "CA1502:AvoidExcessiveComplexity", Justification = "These are simple conversion function and cannot be split up.")]
private Expression ProjectCollection(Expression source, Type elementType,
SelectExpandClause selectExpandClause, IEdmStructuredType structuredType, IEdmNavigationSource navigationSource,
OrderByClause orderByClause,
long? topOption,
long? skipOption,
int? modelBoundPageSize)
{
// structuralType could be null, because it can be primitive collection.
ParameterExpression element = Expression.Parameter(elementType, "$it");
Expression projection;
// expression
// new Wrapper { }
if (structuredType != null)
{