Skip to content

Commit

Permalink
Fixing Azure#210 (Improve binding error message)
Browse files Browse the repository at this point in the history
  • Loading branch information
mathewc committed Oct 20, 2015
1 parent 9ab2199 commit 84faf32
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
11 changes: 11 additions & 0 deletions src/Microsoft.Azure.WebJobs.Host/Bindings/BindingDataProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,11 @@ public IReadOnlyDictionary<string, object> GetBindingData(object value)
/// <returns>A <see cref="BindingDataProvider"/> instance or null for unsupported types.</returns>
public static BindingDataProvider FromType(Type type)
{
if (type == null)
{
throw new ArgumentNullException("type");
}

if ((type == typeof(object)) ||
(type == typeof(string)) ||
(type == typeof(byte[])))
Expand All @@ -99,6 +104,12 @@ public static BindingDataProvider FromType(Type type)
Dictionary<string, Type> contract = new Dictionary<string, Type>(StringComparer.OrdinalIgnoreCase);
foreach (PropertyHelper property in bindingDataProperties)
{
if (contract.ContainsKey(property.Name))
{
throw new InvalidOperationException(
string.Format(CultureInfo.InvariantCulture,
"Multiple properties named '{0}' found in type '{1}'.", property.Name, type.Name));
}
contract.Add(property.Name, property.PropertyType);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,16 @@ public void FromType_IfStructuredDataType_ReturnsValidContract()
Assert.Equal(expected, names);
}

[Fact]
public void FromType_DuplicatePropertiesDetected_Throws()
{
var ex = Assert.Throws<InvalidOperationException>(() =>
{
BindingDataProvider.FromType(typeof(DerivedWithNew));
});
Assert.Equal("Multiple properties named 'A' found in type 'DerivedWithNew'.", ex.Message);
}

[Fact]
public void GetBindingData_IfSimpleDataType_ReturnsValidBindingData()
{
Expand Down Expand Up @@ -135,7 +145,7 @@ private class DataTypeWithDateProperty
public DateTime date { get; set; }
}

class NestedDataType
private class NestedDataType
{
public int _field = 0; // skip: not a property

Expand All @@ -149,5 +159,15 @@ class NestedDataType

private int PrivateIntProp { get; set; } // skip: private property
}

private class Base
{
public int? A { get; set; }
}

private class DerivedWithNew : Base
{
new public int A { get; set; }
}
}
}

0 comments on commit 84faf32

Please sign in to comment.