diff options
author | raghuramn <ranadimi@microsoft.com> | 2012-10-06 01:41:41 +0400 |
---|---|---|
committer | raghuramn <ranadimi@microsoft.com> | 2012-10-11 23:28:38 +0400 |
commit | 88d18486a47b4044e84fc1683713d7cdfc965ae0 (patch) | |
tree | 1ea88019ce27cf527d940d05451cc23aea1d8d78 | |
parent | ebde3ec91024ac63aa5b4f432ad2a3689e58e0cd (diff) |
Issue 505: ConventionModelBuilder should ignore indexer property.
4 files changed, 52 insertions, 5 deletions
diff --git a/src/System.Web.Http.OData/OData/Builder/Conventions/ConventionsHelpers.cs b/src/System.Web.Http.OData/OData/Builder/Conventions/ConventionsHelpers.cs index 36ce68d3..7816ff6a 100644 --- a/src/System.Web.Http.OData/OData/Builder/Conventions/ConventionsHelpers.cs +++ b/src/System.Web.Http.OData/OData/Builder/Conventions/ConventionsHelpers.cs @@ -71,6 +71,12 @@ namespace System.Web.Http.OData.Builder.Conventions throw Error.ArgumentNull("propertyInfo"); } + // ignore any indexer properties. + if (propertyInfo.GetIndexParameters().Any()) + { + return false; + } + if (propertyInfo.CanRead) { // non-public getters are not valid properties diff --git a/test/System.Web.Http.OData.Test/OData/Builder/Conventions/ConventionsHelpersTests.cs b/test/System.Web.Http.OData.Test/OData/Builder/Conventions/ConventionsHelpersTests.cs index 8a9f25eb..0d689966 100644 --- a/test/System.Web.Http.OData.Test/OData/Builder/Conventions/ConventionsHelpersTests.cs +++ b/test/System.Web.Http.OData.Test/OData/Builder/Conventions/ConventionsHelpersTests.cs @@ -115,6 +115,16 @@ namespace System.Web.Http.OData.Builder.Conventions } [Fact] + public void GetAllProperties_Ignores_IndexerProperties() + { + Mock<IStructuralTypeConfiguration> edmType = new Mock<IStructuralTypeConfiguration>(); + edmType.Setup(t => t.ClrType).Returns(typeof(GetProperties_Derived)); + + var properties = ConventionsHelpers.GetAllProperties(edmType.Object).Select(p => p.Name); + Assert.DoesNotContain("Item", properties); + } + + [Fact] public void GetEntityKeyValue_SingleKey() { // Arrange @@ -331,6 +341,19 @@ namespace System.Web.Http.OData.Builder.Conventions public GetProperties_Complex Derived_Complex { get; set; } public int[] Collection { get; private set; } + + public string this[string str] + { + get + { + throw new NotImplementedException(); + } + + set + { + throw new NotImplementedException(); + } + } } class GetProperties_Complex diff --git a/test/System.Web.Http.OData.Test/OData/Builder/Conventions/ODataConventionModelBuilderTests.cs b/test/System.Web.Http.OData.Test/OData/Builder/Conventions/ODataConventionModelBuilderTests.cs index 7ed989f0..3576c98f 100644 --- a/test/System.Web.Http.OData.Test/OData/Builder/Conventions/ODataConventionModelBuilderTests.cs +++ b/test/System.Web.Http.OData.Test/OData/Builder/Conventions/ODataConventionModelBuilderTests.cs @@ -10,6 +10,7 @@ using System.Web.Http.OData.Builder.TestModels; using System.Web.Http.OData.Formatter; using Microsoft.Data.Edm; using Microsoft.TestCommon; +using Moq; namespace System.Web.Http.OData.Builder.Conventions { @@ -894,6 +895,24 @@ namespace System.Web.Http.OData.Builder.Conventions } [Fact] + public void ODataConventionModelBuilder_IgnoresIndexerProperties() + { + MockType type = + new MockType("ComplexType") + .Property<int>("Item"); + + MockPropertyInfo pi = type.GetProperty("Item"); + pi.Setup(p => p.GetIndexParameters()).Returns(new[] { new Mock<ParameterInfo>().Object }); // make it indexer + + ODataConventionModelBuilder builder = new ODataConventionModelBuilder(); + builder.AddComplexType(type); + + IEdmModel model = builder.GetEdmModel(); + IEdmComplexType complexType = model.AssertHasComplexType(type); + Assert.Empty(complexType.Properties()); + } + + [Fact] public void CanBuildModelForAnonymousTypes() { Type entityType = new diff --git a/test/System.Web.Http.OData.Test/TestCommon/MockType.cs b/test/System.Web.Http.OData.Test/TestCommon/MockType.cs index a434239c..0127e324 100644 --- a/test/System.Web.Http.OData.Test/TestCommon/MockType.cs +++ b/test/System.Web.Http.OData.Test/TestCommon/MockType.cs @@ -15,7 +15,7 @@ namespace System.Web.Http.OData return mockType.Object; } - private readonly List<PropertyInfo> _propertyInfos = new List<PropertyInfo>(); + private readonly List<MockPropertyInfo> _propertyInfos = new List<MockPropertyInfo>(); public MockType() : this("T") @@ -27,7 +27,7 @@ namespace System.Web.Http.OData SetupGet(t => t.Name).Returns(typeName); SetupGet(t => t.BaseType).Returns(typeof(Object)); SetupGet(t => t.Assembly).Returns(typeof(object).Assembly); - Setup(t => t.GetProperties(It.IsAny<BindingFlags>())).Returns(() => _propertyInfos.ToArray()); + Setup(t => t.GetProperties(It.IsAny<BindingFlags>())).Returns(() => _propertyInfos.Select(p => p.Object).ToArray()); Setup(t => t.Equals(It.IsAny<object>())).Returns<Type>(t => ReferenceEquals(Object, t)); Setup(t => t.ToString()).Returns(typeName); Setup(t => t.Namespace).Returns(@namespace); @@ -87,9 +87,9 @@ namespace System.Web.Http.OData return this; } - public PropertyInfo GetProperty(string name) + public MockPropertyInfo GetProperty(string name) { - return _propertyInfos.Single(p => p.Name == name); + return _propertyInfos.Single(p => p.Object.Name == name); } public MockType AsCollection() @@ -97,7 +97,6 @@ namespace System.Web.Http.OData var mockCollectionType = new MockType(); mockCollectionType.Setup(t => t.GetInterfaces()).Returns(new Type[] { typeof(IEnumerable<>).MakeGenericType(this) }); - //mockCollectionType.Setup(t => t.GetInterfaces()).Returns(new[] { typeof(ICollection<>).MakeGenericType(this) }); return mockCollectionType; } |