From 66f772022efe15bdb93db70e05904e0646241138 Mon Sep 17 00:00:00 2001 From: Dilshod Komilov Date: Wed, 1 Sep 2021 15:46:21 +0500 Subject: [PATCH 1/9] add ctors --- .../Query/Expressions/SelectExpandBinder.cs | 19 +++++- .../Expressions/SelectExpandWrapperOfT.cs | 5 ++ .../Expressions/SelectExpandWrapperTest.cs | 62 +++++++++---------- .../ResourceContextTest.cs | 2 +- 4 files changed, 55 insertions(+), 33 deletions(-) diff --git a/src/Microsoft.AspNet.OData.Shared/Query/Expressions/SelectExpandBinder.cs b/src/Microsoft.AspNet.OData.Shared/Query/Expressions/SelectExpandBinder.cs index 1e83b186e5..35dcb40841 100644 --- a/src/Microsoft.AspNet.OData.Shared/Query/Expressions/SelectExpandBinder.cs +++ b/src/Microsoft.AspNet.OData.Shared/Query/Expressions/SelectExpandBinder.cs @@ -349,7 +349,8 @@ internal Expression ProjectElement(Expression source, SelectExpandClause selectE Type wrapperGenericType = GetWrapperGenericType(isInstancePropertySet, isTypeNamePropertySet, isContainerPropertySet); wrapperType = wrapperGenericType.MakeGenericType(elementType); - return Expression.MemberInit(Expression.New(wrapperType), wrapperTypeMemberAssignments); + ConstructorInfo constructorInfo = wrapperType.GetConstructors().Single(); + return Expression.MemberInit(Expression.New(constructorInfo, source), wrapperTypeMemberAssignments); } /// @@ -1243,18 +1244,34 @@ property selection combination possible. */ private class SelectAllAndExpand : SelectExpandWrapper { + public SelectAllAndExpand(TEntity entity) : base(entity) + { + + } } private class SelectAll : SelectExpandWrapper { + public SelectAll(TEntity entity) : base(entity) + { + + } } private class SelectSomeAndInheritance : SelectExpandWrapper { + public SelectSomeAndInheritance(TEntity entity) : base(entity) + { + + } } private class SelectSome : SelectAllAndExpand { + public SelectSome(TEntity entity) : base(entity) + { + + } } } } diff --git a/src/Microsoft.AspNet.OData.Shared/Query/Expressions/SelectExpandWrapperOfT.cs b/src/Microsoft.AspNet.OData.Shared/Query/Expressions/SelectExpandWrapperOfT.cs index d06afd4d55..bb59257dc2 100644 --- a/src/Microsoft.AspNet.OData.Shared/Query/Expressions/SelectExpandWrapperOfT.cs +++ b/src/Microsoft.AspNet.OData.Shared/Query/Expressions/SelectExpandWrapperOfT.cs @@ -17,6 +17,11 @@ namespace Microsoft.AspNet.OData.Query.Expressions [JsonConverter(typeof(SelectExpandWrapperConverter))] internal class SelectExpandWrapper : SelectExpandWrapper { + public SelectExpandWrapper(TElement element) + { + Instance = element; + } + /// /// Gets or sets the instance of the element being selected and expanded. /// diff --git a/test/UnitTest/Microsoft.AspNet.OData.Test.Shared/Query/Expressions/SelectExpandWrapperTest.cs b/test/UnitTest/Microsoft.AspNet.OData.Test.Shared/Query/Expressions/SelectExpandWrapperTest.cs index c2ff968ce6..272d885084 100644 --- a/test/UnitTest/Microsoft.AspNet.OData.Test.Shared/Query/Expressions/SelectExpandWrapperTest.cs +++ b/test/UnitTest/Microsoft.AspNet.OData.Test.Shared/Query/Expressions/SelectExpandWrapperTest.cs @@ -31,14 +31,14 @@ public SelectExpandWrapperTest() [Fact] public void Property_Instance_RoundTrips() { - SelectExpandWrapper wrapper = new SelectExpandWrapper(); + SelectExpandWrapper wrapper = new SelectExpandWrapper(null); ReflectionAssert.Property(wrapper, w => w.Instance, expectedDefaultValue: null, allowNull: true, roundTripTestValue: new TestEntity()); } [Fact] public void Property_Container_RoundTrips() { - SelectExpandWrapper wrapper = new SelectExpandWrapper(); + SelectExpandWrapper wrapper = new SelectExpandWrapper(null); ReflectionAssert.Property( wrapper, w => w.Container, expectedDefaultValue: null, allowNull: true, roundTripTestValue: new MockPropertyContainer()); @@ -49,7 +49,7 @@ public void GetEdmType_Returns_InstanceType() { _model.Model.SetAnnotationValue(_model.Customer, new ClrTypeAnnotation(typeof(TestEntity))); _model.Model.SetAnnotationValue(_model.SpecialCustomer, new ClrTypeAnnotation(typeof(DerivedEntity))); - SelectExpandWrapper wrapper = new SelectExpandWrapper { ModelID = _modelID }; + SelectExpandWrapper wrapper = new SelectExpandWrapper(null) { ModelID = _modelID }; wrapper.Instance = new DerivedEntity(); IEdmTypeReference edmType = wrapper.GetEdmType(); @@ -62,7 +62,7 @@ public void GetEdmType_Returns_ElementTypeIfInstanceIsNull() { _model.Model.SetAnnotationValue(_model.Customer, new ClrTypeAnnotation(typeof(TestEntity))); _model.Model.SetAnnotationValue(_model.SpecialCustomer, new ClrTypeAnnotation(typeof(DerivedEntity))); - SelectExpandWrapper wrapper = new SelectExpandWrapper { ModelID = _modelID }; + SelectExpandWrapper wrapper = new SelectExpandWrapper(null) { ModelID = _modelID }; IEdmTypeReference edmType = wrapper.GetEdmType(); @@ -75,11 +75,11 @@ public void TryGetValue_ReturnsValueFromPropertyContainer_IfPresent() object expectedPropertyValue = new object(); MockPropertyContainer container = new MockPropertyContainer(); container.Properties.Add("SampleProperty", expectedPropertyValue); - SelectExpandWrapper wrapper = new SelectExpandWrapper - { - ModelID = _modelID, - Container = container - }; + SelectExpandWrapper wrapper = new SelectExpandWrapper(null) + { + ModelID = _modelID, + Container = container + }; wrapper.Instance = new TestEntity(); object value; @@ -94,11 +94,11 @@ public void TryGetValue_ReturnsValueFromInstance_IfNotPresentInContainer() { object expectedPropertyValue = new object(); MockPropertyContainer container = new MockPropertyContainer(); - SelectExpandWrapper wrapper = new SelectExpandWrapper - { - ModelID = _modelID, - Container = container - }; + SelectExpandWrapper wrapper = new SelectExpandWrapper(null) + { + ModelID = _modelID, + Container = container + }; wrapper.Instance = new TestEntity { SampleProperty = expectedPropertyValue }; wrapper.UseInstanceForProperties = true; @@ -113,7 +113,7 @@ public void TryGetValue_ReturnsValueFromInstance_IfNotPresentInContainer() public void TryGetValue_ReturnsValueFromInstance_IfContainerIsNull() { object expectedPropertyValue = new object(); - SelectExpandWrapper wrapper = new SelectExpandWrapper { ModelID = _modelID }; + SelectExpandWrapper wrapper = new SelectExpandWrapper(null) { ModelID = _modelID }; wrapper.Instance = new TestEntity { SampleProperty = expectedPropertyValue }; wrapper.UseInstanceForProperties = true; @@ -133,7 +133,7 @@ public void TryGetValue_PropertyAliased_IfAnnotationSet() _model.CustomerName, new ClrPropertyInfoAnnotation(typeof(TestEntityWithAlias).GetProperty("SampleProperty"))); object expectedPropertyValue = new object(); - SelectExpandWrapper wrapper = new SelectExpandWrapper { ModelID = _modelID }; + SelectExpandWrapper wrapper = new SelectExpandWrapper(null) { ModelID = _modelID }; wrapper.Instance = new TestEntityWithAlias { SampleProperty = expectedPropertyValue }; wrapper.UseInstanceForProperties = true; @@ -149,7 +149,7 @@ public void TryGetValue_PropertyAliased_IfAnnotationSet() [Fact] public void TryGetValue_ReturnsFalse_IfContainerAndInstanceAreNull() { - SelectExpandWrapper wrapper = new SelectExpandWrapper { ModelID = _modelID }; + SelectExpandWrapper wrapper = new SelectExpandWrapper(null) { ModelID = _modelID }; object value; bool result = wrapper.TryGetPropertyValue("SampleProperty", out value); @@ -160,7 +160,7 @@ public void TryGetValue_ReturnsFalse_IfContainerAndInstanceAreNull() [Fact] public void TryGetValue_ReturnsFalse_IfPropertyNotPresentInElement() { - SelectExpandWrapper wrapper = new SelectExpandWrapper { ModelID = _modelID }; + SelectExpandWrapper wrapper = new SelectExpandWrapper(null) { ModelID = _modelID }; object value; bool result = wrapper.TryGetPropertyValue("SampleNotPresentProperty", out value); @@ -178,7 +178,7 @@ public void ToDictionary_ContainsAllStructuralProperties_IfInstanceIsNotNull() model.SetAnnotationValue(entityType, new ClrTypeAnnotation(typeof(TestEntity))); entityType.AddStructuralProperty("SampleProperty", EdmPrimitiveTypeKind.Int32); IEdmTypeReference edmType = new EdmEntityTypeReference(entityType, isNullable: false); - SelectExpandWrapper testWrapper = new SelectExpandWrapper + SelectExpandWrapper testWrapper = new SelectExpandWrapper(null) { Instance = new TestEntity { SampleProperty = 42 }, ModelID = ModelContainer.GetModelID(model), @@ -203,7 +203,7 @@ public void ToDictionary_ContainsAllProperties_FromContainer() entityType.AddStructuralProperty("SampleProperty", EdmPrimitiveTypeKind.Int32); MockPropertyContainer container = new MockPropertyContainer(); container.Properties.Add("Property", 42); - SelectExpandWrapper wrapper = new SelectExpandWrapper + SelectExpandWrapper wrapper = new SelectExpandWrapper(null) { Container = container, ModelID = ModelContainer.GetModelID(model) @@ -220,7 +220,7 @@ public void ToDictionary_ContainsAllProperties_FromContainer() public void ToDictionary_Throws_IfMapperProviderIsNull() { // Arrange - SelectExpandWrapper wrapper = new SelectExpandWrapper(); + SelectExpandWrapper wrapper = new SelectExpandWrapper(null); // Act & Assert ExceptionAssert.Throws(() => wrapper.ToDictionary(mapperProvider: null)); @@ -238,7 +238,7 @@ public void ToDictionary_Throws_IfMapperProvider_ReturnsNullPropertyMapper() model.SetAnnotationValue(entityType, new ClrTypeAnnotation(typeof(TestEntity))); IEdmTypeReference edmType = new EdmEntityTypeReference(entityType, isNullable: false); - SelectExpandWrapper wrapper = new SelectExpandWrapper + SelectExpandWrapper wrapper = new SelectExpandWrapper(null) { Instance = new TestEntity { SampleProperty = 42 }, ModelID = ModelContainer.GetModelID(model) @@ -248,7 +248,7 @@ public void ToDictionary_Throws_IfMapperProvider_ReturnsNullPropertyMapper() (IEdmModel m, IEdmStructuredType t) => null; // Act & Assert - ExceptionAssert.Throws(() => + ExceptionAssert.Throws(() => wrapper.ToDictionary(mapperProvider: mapperProvider), "The mapper provider must return a valid 'Microsoft.AspNet.OData.Query.IPropertyMapper' instance for the given 'NS.Name' IEdmType."); } @@ -261,13 +261,13 @@ public void ToDictionary_Throws_IfMappingIsNullOrEmpty_ForAGivenProperty(string // Arrange EdmEntityType entityType = new EdmEntityType("NS", "Name"); entityType.AddStructuralProperty("SampleProperty", EdmPrimitiveTypeKind.Int32); - + EdmModel model = new EdmModel(); model.AddElement(entityType); model.SetAnnotationValue(entityType, new ClrTypeAnnotation(typeof(TestEntity))); IEdmTypeReference edmType = new EdmEntityTypeReference(entityType, isNullable: false); - - SelectExpandWrapper testWrapper = new SelectExpandWrapper + + SelectExpandWrapper testWrapper = new SelectExpandWrapper(null) { Instance = new TestEntity { SampleProperty = 42 }, ModelID = ModelContainer.GetModelID(model), @@ -281,7 +281,7 @@ public void ToDictionary_Throws_IfMappingIsNullOrEmpty_ForAGivenProperty(string // Act & Assert ExceptionAssert.Throws(() => - testWrapper.ToDictionary(mapperProvider), + testWrapper.ToDictionary(mapperProvider), "The key mapping for the property 'SampleProperty' can't be null or empty."); } @@ -291,19 +291,19 @@ public void ToDictionary_AppliesMappingToAllProperties_IfInstanceIsNotNull() // Arrange EdmEntityType entityType = new EdmEntityType("NS", "Name"); entityType.AddStructuralProperty("SampleProperty", EdmPrimitiveTypeKind.Int32); - + EdmModel model = new EdmModel(); model.AddElement(entityType); model.SetAnnotationValue(entityType, new ClrTypeAnnotation(typeof(TestEntity))); IEdmTypeReference edmType = new EdmEntityTypeReference(entityType, isNullable: false); - - SelectExpandWrapper testWrapper = new SelectExpandWrapper + + SelectExpandWrapper testWrapper = new SelectExpandWrapper(null) { Instance = new TestEntity { SampleProperty = 42 }, ModelID = ModelContainer.GetModelID(model), UseInstanceForProperties = true, }; - + Mock mapperMock = new Mock(); mapperMock.Setup(m => m.MapProperty("SampleProperty")).Returns("Sample"); Func mapperProvider = diff --git a/test/UnitTest/Microsoft.AspNet.OData.Test.Shared/ResourceContextTest.cs b/test/UnitTest/Microsoft.AspNet.OData.Test.Shared/ResourceContextTest.cs index 214d88010d..d0ae25bfd3 100644 --- a/test/UnitTest/Microsoft.AspNet.OData.Test.Shared/ResourceContextTest.cs +++ b/test/UnitTest/Microsoft.AspNet.OData.Test.Shared/ResourceContextTest.cs @@ -300,7 +300,7 @@ public void Property_ResourceInstance_CanBeBuiltWithSelectExpandWrapperPropertie model.SetAnnotationValue(edmType, new ClrTypeAnnotation(typeof(TestSubEntity))); Mock edmObject = new Mock(); object propertyValue = 42; - object selectExpandWrapper = new SelectExpandWrapper(); + object selectExpandWrapper = new SelectExpandWrapper(null); object subEntity2 = new TestSubEntity { Property = 33 From c85d30fd3828c731a2b25bdeb98a099146a7bd26 Mon Sep 17 00:00:00 2001 From: Dilshod Komilov Date: Wed, 1 Sep 2021 16:49:22 +0500 Subject: [PATCH 2/9] change behaviors --- .../Query/Expressions/SelectExpandBinderTest.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/UnitTest/Microsoft.AspNet.OData.Test.Shared/Query/Expressions/SelectExpandBinderTest.cs b/test/UnitTest/Microsoft.AspNet.OData.Test.Shared/Query/Expressions/SelectExpandBinderTest.cs index b360b48ed5..b5a298b328 100644 --- a/test/UnitTest/Microsoft.AspNet.OData.Test.Shared/Query/Expressions/SelectExpandBinderTest.cs +++ b/test/UnitTest/Microsoft.AspNet.OData.Test.Shared/Query/Expressions/SelectExpandBinderTest.cs @@ -112,7 +112,7 @@ public void Bind_GeneratedExpression_ContainsExpandedObject() Assert.True(enumerator.MoveNext()); var partialCustomer = Assert.IsAssignableFrom>(enumerator.Current); Assert.False(enumerator.MoveNext()); - Assert.Null(partialCustomer.Instance); + Assert.NotNull(partialCustomer.Instance); Assert.Equal("Microsoft.AspNet.OData.Test.Query.Expressions.QueryCustomer", partialCustomer.InstanceType); IEnumerable> innerOrders = partialCustomer.Container .ToDictionary(PropertyMapper)["Orders"] as IEnumerable>; @@ -486,7 +486,7 @@ public void ProjectAsWrapper_Element_ProjectedValueDoesNotContainInstance_IfSele Assert.Empty((projection as MemberInitExpression).Bindings.Where(p => p.Member.Name == "Instance")); Assert.NotEmpty((projection as MemberInitExpression).Bindings.Where(p => p.Member.Name == "InstanceType")); SelectExpandWrapper customerWrapper = Expression.Lambda(projection).Compile().DynamicInvoke() as SelectExpandWrapper; - Assert.Null(customerWrapper.Instance); + Assert.NotNull(customerWrapper.Instance); Assert.Equal("Microsoft.AspNet.OData.Test.Query.Expressions.QueryCustomer", customerWrapper.InstanceType); } From a1855f0b1a3a0976d80e482c705c841128f67429 Mon Sep 17 00:00:00 2001 From: Dilshod Komilov Date: Thu, 2 Sep 2021 08:59:59 +0500 Subject: [PATCH 3/9] create one more constructor --- .../Query/Expressions/SelectExpandBinder.cs | 20 ++++++++++++ .../Expressions/SelectExpandWrapperOfT.cs | 5 +++ .../Expressions/SelectExpandBinderTest.cs | 6 ++-- .../Expressions/SelectExpandWrapperTest.cs | 32 +++++++++---------- .../ResourceContextTest.cs | 2 +- 5 files changed, 45 insertions(+), 20 deletions(-) diff --git a/src/Microsoft.AspNet.OData.Shared/Query/Expressions/SelectExpandBinder.cs b/src/Microsoft.AspNet.OData.Shared/Query/Expressions/SelectExpandBinder.cs index 35dcb40841..790c3fd6cf 100644 --- a/src/Microsoft.AspNet.OData.Shared/Query/Expressions/SelectExpandBinder.cs +++ b/src/Microsoft.AspNet.OData.Shared/Query/Expressions/SelectExpandBinder.cs @@ -1248,6 +1248,11 @@ public SelectAllAndExpand(TEntity entity) : base(entity) { } + + public SelectAllAndExpand() + { + + } } private class SelectAll : SelectExpandWrapper @@ -1256,6 +1261,11 @@ public SelectAll(TEntity entity) : base(entity) { } + + public SelectAll() + { + + } } private class SelectSomeAndInheritance : SelectExpandWrapper @@ -1264,6 +1274,11 @@ public SelectSomeAndInheritance(TEntity entity) : base(entity) { } + + public SelectSomeAndInheritance() + { + + } } private class SelectSome : SelectAllAndExpand @@ -1272,6 +1287,11 @@ public SelectSome(TEntity entity) : base(entity) { } + + public SelectSome() + { + + } } } } diff --git a/src/Microsoft.AspNet.OData.Shared/Query/Expressions/SelectExpandWrapperOfT.cs b/src/Microsoft.AspNet.OData.Shared/Query/Expressions/SelectExpandWrapperOfT.cs index bb59257dc2..9fa9714797 100644 --- a/src/Microsoft.AspNet.OData.Shared/Query/Expressions/SelectExpandWrapperOfT.cs +++ b/src/Microsoft.AspNet.OData.Shared/Query/Expressions/SelectExpandWrapperOfT.cs @@ -22,6 +22,11 @@ public SelectExpandWrapper(TElement element) Instance = element; } + public SelectExpandWrapper() + { + + } + /// /// Gets or sets the instance of the element being selected and expanded. /// diff --git a/test/UnitTest/Microsoft.AspNet.OData.Test.Shared/Query/Expressions/SelectExpandBinderTest.cs b/test/UnitTest/Microsoft.AspNet.OData.Test.Shared/Query/Expressions/SelectExpandBinderTest.cs index b5a298b328..5361d9be3c 100644 --- a/test/UnitTest/Microsoft.AspNet.OData.Test.Shared/Query/Expressions/SelectExpandBinderTest.cs +++ b/test/UnitTest/Microsoft.AspNet.OData.Test.Shared/Query/Expressions/SelectExpandBinderTest.cs @@ -112,7 +112,7 @@ public void Bind_GeneratedExpression_ContainsExpandedObject() Assert.True(enumerator.MoveNext()); var partialCustomer = Assert.IsAssignableFrom>(enumerator.Current); Assert.False(enumerator.MoveNext()); - Assert.NotNull(partialCustomer.Instance); + Assert.Null(partialCustomer.Instance); Assert.Equal("Microsoft.AspNet.OData.Test.Query.Expressions.QueryCustomer", partialCustomer.InstanceType); IEnumerable> innerOrders = partialCustomer.Container .ToDictionary(PropertyMapper)["Orders"] as IEnumerable>; @@ -476,7 +476,7 @@ public void ProjectAsWrapper_Element_ProjectedValueDoesNotContainInstance_IfSele Expression source = Expression.Constant(aCustomer); SelectExpandClause selectExpandClause = ParseSelectExpand(select, expand, _model, _customer, _customers); - Assert.NotNull(selectExpandClause); + Assert.Null(selectExpandClause); // Act Expression projection = _binder.ProjectAsWrapper(source, selectExpandClause, _customer, _customers); @@ -486,7 +486,7 @@ public void ProjectAsWrapper_Element_ProjectedValueDoesNotContainInstance_IfSele Assert.Empty((projection as MemberInitExpression).Bindings.Where(p => p.Member.Name == "Instance")); Assert.NotEmpty((projection as MemberInitExpression).Bindings.Where(p => p.Member.Name == "InstanceType")); SelectExpandWrapper customerWrapper = Expression.Lambda(projection).Compile().DynamicInvoke() as SelectExpandWrapper; - Assert.NotNull(customerWrapper.Instance); + Assert.Null(customerWrapper.Instance); Assert.Equal("Microsoft.AspNet.OData.Test.Query.Expressions.QueryCustomer", customerWrapper.InstanceType); } diff --git a/test/UnitTest/Microsoft.AspNet.OData.Test.Shared/Query/Expressions/SelectExpandWrapperTest.cs b/test/UnitTest/Microsoft.AspNet.OData.Test.Shared/Query/Expressions/SelectExpandWrapperTest.cs index 272d885084..afb47bab9c 100644 --- a/test/UnitTest/Microsoft.AspNet.OData.Test.Shared/Query/Expressions/SelectExpandWrapperTest.cs +++ b/test/UnitTest/Microsoft.AspNet.OData.Test.Shared/Query/Expressions/SelectExpandWrapperTest.cs @@ -31,14 +31,14 @@ public SelectExpandWrapperTest() [Fact] public void Property_Instance_RoundTrips() { - SelectExpandWrapper wrapper = new SelectExpandWrapper(null); + SelectExpandWrapper wrapper = new SelectExpandWrapper(); ReflectionAssert.Property(wrapper, w => w.Instance, expectedDefaultValue: null, allowNull: true, roundTripTestValue: new TestEntity()); } [Fact] public void Property_Container_RoundTrips() { - SelectExpandWrapper wrapper = new SelectExpandWrapper(null); + SelectExpandWrapper wrapper = new SelectExpandWrapper(); ReflectionAssert.Property( wrapper, w => w.Container, expectedDefaultValue: null, allowNull: true, roundTripTestValue: new MockPropertyContainer()); @@ -49,7 +49,7 @@ public void GetEdmType_Returns_InstanceType() { _model.Model.SetAnnotationValue(_model.Customer, new ClrTypeAnnotation(typeof(TestEntity))); _model.Model.SetAnnotationValue(_model.SpecialCustomer, new ClrTypeAnnotation(typeof(DerivedEntity))); - SelectExpandWrapper wrapper = new SelectExpandWrapper(null) { ModelID = _modelID }; + SelectExpandWrapper wrapper = new SelectExpandWrapper() { ModelID = _modelID }; wrapper.Instance = new DerivedEntity(); IEdmTypeReference edmType = wrapper.GetEdmType(); @@ -62,7 +62,7 @@ public void GetEdmType_Returns_ElementTypeIfInstanceIsNull() { _model.Model.SetAnnotationValue(_model.Customer, new ClrTypeAnnotation(typeof(TestEntity))); _model.Model.SetAnnotationValue(_model.SpecialCustomer, new ClrTypeAnnotation(typeof(DerivedEntity))); - SelectExpandWrapper wrapper = new SelectExpandWrapper(null) { ModelID = _modelID }; + SelectExpandWrapper wrapper = new SelectExpandWrapper() { ModelID = _modelID }; IEdmTypeReference edmType = wrapper.GetEdmType(); @@ -75,7 +75,7 @@ public void TryGetValue_ReturnsValueFromPropertyContainer_IfPresent() object expectedPropertyValue = new object(); MockPropertyContainer container = new MockPropertyContainer(); container.Properties.Add("SampleProperty", expectedPropertyValue); - SelectExpandWrapper wrapper = new SelectExpandWrapper(null) + SelectExpandWrapper wrapper = new SelectExpandWrapper() { ModelID = _modelID, Container = container @@ -94,7 +94,7 @@ public void TryGetValue_ReturnsValueFromInstance_IfNotPresentInContainer() { object expectedPropertyValue = new object(); MockPropertyContainer container = new MockPropertyContainer(); - SelectExpandWrapper wrapper = new SelectExpandWrapper(null) + SelectExpandWrapper wrapper = new SelectExpandWrapper() { ModelID = _modelID, Container = container @@ -113,7 +113,7 @@ public void TryGetValue_ReturnsValueFromInstance_IfNotPresentInContainer() public void TryGetValue_ReturnsValueFromInstance_IfContainerIsNull() { object expectedPropertyValue = new object(); - SelectExpandWrapper wrapper = new SelectExpandWrapper(null) { ModelID = _modelID }; + SelectExpandWrapper wrapper = new SelectExpandWrapper() { ModelID = _modelID }; wrapper.Instance = new TestEntity { SampleProperty = expectedPropertyValue }; wrapper.UseInstanceForProperties = true; @@ -133,7 +133,7 @@ public void TryGetValue_PropertyAliased_IfAnnotationSet() _model.CustomerName, new ClrPropertyInfoAnnotation(typeof(TestEntityWithAlias).GetProperty("SampleProperty"))); object expectedPropertyValue = new object(); - SelectExpandWrapper wrapper = new SelectExpandWrapper(null) { ModelID = _modelID }; + SelectExpandWrapper wrapper = new SelectExpandWrapper() { ModelID = _modelID }; wrapper.Instance = new TestEntityWithAlias { SampleProperty = expectedPropertyValue }; wrapper.UseInstanceForProperties = true; @@ -149,7 +149,7 @@ public void TryGetValue_PropertyAliased_IfAnnotationSet() [Fact] public void TryGetValue_ReturnsFalse_IfContainerAndInstanceAreNull() { - SelectExpandWrapper wrapper = new SelectExpandWrapper(null) { ModelID = _modelID }; + SelectExpandWrapper wrapper = new SelectExpandWrapper() { ModelID = _modelID }; object value; bool result = wrapper.TryGetPropertyValue("SampleProperty", out value); @@ -160,7 +160,7 @@ public void TryGetValue_ReturnsFalse_IfContainerAndInstanceAreNull() [Fact] public void TryGetValue_ReturnsFalse_IfPropertyNotPresentInElement() { - SelectExpandWrapper wrapper = new SelectExpandWrapper(null) { ModelID = _modelID }; + SelectExpandWrapper wrapper = new SelectExpandWrapper() { ModelID = _modelID }; object value; bool result = wrapper.TryGetPropertyValue("SampleNotPresentProperty", out value); @@ -178,7 +178,7 @@ public void ToDictionary_ContainsAllStructuralProperties_IfInstanceIsNotNull() model.SetAnnotationValue(entityType, new ClrTypeAnnotation(typeof(TestEntity))); entityType.AddStructuralProperty("SampleProperty", EdmPrimitiveTypeKind.Int32); IEdmTypeReference edmType = new EdmEntityTypeReference(entityType, isNullable: false); - SelectExpandWrapper testWrapper = new SelectExpandWrapper(null) + SelectExpandWrapper testWrapper = new SelectExpandWrapper() { Instance = new TestEntity { SampleProperty = 42 }, ModelID = ModelContainer.GetModelID(model), @@ -203,7 +203,7 @@ public void ToDictionary_ContainsAllProperties_FromContainer() entityType.AddStructuralProperty("SampleProperty", EdmPrimitiveTypeKind.Int32); MockPropertyContainer container = new MockPropertyContainer(); container.Properties.Add("Property", 42); - SelectExpandWrapper wrapper = new SelectExpandWrapper(null) + SelectExpandWrapper wrapper = new SelectExpandWrapper() { Container = container, ModelID = ModelContainer.GetModelID(model) @@ -220,7 +220,7 @@ public void ToDictionary_ContainsAllProperties_FromContainer() public void ToDictionary_Throws_IfMapperProviderIsNull() { // Arrange - SelectExpandWrapper wrapper = new SelectExpandWrapper(null); + SelectExpandWrapper wrapper = new SelectExpandWrapper(); // Act & Assert ExceptionAssert.Throws(() => wrapper.ToDictionary(mapperProvider: null)); @@ -238,7 +238,7 @@ public void ToDictionary_Throws_IfMapperProvider_ReturnsNullPropertyMapper() model.SetAnnotationValue(entityType, new ClrTypeAnnotation(typeof(TestEntity))); IEdmTypeReference edmType = new EdmEntityTypeReference(entityType, isNullable: false); - SelectExpandWrapper wrapper = new SelectExpandWrapper(null) + SelectExpandWrapper wrapper = new SelectExpandWrapper() { Instance = new TestEntity { SampleProperty = 42 }, ModelID = ModelContainer.GetModelID(model) @@ -267,7 +267,7 @@ public void ToDictionary_Throws_IfMappingIsNullOrEmpty_ForAGivenProperty(string model.SetAnnotationValue(entityType, new ClrTypeAnnotation(typeof(TestEntity))); IEdmTypeReference edmType = new EdmEntityTypeReference(entityType, isNullable: false); - SelectExpandWrapper testWrapper = new SelectExpandWrapper(null) + SelectExpandWrapper testWrapper = new SelectExpandWrapper() { Instance = new TestEntity { SampleProperty = 42 }, ModelID = ModelContainer.GetModelID(model), @@ -297,7 +297,7 @@ public void ToDictionary_AppliesMappingToAllProperties_IfInstanceIsNotNull() model.SetAnnotationValue(entityType, new ClrTypeAnnotation(typeof(TestEntity))); IEdmTypeReference edmType = new EdmEntityTypeReference(entityType, isNullable: false); - SelectExpandWrapper testWrapper = new SelectExpandWrapper(null) + SelectExpandWrapper testWrapper = new SelectExpandWrapper() { Instance = new TestEntity { SampleProperty = 42 }, ModelID = ModelContainer.GetModelID(model), diff --git a/test/UnitTest/Microsoft.AspNet.OData.Test.Shared/ResourceContextTest.cs b/test/UnitTest/Microsoft.AspNet.OData.Test.Shared/ResourceContextTest.cs index d0ae25bfd3..214d88010d 100644 --- a/test/UnitTest/Microsoft.AspNet.OData.Test.Shared/ResourceContextTest.cs +++ b/test/UnitTest/Microsoft.AspNet.OData.Test.Shared/ResourceContextTest.cs @@ -300,7 +300,7 @@ public void Property_ResourceInstance_CanBeBuiltWithSelectExpandWrapperPropertie model.SetAnnotationValue(edmType, new ClrTypeAnnotation(typeof(TestSubEntity))); Mock edmObject = new Mock(); object propertyValue = 42; - object selectExpandWrapper = new SelectExpandWrapper(null); + object selectExpandWrapper = new SelectExpandWrapper(); object subEntity2 = new TestSubEntity { Property = 33 From ed8c5615751841924f475348f2aabcd27f96178f Mon Sep 17 00:00:00 2001 From: Dilshod Komilov Date: Thu, 2 Sep 2021 09:01:40 +0500 Subject: [PATCH 4/9] run with parameterless constructor --- .../Query/Expressions/SelectExpandBinder.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Microsoft.AspNet.OData.Shared/Query/Expressions/SelectExpandBinder.cs b/src/Microsoft.AspNet.OData.Shared/Query/Expressions/SelectExpandBinder.cs index 790c3fd6cf..1c867ccc46 100644 --- a/src/Microsoft.AspNet.OData.Shared/Query/Expressions/SelectExpandBinder.cs +++ b/src/Microsoft.AspNet.OData.Shared/Query/Expressions/SelectExpandBinder.cs @@ -349,7 +349,7 @@ internal Expression ProjectElement(Expression source, SelectExpandClause selectE Type wrapperGenericType = GetWrapperGenericType(isInstancePropertySet, isTypeNamePropertySet, isContainerPropertySet); wrapperType = wrapperGenericType.MakeGenericType(elementType); - ConstructorInfo constructorInfo = wrapperType.GetConstructors().Single(); + ConstructorInfo constructorInfo = wrapperType.GetConstructors().Single(c => c.GetParameters().Length == 1); return Expression.MemberInit(Expression.New(constructorInfo, source), wrapperTypeMemberAssignments); } From 5aee3d040c838e8a420ba9d964f8e8f08cff4281 Mon Sep 17 00:00:00 2001 From: Dilshod Komilov Date: Thu, 2 Sep 2021 09:04:08 +0500 Subject: [PATCH 5/9] undo changes --- .../Expressions/SelectExpandBinderTest.cs | 2 +- .../Expressions/SelectExpandWrapperTest.cs | 24 +++++++++---------- 2 files changed, 13 insertions(+), 13 deletions(-) diff --git a/test/UnitTest/Microsoft.AspNet.OData.Test.Shared/Query/Expressions/SelectExpandBinderTest.cs b/test/UnitTest/Microsoft.AspNet.OData.Test.Shared/Query/Expressions/SelectExpandBinderTest.cs index 5361d9be3c..b360b48ed5 100644 --- a/test/UnitTest/Microsoft.AspNet.OData.Test.Shared/Query/Expressions/SelectExpandBinderTest.cs +++ b/test/UnitTest/Microsoft.AspNet.OData.Test.Shared/Query/Expressions/SelectExpandBinderTest.cs @@ -476,7 +476,7 @@ public void ProjectAsWrapper_Element_ProjectedValueDoesNotContainInstance_IfSele Expression source = Expression.Constant(aCustomer); SelectExpandClause selectExpandClause = ParseSelectExpand(select, expand, _model, _customer, _customers); - Assert.Null(selectExpandClause); + Assert.NotNull(selectExpandClause); // Act Expression projection = _binder.ProjectAsWrapper(source, selectExpandClause, _customer, _customers); diff --git a/test/UnitTest/Microsoft.AspNet.OData.Test.Shared/Query/Expressions/SelectExpandWrapperTest.cs b/test/UnitTest/Microsoft.AspNet.OData.Test.Shared/Query/Expressions/SelectExpandWrapperTest.cs index afb47bab9c..0892162fac 100644 --- a/test/UnitTest/Microsoft.AspNet.OData.Test.Shared/Query/Expressions/SelectExpandWrapperTest.cs +++ b/test/UnitTest/Microsoft.AspNet.OData.Test.Shared/Query/Expressions/SelectExpandWrapperTest.cs @@ -49,7 +49,7 @@ public void GetEdmType_Returns_InstanceType() { _model.Model.SetAnnotationValue(_model.Customer, new ClrTypeAnnotation(typeof(TestEntity))); _model.Model.SetAnnotationValue(_model.SpecialCustomer, new ClrTypeAnnotation(typeof(DerivedEntity))); - SelectExpandWrapper wrapper = new SelectExpandWrapper() { ModelID = _modelID }; + SelectExpandWrapper wrapper = new SelectExpandWrapper { ModelID = _modelID }; wrapper.Instance = new DerivedEntity(); IEdmTypeReference edmType = wrapper.GetEdmType(); @@ -62,7 +62,7 @@ public void GetEdmType_Returns_ElementTypeIfInstanceIsNull() { _model.Model.SetAnnotationValue(_model.Customer, new ClrTypeAnnotation(typeof(TestEntity))); _model.Model.SetAnnotationValue(_model.SpecialCustomer, new ClrTypeAnnotation(typeof(DerivedEntity))); - SelectExpandWrapper wrapper = new SelectExpandWrapper() { ModelID = _modelID }; + SelectExpandWrapper wrapper = new SelectExpandWrapper { ModelID = _modelID }; IEdmTypeReference edmType = wrapper.GetEdmType(); @@ -75,7 +75,7 @@ public void TryGetValue_ReturnsValueFromPropertyContainer_IfPresent() object expectedPropertyValue = new object(); MockPropertyContainer container = new MockPropertyContainer(); container.Properties.Add("SampleProperty", expectedPropertyValue); - SelectExpandWrapper wrapper = new SelectExpandWrapper() + SelectExpandWrapper wrapper = new SelectExpandWrapper { ModelID = _modelID, Container = container @@ -94,7 +94,7 @@ public void TryGetValue_ReturnsValueFromInstance_IfNotPresentInContainer() { object expectedPropertyValue = new object(); MockPropertyContainer container = new MockPropertyContainer(); - SelectExpandWrapper wrapper = new SelectExpandWrapper() + SelectExpandWrapper wrapper = new SelectExpandWrapper { ModelID = _modelID, Container = container @@ -113,7 +113,7 @@ public void TryGetValue_ReturnsValueFromInstance_IfNotPresentInContainer() public void TryGetValue_ReturnsValueFromInstance_IfContainerIsNull() { object expectedPropertyValue = new object(); - SelectExpandWrapper wrapper = new SelectExpandWrapper() { ModelID = _modelID }; + SelectExpandWrapper wrapper = new SelectExpandWrapper { ModelID = _modelID }; wrapper.Instance = new TestEntity { SampleProperty = expectedPropertyValue }; wrapper.UseInstanceForProperties = true; @@ -149,7 +149,7 @@ public void TryGetValue_PropertyAliased_IfAnnotationSet() [Fact] public void TryGetValue_ReturnsFalse_IfContainerAndInstanceAreNull() { - SelectExpandWrapper wrapper = new SelectExpandWrapper() { ModelID = _modelID }; + SelectExpandWrapper wrapper = new SelectExpandWrapper { ModelID = _modelID }; object value; bool result = wrapper.TryGetPropertyValue("SampleProperty", out value); @@ -160,7 +160,7 @@ public void TryGetValue_ReturnsFalse_IfContainerAndInstanceAreNull() [Fact] public void TryGetValue_ReturnsFalse_IfPropertyNotPresentInElement() { - SelectExpandWrapper wrapper = new SelectExpandWrapper() { ModelID = _modelID }; + SelectExpandWrapper wrapper = new SelectExpandWrapper { ModelID = _modelID }; object value; bool result = wrapper.TryGetPropertyValue("SampleNotPresentProperty", out value); @@ -178,7 +178,7 @@ public void ToDictionary_ContainsAllStructuralProperties_IfInstanceIsNotNull() model.SetAnnotationValue(entityType, new ClrTypeAnnotation(typeof(TestEntity))); entityType.AddStructuralProperty("SampleProperty", EdmPrimitiveTypeKind.Int32); IEdmTypeReference edmType = new EdmEntityTypeReference(entityType, isNullable: false); - SelectExpandWrapper testWrapper = new SelectExpandWrapper() + SelectExpandWrapper testWrapper = new SelectExpandWrapper { Instance = new TestEntity { SampleProperty = 42 }, ModelID = ModelContainer.GetModelID(model), @@ -203,7 +203,7 @@ public void ToDictionary_ContainsAllProperties_FromContainer() entityType.AddStructuralProperty("SampleProperty", EdmPrimitiveTypeKind.Int32); MockPropertyContainer container = new MockPropertyContainer(); container.Properties.Add("Property", 42); - SelectExpandWrapper wrapper = new SelectExpandWrapper() + SelectExpandWrapper wrapper = new SelectExpandWrapper { Container = container, ModelID = ModelContainer.GetModelID(model) @@ -238,7 +238,7 @@ public void ToDictionary_Throws_IfMapperProvider_ReturnsNullPropertyMapper() model.SetAnnotationValue(entityType, new ClrTypeAnnotation(typeof(TestEntity))); IEdmTypeReference edmType = new EdmEntityTypeReference(entityType, isNullable: false); - SelectExpandWrapper wrapper = new SelectExpandWrapper() + SelectExpandWrapper wrapper = new SelectExpandWrapper { Instance = new TestEntity { SampleProperty = 42 }, ModelID = ModelContainer.GetModelID(model) @@ -267,7 +267,7 @@ public void ToDictionary_Throws_IfMappingIsNullOrEmpty_ForAGivenProperty(string model.SetAnnotationValue(entityType, new ClrTypeAnnotation(typeof(TestEntity))); IEdmTypeReference edmType = new EdmEntityTypeReference(entityType, isNullable: false); - SelectExpandWrapper testWrapper = new SelectExpandWrapper() + SelectExpandWrapper testWrapper = new SelectExpandWrapper { Instance = new TestEntity { SampleProperty = 42 }, ModelID = ModelContainer.GetModelID(model), @@ -297,7 +297,7 @@ public void ToDictionary_AppliesMappingToAllProperties_IfInstanceIsNotNull() model.SetAnnotationValue(entityType, new ClrTypeAnnotation(typeof(TestEntity))); IEdmTypeReference edmType = new EdmEntityTypeReference(entityType, isNullable: false); - SelectExpandWrapper testWrapper = new SelectExpandWrapper() + SelectExpandWrapper testWrapper = new SelectExpandWrapper { Instance = new TestEntity { SampleProperty = 42 }, ModelID = ModelContainer.GetModelID(model), From 9731199f54b957ed42f6fa710ac4dd9c24f50348 Mon Sep 17 00:00:00 2001 From: Dilshod Komilov Date: Thu, 2 Sep 2021 09:29:01 +0500 Subject: [PATCH 6/9] fix failed tests --- .../Query/Expressions/SelectExpandBinderTest.cs | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/test/UnitTest/Microsoft.AspNet.OData.Test.Shared/Query/Expressions/SelectExpandBinderTest.cs b/test/UnitTest/Microsoft.AspNet.OData.Test.Shared/Query/Expressions/SelectExpandBinderTest.cs index b360b48ed5..0f94e1b7c9 100644 --- a/test/UnitTest/Microsoft.AspNet.OData.Test.Shared/Query/Expressions/SelectExpandBinderTest.cs +++ b/test/UnitTest/Microsoft.AspNet.OData.Test.Shared/Query/Expressions/SelectExpandBinderTest.cs @@ -109,10 +109,12 @@ public void Bind_GeneratedExpression_ContainsExpandedObject() // Assert IEnumerator enumerator = queryable.GetEnumerator(); + IEnumerator _querableEnumerator = _queryable.GetEnumerator(); Assert.True(enumerator.MoveNext()); + Assert.True(_querableEnumerator.MoveNext()); var partialCustomer = Assert.IsAssignableFrom>(enumerator.Current); Assert.False(enumerator.MoveNext()); - Assert.Null(partialCustomer.Instance); + Assert.Equal(_querableEnumerator.Current, partialCustomer.Instance); Assert.Equal("Microsoft.AspNet.OData.Test.Query.Expressions.QueryCustomer", partialCustomer.InstanceType); IEnumerable> innerOrders = partialCustomer.Container .ToDictionary(PropertyMapper)["Orders"] as IEnumerable>; @@ -464,7 +466,7 @@ public void ProjectAsWrapper_Element_ProjectedValueContainsInstance_IfSelectionI } [Fact] - public void ProjectAsWrapper_Element_ProjectedValueDoesNotContainInstance_IfSelectionIsPartial() + public void ProjectAsWrapper_Element_ProjectedValueShouldContainInstance_IfSelectionIsPartial() { // Arrange string select = "Id,Orders"; @@ -486,7 +488,7 @@ public void ProjectAsWrapper_Element_ProjectedValueDoesNotContainInstance_IfSele Assert.Empty((projection as MemberInitExpression).Bindings.Where(p => p.Member.Name == "Instance")); Assert.NotEmpty((projection as MemberInitExpression).Bindings.Where(p => p.Member.Name == "InstanceType")); SelectExpandWrapper customerWrapper = Expression.Lambda(projection).Compile().DynamicInvoke() as SelectExpandWrapper; - Assert.Null(customerWrapper.Instance); + Assert.Equal(aCustomer, customerWrapper.Instance); Assert.Equal("Microsoft.AspNet.OData.Test.Query.Expressions.QueryCustomer", customerWrapper.InstanceType); } From 11ad62685d690d294db2d8736a7ced266dab9371 Mon Sep 17 00:00:00 2001 From: Dilshod Komilov Date: Mon, 6 Sep 2021 14:07:38 +0500 Subject: [PATCH 7/9] fix --- .../Query/Expressions/SelectExpandBinder.cs | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/src/Microsoft.AspNet.OData.Shared/Query/Expressions/SelectExpandBinder.cs b/src/Microsoft.AspNet.OData.Shared/Query/Expressions/SelectExpandBinder.cs index 1c867ccc46..7a46a2ca8b 100644 --- a/src/Microsoft.AspNet.OData.Shared/Query/Expressions/SelectExpandBinder.cs +++ b/src/Microsoft.AspNet.OData.Shared/Query/Expressions/SelectExpandBinder.cs @@ -293,9 +293,10 @@ internal Expression ProjectElement(Expression source, SelectExpandClause selectE LinqParameterContainer.Parameterize(typeof(string), _modelID) : Expression.Constant(_modelID); wrapperTypeMemberAssignments.Add(Expression.Bind(wrapperProperty, wrapperPropertyValueExpression)); - + bool instanseOfWrapperClassWasSet = false; if (IsSelectAll(selectExpandClause)) { + instanseOfWrapperClassWasSet = true; // Initialize property 'Instance' on the wrapper class wrapperProperty = wrapperType.GetProperty("Instance"); wrapperTypeMemberAssignments.Add(Expression.Bind(wrapperProperty, source)); @@ -349,8 +350,11 @@ internal Expression ProjectElement(Expression source, SelectExpandClause selectE Type wrapperGenericType = GetWrapperGenericType(isInstancePropertySet, isTypeNamePropertySet, isContainerPropertySet); wrapperType = wrapperGenericType.MakeGenericType(elementType); - ConstructorInfo constructorInfo = wrapperType.GetConstructors().Single(c => c.GetParameters().Length == 1); - return Expression.MemberInit(Expression.New(constructorInfo, source), wrapperTypeMemberAssignments); + + if (instanseOfWrapperClassWasSet) + return Expression.MemberInit(Expression.New(wrapperType), wrapperTypeMemberAssignments); + ConstructorInfo constructorWithInstanse = wrapperType.GetConstructors().Single(c => c.GetParameters().Length == 1); + return Expression.MemberInit(Expression.New(constructorWithInstanse, source), wrapperTypeMemberAssignments); } /// From 119f2a97aeab0af7929095eb78afa63481debce5 Mon Sep 17 00:00:00 2001 From: Dilshod Komilov Date: Mon, 13 Sep 2021 13:06:55 +0500 Subject: [PATCH 8/9] remove flag --- .../Query/Expressions/SelectExpandBinder.cs | 7 +------ 1 file changed, 1 insertion(+), 6 deletions(-) diff --git a/src/Microsoft.AspNet.OData.Shared/Query/Expressions/SelectExpandBinder.cs b/src/Microsoft.AspNet.OData.Shared/Query/Expressions/SelectExpandBinder.cs index 7a46a2ca8b..681d7c5e40 100644 --- a/src/Microsoft.AspNet.OData.Shared/Query/Expressions/SelectExpandBinder.cs +++ b/src/Microsoft.AspNet.OData.Shared/Query/Expressions/SelectExpandBinder.cs @@ -293,10 +293,8 @@ internal Expression ProjectElement(Expression source, SelectExpandClause selectE LinqParameterContainer.Parameterize(typeof(string), _modelID) : Expression.Constant(_modelID); wrapperTypeMemberAssignments.Add(Expression.Bind(wrapperProperty, wrapperPropertyValueExpression)); - bool instanseOfWrapperClassWasSet = false; if (IsSelectAll(selectExpandClause)) { - instanseOfWrapperClassWasSet = true; // Initialize property 'Instance' on the wrapper class wrapperProperty = wrapperType.GetProperty("Instance"); wrapperTypeMemberAssignments.Add(Expression.Bind(wrapperProperty, source)); @@ -351,10 +349,7 @@ internal Expression ProjectElement(Expression source, SelectExpandClause selectE Type wrapperGenericType = GetWrapperGenericType(isInstancePropertySet, isTypeNamePropertySet, isContainerPropertySet); wrapperType = wrapperGenericType.MakeGenericType(elementType); - if (instanseOfWrapperClassWasSet) - return Expression.MemberInit(Expression.New(wrapperType), wrapperTypeMemberAssignments); - ConstructorInfo constructorWithInstanse = wrapperType.GetConstructors().Single(c => c.GetParameters().Length == 1); - return Expression.MemberInit(Expression.New(constructorWithInstanse, source), wrapperTypeMemberAssignments); + return Expression.MemberInit(Expression.New(wrapperType), wrapperTypeMemberAssignments); } /// From dfb9bac2ab5fa71c81af70d2b39b79ea7bc13608 Mon Sep 17 00:00:00 2001 From: Dilshod Komilov Date: Mon, 22 Nov 2021 15:58:23 +0500 Subject: [PATCH 9/9] fix --- .../Query/Expressions/SelectExpandBinder.cs | 48 ++++--------------- 1 file changed, 9 insertions(+), 39 deletions(-) diff --git a/src/Microsoft.AspNet.OData.Shared/Query/Expressions/SelectExpandBinder.cs b/src/Microsoft.AspNet.OData.Shared/Query/Expressions/SelectExpandBinder.cs index 681d7c5e40..9d3cb65228 100644 --- a/src/Microsoft.AspNet.OData.Shared/Query/Expressions/SelectExpandBinder.cs +++ b/src/Microsoft.AspNet.OData.Shared/Query/Expressions/SelectExpandBinder.cs @@ -293,6 +293,7 @@ internal Expression ProjectElement(Expression source, SelectExpandClause selectE LinqParameterContainer.Parameterize(typeof(string), _modelID) : Expression.Constant(_modelID); wrapperTypeMemberAssignments.Add(Expression.Bind(wrapperProperty, wrapperPropertyValueExpression)); + if (IsSelectAll(selectExpandClause)) { // Initialize property 'Instance' on the wrapper class @@ -345,11 +346,16 @@ internal Expression ProjectElement(Expression source, SelectExpandClause selectE } } } + if (isInstancePropertySet) + { + Type wrapperGenericType = GetWrapperGenericType(isInstancePropertySet, isTypeNamePropertySet, isContainerPropertySet); + wrapperType = wrapperGenericType.MakeGenericType(elementType); + return Expression.MemberInit(Expression.New(wrapperType), wrapperTypeMemberAssignments); + } + ConstructorInfo constructorWithInstanse = wrapperType.GetConstructors().Single(c => c.GetParameters().Length == 1); + return Expression.MemberInit(Expression.New(constructorWithInstanse, source), wrapperTypeMemberAssignments); - Type wrapperGenericType = GetWrapperGenericType(isInstancePropertySet, isTypeNamePropertySet, isContainerPropertySet); - wrapperType = wrapperGenericType.MakeGenericType(elementType); - return Expression.MemberInit(Expression.New(wrapperType), wrapperTypeMemberAssignments); } /// @@ -1243,54 +1249,18 @@ property selection combination possible. */ private class SelectAllAndExpand : SelectExpandWrapper { - public SelectAllAndExpand(TEntity entity) : base(entity) - { - - } - - public SelectAllAndExpand() - { - - } } private class SelectAll : SelectExpandWrapper { - public SelectAll(TEntity entity) : base(entity) - { - - } - - public SelectAll() - { - - } } private class SelectSomeAndInheritance : SelectExpandWrapper { - public SelectSomeAndInheritance(TEntity entity) : base(entity) - { - - } - - public SelectSomeAndInheritance() - { - - } } private class SelectSome : SelectAllAndExpand { - public SelectSome(TEntity entity) : base(entity) - { - - } - - public SelectSome() - { - - } } } }