Skip to content

Commit

Permalink
add support for newtonsoft 9.0+, fix Azure#28
Browse files Browse the repository at this point in the history
  • Loading branch information
watashiSHUN committed Apr 16, 2018
1 parent 76fa809 commit df8672a
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 4 deletions.
7 changes: 7 additions & 0 deletions EventGridBinding.sln
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "test", "test", "{B40A0AB6-1
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Extension.tests", "test\Extension.tests\Extension.tests.csproj", "{694416E6-094C-4F52-8B0F-D6B8197E5660}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Extension.testHelper", "test\Extension.testHelper\Extension.testHelper.csproj", "{0E652881-C631-4EA0-82AE-5BB21A8247BB}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand All @@ -25,13 +27,18 @@ Global
{694416E6-094C-4F52-8B0F-D6B8197E5660}.Debug|Any CPU.Build.0 = Debug|Any CPU
{694416E6-094C-4F52-8B0F-D6B8197E5660}.Release|Any CPU.ActiveCfg = Release|Any CPU
{694416E6-094C-4F52-8B0F-D6B8197E5660}.Release|Any CPU.Build.0 = Release|Any CPU
{0E652881-C631-4EA0-82AE-5BB21A8247BB}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{0E652881-C631-4EA0-82AE-5BB21A8247BB}.Debug|Any CPU.Build.0 = Debug|Any CPU
{0E652881-C631-4EA0-82AE-5BB21A8247BB}.Release|Any CPU.ActiveCfg = Release|Any CPU
{0E652881-C631-4EA0-82AE-5BB21A8247BB}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{D04DD2A7-EE6B-44C2-B615-DC380D8D2569} = {D0455F87-5E51-4AFD-ACF1-2F50A352F3D5}
{694416E6-094C-4F52-8B0F-D6B8197E5660} = {B40A0AB6-1971-4DE9-954A-DD92AF1694CC}
{0E652881-C631-4EA0-82AE-5BB21A8247BB} = {B40A0AB6-1971-4DE9-954A-DD92AF1694CC}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {7059D7BF-670B-4F26-9A7A-E73F95BEBF57}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public Task<ITriggerBinding> TryCreateAsync(TriggerBindingProviderContext contex

public bool IsSupportedBindingType(Type t)
{
return (t == typeof(EventGridEvent) || t == typeof(string) || t == typeof(JObject));
return (t == typeof(EventGridEvent) || t == typeof(string) || t.Name == "JObject");
}

internal class EventGridTriggerBinding : ITriggerBinding
Expand Down Expand Up @@ -114,7 +114,6 @@ public Task<ITriggerData> BindAsync(object value, ValueBindingContext context)
};

// convert to parameterType
// TODO use converters
object argument;
if (_parameter.ParameterType == typeof(string))
{
Expand All @@ -124,10 +123,16 @@ public Task<ITriggerData> BindAsync(object value, ValueBindingContext context)
{
argument = triggerValue.ToObject<EventGridEvent>();
}
else
else if (_parameter.ParameterType == typeof(JObject))
{
argument = triggerValue;
}
else
{
// convert to jobject of specific newtonsoft version
var method = _parameter.ParameterType.GetMethod("Parse", new Type[] { typeof(string) });
argument = method.Invoke(null, new object[] { triggerValue.ToString() });
}

IValueBinder valueBinder = new EventGridValueBinder(_parameter, argument);
return Task.FromResult<ITriggerData>(new TriggerData(valueBinder, bindingData));
Expand Down
14 changes: 14 additions & 0 deletions test/Extension.testHelper/Extension.testHelper.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<TargetFramework>net46</TargetFramework>
<AssemblyName>Microsoft.Azure.WebJobs.Extensions.EventGrid.TestHelper</AssemblyName>
<RootNamespace>Microsoft.Azure.WebJobs.Extensions.EventGrid.Tests</RootNamespace>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Newtonsoft.Json" Version="11.0.2" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\..\src\EventGridExtension\EventGridExtension.csproj" />
</ItemGroup>
</Project>
14 changes: 14 additions & 0 deletions test/Extension.testHelper/MyProgram.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using Newtonsoft.Json.Linq;

namespace Microsoft.Azure.WebJobs.Extensions.EventGrid.Tests
{
public class MyProgram
{
public static string functionOut = null;

public void TestEventGridToJObject([EventGridTrigger] JObject value)
{
functionOut = (string)value["subject"];
}
}
}
14 changes: 13 additions & 1 deletion test/Extension.tests/JobhostEndToEnd.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using System.Reflection;
using System.Threading.Tasks;
using Microsoft.Azure.WebJobs.Extensions.EventGrid.Tests.Common;
using Newtonsoft.Json.Linq;
Expand Down Expand Up @@ -34,6 +36,16 @@ public async Task ConsumeEventGridEventTest()
await host.CallAsync("MyProg1.TestEventGridToJObject", args);
Assert.Equal(functionOut, expectOut);
functionOut = null;

Assembly ass = Assembly.LoadFrom(@"..\..\..\..\Extension.testHelper\bin\Debug\net46\Microsoft.Azure.WebJobs.Extensions.EventGrid.TestHelper.dll");
Type type = ass.GetType("Microsoft.Azure.WebJobs.Extensions.EventGrid.Tests.MyProgram");
var genericMethod = typeof(TestHelpers).GetMethod("NewHost");
var method = genericMethod.MakeGenericMethod(type);
host = (JobHost)method.Invoke(null, new object[] { null });
await host.CallAsync("MyProgram.TestEventGridToJObject", args);
var output = type.GetField("functionOut").GetValue(null);
Assert.Equal(output, expectOut);

}

[Fact]
Expand Down

0 comments on commit df8672a

Please sign in to comment.