Skip to content

Commit b712e5c

Browse files
committed
Fixing BindingData conversion bug
1 parent 9496d18 commit b712e5c

File tree

5 files changed

+33
-21
lines changed

5 files changed

+33
-21
lines changed

src/WebJobs.Script.WebHost/Diagnostics/FastLogger.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,8 @@
44
using System;
55
using System.Threading;
66
using System.Threading.Tasks;
7-
using Microsoft.Azure.WebJobs;
87
using Microsoft.Azure.WebJobs.Host.Loggers;
98
using Microsoft.Azure.WebJobs.Logging;
10-
using Microsoft.Azure.WebJobs.Script;
119
using Microsoft.WindowsAzure.Storage;
1210
using Newtonsoft.Json;
1311

src/WebJobs.Script/Binding/Http/HttpTriggerAttributeBindingProvider.cs

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -201,14 +201,7 @@ internal static void ApplyBindingData(object target, IDictionary<string, object>
201201
propertyHelper.Property.CanWrite)
202202
{
203203
object value = pair.Value;
204-
Type targetType = propertyHelper.Property.PropertyType;
205-
if (value != null && value.GetType() != targetType)
206-
{
207-
// if the type is nullable, we only need to convert to the
208-
// correct underlying type
209-
targetType = Nullable.GetUnderlyingType(targetType) ?? targetType;
210-
value = Convert.ChangeType(value, targetType);
211-
}
204+
value = ConvertValueIfNecessary(value, propertyHelper.Property.PropertyType);
212205
propertyHelper.SetValue(target, value);
213206
}
214207
}
@@ -249,15 +242,10 @@ internal static async Task<IReadOnlyDictionary<string, object>> GetRequestBindin
249242
// necessary conversion
250243
value = pair.Value;
251244
Type type = null;
252-
if (value != null &&
253-
bindingDataContract != null &&
254-
bindingDataContract.TryGetValue(pair.Key, out type) &&
255-
value.GetType() != type)
245+
if (bindingDataContract != null &&
246+
bindingDataContract.TryGetValue(pair.Key, out type))
256247
{
257-
// if the type is nullable, we only need to convert to the
258-
// correct underlying type
259-
type = Nullable.GetUnderlyingType(type) ?? type;
260-
value = Convert.ChangeType(value, type);
248+
value = ConvertValueIfNecessary(value, type);
261249
}
262250

263251
bindingData[pair.Key] = value;
@@ -290,6 +278,19 @@ private async Task<IValueProvider> CreateUserTypeValueProvider(HttpRequestMessag
290278
return new SimpleValueProvider(_parameter.ParameterType, value, invokeString);
291279
}
292280

281+
private static object ConvertValueIfNecessary(object value, Type targetType)
282+
{
283+
if (value != null && !targetType.IsAssignableFrom(value.GetType()))
284+
{
285+
// if the type is nullable, we only need to convert to the
286+
// correct underlying type
287+
targetType = Nullable.GetUnderlyingType(targetType) ?? targetType;
288+
value = Convert.ChangeType(value, targetType);
289+
}
290+
291+
return value;
292+
}
293+
293294
/// <summary>
294295
/// ValueBinder for all our built in supported Types
295296
/// </summary>

test/WebJobs.Script.Tests/HttpTriggerBindingTests.cs

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ public async Task BindAsync_Poco_MergedBindingData()
199199
ValueBindingContext context = new ValueBindingContext(functionContext, CancellationToken.None);
200200
ITriggerData triggerData = await binding.BindAsync(request, context);
201201

202-
Assert.Equal(5, triggerData.BindingData.Count);
202+
Assert.Equal(6, triggerData.BindingData.Count);
203203
Assert.Equal("Mathew Charles", triggerData.BindingData["Name"]);
204204
Assert.Equal("Seattle", triggerData.BindingData["Location"]);
205205
Assert.Equal("(425) 555-6666", triggerData.BindingData["Phone"]);
@@ -284,12 +284,19 @@ public async Task BindAsync_String()
284284
public static void ApplyBindingData_Succeeds()
285285
{
286286
TestPocoEx poco = new TestPocoEx();
287+
Dictionary<string, string> properties = new Dictionary<string, string>
288+
{
289+
{ "A", "123" },
290+
{ "B", "456" },
291+
{ "c", "789" }
292+
};
287293
Dictionary<string, object> bindingData = new Dictionary<string, object>()
288294
{
289295
{ "name", "Ted" },
290296
{ "Location", "Seattle" },
291297
{ "Age", "25" },
292-
{ "Readonly", "Test" }
298+
{ "Readonly", "Test" },
299+
{ "Properties", properties }
293300
};
294301

295302
HttpTriggerAttributeBindingProvider.HttpTriggerBinding.ApplyBindingData(poco, bindingData);
@@ -298,6 +305,11 @@ public static void ApplyBindingData_Succeeds()
298305
Assert.Equal("Seattle", poco.Location);
299306
Assert.Equal(25, poco.Age); // verifies string was converted
300307
Assert.Null(poco.Readonly);
308+
Assert.Equal(3, poco.Properties.Count);
309+
foreach (var pair in properties)
310+
{
311+
Assert.Equal(pair.Value, poco.Properties[pair.Key]);
312+
}
301313
}
302314

303315
public void TestPocoFunction(TestPoco poco)

test/WebJobs.Script.Tests/PhpEndToEndTests.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
// Copyright (c) .NET Foundation. All rights reserved.
22
// Licensed under the MIT License. See License.txt in the project root for license information.
33

4-
using System;
54
using System.Threading.Tasks;
65
using Microsoft.WindowsAzure.Storage.Blob;
76
using Xunit;

test/WebJobs.Script.Tests/UtilityTests.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ public class TestPocoEx : TestPoco
3131
public string Phone { get; set; }
3232

3333
public string Readonly { get; }
34+
35+
public IDictionary<string, string> Properties { get; set; }
3436
}
3537

3638
public class UtilityTests

0 commit comments

Comments
 (0)