Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MissingMethodException on getting ResourceInstance from ODataResourceSerializer #2558

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -346,10 +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);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

as per code guidelines, please enclose body of conditional in braces (even if it's a single line). Improves code readability and reduces introducing logical errors later.

}
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);
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,16 @@ namespace Microsoft.AspNet.OData.Query.Expressions
[JsonConverter(typeof(SelectExpandWrapperConverter))]
internal class SelectExpandWrapper<TElement> : SelectExpandWrapper
{
public SelectExpandWrapper(TElement element)
{
Instance = element;
}

public SelectExpandWrapper()
{

}

/// <summary>
/// Gets or sets the instance of the element being selected and expanded.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,10 +109,12 @@ public void Bind_GeneratedExpression_ContainsExpandedObject()

// Assert
IEnumerator enumerator = queryable.GetEnumerator();
IEnumerator _querableEnumerator = _queryable.GetEnumerator();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IEnumerator implements IDisposable. Please wrap this in a using block

Assert.True(enumerator.MoveNext());
Assert.True(_querableEnumerator.MoveNext());
var partialCustomer = Assert.IsAssignableFrom<SelectExpandWrapper<QueryCustomer>>(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<SelectExpandWrapper<QueryOrder>> innerOrders = partialCustomer.Container
.ToDictionary(PropertyMapper)["Orders"] as IEnumerable<SelectExpandWrapper<QueryOrder>>;
Expand Down Expand Up @@ -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";
Expand All @@ -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<QueryCustomer> customerWrapper = Expression.Lambda(projection).Compile().DynamicInvoke() as SelectExpandWrapper<QueryCustomer>;
Assert.Null(customerWrapper.Instance);
Assert.Equal(aCustomer, customerWrapper.Instance);
Assert.Equal("Microsoft.AspNet.OData.Test.Query.Expressions.QueryCustomer", customerWrapper.InstanceType);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,10 @@ public void TryGetValue_ReturnsValueFromPropertyContainer_IfPresent()
MockPropertyContainer container = new MockPropertyContainer();
container.Properties.Add("SampleProperty", expectedPropertyValue);
SelectExpandWrapper<TestEntity> wrapper = new SelectExpandWrapper<TestEntity>
{
ModelID = _modelID,
Container = container
};
{
ModelID = _modelID,
Container = container
};
wrapper.Instance = new TestEntity();

object value;
Expand All @@ -95,10 +95,10 @@ public void TryGetValue_ReturnsValueFromInstance_IfNotPresentInContainer()
object expectedPropertyValue = new object();
MockPropertyContainer container = new MockPropertyContainer();
SelectExpandWrapper<TestEntity> wrapper = new SelectExpandWrapper<TestEntity>
{
ModelID = _modelID,
Container = container
};
{
ModelID = _modelID,
Container = container
};
wrapper.Instance = new TestEntity { SampleProperty = expectedPropertyValue };
wrapper.UseInstanceForProperties = true;

Expand Down Expand Up @@ -133,7 +133,7 @@ public void TryGetValue_PropertyAliased_IfAnnotationSet()
_model.CustomerName,
new ClrPropertyInfoAnnotation(typeof(TestEntityWithAlias).GetProperty("SampleProperty")));
object expectedPropertyValue = new object();
SelectExpandWrapper<TestEntityWithAlias> wrapper = new SelectExpandWrapper<TestEntityWithAlias> { ModelID = _modelID };
SelectExpandWrapper<TestEntityWithAlias> wrapper = new SelectExpandWrapper<TestEntityWithAlias>() { ModelID = _modelID };
wrapper.Instance = new TestEntityWithAlias { SampleProperty = expectedPropertyValue };
wrapper.UseInstanceForProperties = true;

Expand Down Expand Up @@ -248,7 +248,7 @@ public void ToDictionary_Throws_IfMapperProvider_ReturnsNullPropertyMapper()
(IEdmModel m, IEdmStructuredType t) => null;

// Act & Assert
ExceptionAssert.Throws<InvalidOperationException>(() =>
ExceptionAssert.Throws<InvalidOperationException>(() =>
wrapper.ToDictionary(mapperProvider: mapperProvider),
"The mapper provider must return a valid 'Microsoft.AspNet.OData.Query.IPropertyMapper' instance for the given 'NS.Name' IEdmType.");
}
Expand All @@ -261,12 +261,12 @@ 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<TestEntity> testWrapper = new SelectExpandWrapper<TestEntity>
{
Instance = new TestEntity { SampleProperty = 42 },
Expand All @@ -281,7 +281,7 @@ public void ToDictionary_Throws_IfMappingIsNullOrEmpty_ForAGivenProperty(string

// Act & Assert
ExceptionAssert.Throws<InvalidOperationException>(() =>
testWrapper.ToDictionary(mapperProvider),
testWrapper.ToDictionary(mapperProvider),
"The key mapping for the property 'SampleProperty' can't be null or empty.");
}

Expand All @@ -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<TestEntity> testWrapper = new SelectExpandWrapper<TestEntity>
{
Instance = new TestEntity { SampleProperty = 42 },
ModelID = ModelContainer.GetModelID(model),
UseInstanceForProperties = true,
};

Mock<IPropertyMapper> mapperMock = new Mock<IPropertyMapper>();
mapperMock.Setup(m => m.MapProperty("SampleProperty")).Returns("Sample");
Func<IEdmModel, IEdmStructuredType, IPropertyMapper> mapperProvider =
Expand Down