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

Add support for custom names for values in data binding #787

Open
wants to merge 4 commits into
base: dev
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
14 changes: 10 additions & 4 deletions src/WebJobs.Extensions.Http/PropertyHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using System.Runtime.Serialization;

namespace Microsoft.Azure.WebJobs.Extensions.Http
{
Expand Down Expand Up @@ -49,7 +50,7 @@ public PropertyHelper(PropertyInfo property)
}

Property = property;
Name = property.Name;
Name = GetDataMemberAttributeValue(property) ?? property.Name;
ValueGetter = MakeFastPropertyGetter(property);
}

Expand Down Expand Up @@ -506,9 +507,14 @@ protected static PropertyHelper[] GetProperties(
private static bool IsInterestingProperty(PropertyInfo property)
{
return property.GetIndexParameters().Length == 0 &&
property.GetMethod != null &&
property.GetMethod.IsPublic &&
!property.GetMethod.IsStatic;
property.GetMethod != null &&
property.GetMethod.IsPublic &&
!property.GetMethod.IsStatic;
}

private static string GetDataMemberAttributeValue(MemberInfo property)
{
return property.GetCustomAttribute<DataMemberAttribute>()?.Name;
}
}
}
53 changes: 52 additions & 1 deletion test/WebJobs.Extensions.Http.Tests/HttpTriggerBindingTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using System.IO;
using System.Net.Http;
using System.Reflection;
using System.Runtime.Serialization;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
Expand Down Expand Up @@ -255,6 +256,28 @@ public async Task BindAsync_Poco_FromQueryParameters()
Assert.Equal("Mathew Charles", testPoco.Name);
Assert.Equal("Seattle", testPoco.Location);
}

[Fact]
public async Task BindAsync_Poco_FromQueryParameters_WithDifferentNaming()
{
ParameterInfo parameterInfo = GetType().GetMethod("TestPocoFunctionWithCustomName").GetParameters()[0];
HttpTriggerAttributeBindingProvider.HttpTriggerBinding binding = new HttpTriggerAttributeBindingProvider.HttpTriggerBinding(new HttpTriggerAttribute(), parameterInfo, true);

HttpRequest request = HttpTestHelpers.CreateHttpRequest("GET", "http://functions/myfunc?code=abc123&custom_value=Mathew%20Charles&Location=Seattle");

FunctionBindingContext functionContext = new FunctionBindingContext(Guid.NewGuid(), CancellationToken.None);
ValueBindingContext context = new ValueBindingContext(functionContext, CancellationToken.None);
ITriggerData triggerData = await binding.BindAsync(request, context);

Assert.Equal(6, triggerData.BindingData.Count);
Assert.Null(triggerData.BindingData[nameof(TestPocoWithDataMember.CustomValue)]);
Assert.Equal("Mathew Charles", triggerData.BindingData["custom_value"]);
Assert.Equal("Seattle", triggerData.BindingData[nameof(TestPocoWithDataMember.Location)]);

TestPocoWithDataMember testPoco = (TestPocoWithDataMember)(await triggerData.ValueProvider.GetValueAsync());
Assert.Equal("Mathew Charles", testPoco.CustomValue);
Assert.Equal("Seattle", testPoco.Location);
}

[Fact]
public async Task BindAsync_Poco_FromRouteParameters()
Expand Down Expand Up @@ -510,6 +533,22 @@ public static void ApplyBindingData_Succeeds()
Assert.Equal(pair.Value, poco.Properties[pair.Key]);
}
}

[Fact]
public static void ApplyBindingDataWithDataMember_Succeeds()
{
TestPocoWithDataMember poco = new TestPocoWithDataMember();
Dictionary<string, object> bindingData = new Dictionary<string, object>()
{
{ "Location", "Seattle" },
{ "custom_value", "25" }
};

HttpTriggerAttributeBindingProvider.HttpTriggerBinding.ApplyBindingData(poco, bindingData);

Assert.Equal("Seattle", poco.Location);
Assert.Equal("25", poco.CustomValue);
}

[Fact]
public static void LazyBindingData_DelaysInstantiation()
Expand Down Expand Up @@ -552,6 +591,10 @@ public void TestPocoFunction(TestPoco poco)
{
}

public void TestPocoFunctionWithCustomName(TestPocoWithDataMember poco)
{
}

public void TestPocoFunctionEx(TestPocoEx poco)
{
}
Expand Down Expand Up @@ -605,6 +648,14 @@ public class TestPoco
public string Location { get; set; }
}

public class TestPocoWithDataMember
{
[DataMember(Name = "custom_value")]
public string CustomValue { get; set; }

public string Location { get; set; }
}

public class TestPocoEx : TestPoco
{
public int Age { get; set; }
Expand All @@ -616,4 +667,4 @@ public class TestPocoEx : TestPoco
public IDictionary<string, string> Properties { get; set; }
}
}
}
}