Skip to content

Commit da2cbd9

Browse files
committed
Adding handling for ExpandoObject to HttpBinding StringContent default path
1 parent 6b4c50b commit da2cbd9

File tree

6 files changed

+87
-49
lines changed

6 files changed

+87
-49
lines changed

src/WebJobs.Script/Binding/FunctionBinding.cs

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,13 @@
1414
using Microsoft.Azure.WebJobs.Script.Description;
1515
using Microsoft.Azure.WebJobs.Script.Extensibility;
1616
using Newtonsoft.Json;
17-
using Newtonsoft.Json.Converters;
1817
using Newtonsoft.Json.Linq;
1918

2019
namespace Microsoft.Azure.WebJobs.Script.Binding
2120
{
2221
public abstract class FunctionBinding
2322
{
2423
private readonly ScriptHostConfiguration _config;
25-
private static readonly ExpandoObjectConverter _expandoObjectJsonConverter = new ExpandoObjectConverter();
2624

2725
protected FunctionBinding(ScriptHostConfiguration config, BindingMetadata metadata, FileAccess access)
2826
{
@@ -168,7 +166,7 @@ internal static async Task BindAsyncCollectorAsync<T>(BindingContext context)
168166
{
169167
if (value is ExpandoObject)
170168
{
171-
converted = ToJson((ExpandoObject)value);
169+
converted = Utility.ToJson((ExpandoObject)value, Formatting.None);
172170
}
173171
else
174172
{
@@ -183,7 +181,7 @@ internal static async Task BindAsyncCollectorAsync<T>(BindingContext context)
183181
}
184182
else if (value is ExpandoObject)
185183
{
186-
converted = ToJObject((ExpandoObject)value);
184+
converted = Utility.ToJObject((ExpandoObject)value);
187185
}
188186
}
189187
else if (typeof(T) == typeof(byte[]))
@@ -194,7 +192,7 @@ internal static async Task BindAsyncCollectorAsync<T>(BindingContext context)
194192
string stringValue = null;
195193
if (value is ExpandoObject)
196194
{
197-
stringValue = ToJson((ExpandoObject)value);
195+
stringValue = Utility.ToJson((ExpandoObject)value, Formatting.None);
198196
}
199197
else
200198
{
@@ -283,7 +281,7 @@ public static void ConvertValueToStream(object value, Stream stream)
283281
}
284282
else if (value is ExpandoObject)
285283
{
286-
string json = ToJson((ExpandoObject)value);
284+
string json = Utility.ToJson((ExpandoObject)value, Formatting.None);
287285
bytes = Encoding.UTF8.GetBytes(json);
288286
}
289287

@@ -327,16 +325,5 @@ public static void ConvertStreamToValue(Stream stream, DataType dataType, ref ob
327325
break;
328326
}
329327
}
330-
331-
internal static string ToJson(ExpandoObject value)
332-
{
333-
return JsonConvert.SerializeObject(value, Formatting.None, _expandoObjectJsonConverter);
334-
}
335-
336-
internal static JObject ToJObject(ExpandoObject value)
337-
{
338-
string json = ToJson(value);
339-
return JObject.Parse(json);
340-
}
341328
}
342329
}

src/WebJobs.Script/Binding/HttpBinding.cs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System;
55
using System.Collections.Generic;
66
using System.Collections.ObjectModel;
7+
using System.Dynamic;
78
using System.IO;
89
using System.Linq;
910
using System.Net;
@@ -179,7 +180,7 @@ private static HttpResponseMessage CreateResponse(HttpRequestMessage request, Ht
179180
return CreateNegotiatedResponse(request, statusCode, content);
180181
}
181182

182-
private static HttpContent CreateResultContent(object content, string mediaType = null)
183+
internal static HttpContent CreateResultContent(object content, string mediaType = null)
183184
{
184185
if (content is byte[])
185186
{
@@ -190,7 +191,17 @@ private static HttpContent CreateResultContent(object content, string mediaType
190191
return new StreamContent((Stream)content);
191192
}
192193

193-
return new StringContent(content?.ToString() ?? string.Empty, null, mediaType);
194+
string stringContent;
195+
if (content is ExpandoObject)
196+
{
197+
stringContent = Utility.ToJson((ExpandoObject)content);
198+
}
199+
else
200+
{
201+
stringContent = content?.ToString() ?? string.Empty;
202+
}
203+
204+
return new StringContent(stringContent, null, mediaType);
194205
}
195206

196207
private static HttpResponseMessage CreateNegotiatedResponse(HttpRequestMessage request, HttpStatusCode statusCode, object content)

src/WebJobs.Script/Utility.cs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,21 @@
33

44
using System;
55
using System.Collections.Generic;
6+
using System.Dynamic;
67
using System.Globalization;
78
using System.Linq;
89
using System.Text;
910
using Microsoft.Azure.WebJobs.Script.Config;
11+
using Newtonsoft.Json;
12+
using Newtonsoft.Json.Converters;
1013
using Newtonsoft.Json.Linq;
1114

1215
namespace Microsoft.Azure.WebJobs.Script
1316
{
1417
public static class Utility
1518
{
19+
private static readonly ExpandoObjectConverter _expandoObjectJsonConverter = new ExpandoObjectConverter();
20+
1621
public static string GetSubscriptionId()
1722
{
1823
string ownerName = ScriptSettingsManager.Instance.GetSetting(EnvironmentSettingNames.AzureWebsiteOwnerName) ?? string.Empty;
@@ -172,5 +177,16 @@ public static string RemoveUtf8ByteOrderMark(string input)
172177

173178
return input;
174179
}
180+
181+
public static string ToJson(ExpandoObject value, Formatting formatting = Formatting.Indented)
182+
{
183+
return JsonConvert.SerializeObject(value, formatting, _expandoObjectJsonConverter);
184+
}
185+
186+
public static JObject ToJObject(ExpandoObject value)
187+
{
188+
string json = ToJson(value);
189+
return JObject.Parse(json);
190+
}
175191
}
176192
}

test/WebJobs.Script.Tests/Binding/HttpBindingTests.cs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,9 @@
77
using System.Net;
88
using System.Net.Http;
99
using System.Text;
10+
using System.Threading.Tasks;
1011
using Microsoft.Azure.WebJobs.Script.Binding;
12+
using Newtonsoft.Json.Linq;
1113
using Xunit;
1214

1315
namespace Microsoft.Azure.WebJobs.Script.Tests.Binding
@@ -97,5 +99,27 @@ public void ParseResponseObject_StatusWithNullBody_ReturnsExpectedResult()
9799
Assert.Equal(null, content);
98100
Assert.Equal(HttpStatusCode.Accepted, statusCode);
99101
}
102+
103+
[Fact]
104+
public async Task CreateResultContent_ExpandoObject_ReturnsJsonStringContent()
105+
{
106+
dynamic expandoObject = new ExpandoObject();
107+
expandoObject.name = "Mathew";
108+
expandoObject.location = "Seattle";
109+
110+
StringContent stringContent = HttpBinding.CreateResultContent(expandoObject);
111+
string json = await stringContent.ReadAsStringAsync();
112+
JObject parsed = JObject.Parse(json);
113+
Assert.Equal("Mathew", parsed["name"]);
114+
Assert.Equal("Seattle", parsed["location"]);
115+
Assert.Equal("text/plain", stringContent.Headers.ContentType.MediaType);
116+
117+
stringContent = HttpBinding.CreateResultContent(expandoObject, "application/json");
118+
json = await stringContent.ReadAsStringAsync();
119+
parsed = JObject.Parse(json);
120+
Assert.Equal("Mathew", parsed["name"]);
121+
Assert.Equal("Seattle", parsed["location"]);
122+
Assert.Equal("application/json", stringContent.Headers.ContentType.MediaType);
123+
}
100124
}
101125
}

test/WebJobs.Script.Tests/FunctionBindingTests.cs

Lines changed: 0 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
// Licensed under the MIT License. See License.txt in the project root for license information.
33

44
using System;
5-
using System.Dynamic;
65
using System.IO;
76
using System.Linq;
87
using System.Text;
@@ -174,33 +173,5 @@ public void ReadAsCollection_StringArray_WithBOM()
174173
Assert.Equal("Value2", (string)collection[1]);
175174
Assert.Equal("Value3", (string)collection[2]);
176175
}
177-
178-
[Fact]
179-
public void ToJObject_ReturnsExpectedResult()
180-
{
181-
dynamic child = new ExpandoObject();
182-
child.Name = "Mary";
183-
child.Location = "Seattle";
184-
child.Age = 5;
185-
186-
dynamic parent = new ExpandoObject();
187-
parent.Name = "Bob";
188-
parent.Location = "Seattle";
189-
parent.Age = 40;
190-
parent.Children = new object[] { child };
191-
192-
JObject resultParent = FunctionBinding.ToJObject(parent);
193-
194-
Assert.Equal(resultParent["Name"], parent.Name);
195-
Assert.Equal(resultParent["Location"], parent.Location);
196-
Assert.Equal(resultParent["Age"], parent.Age);
197-
198-
var children = (JArray)resultParent["Children"];
199-
Assert.Equal(1, children.Count);
200-
var resultChild = (JObject)children[0];
201-
Assert.Equal(resultChild["Name"], child.Name);
202-
Assert.Equal(resultChild["Location"], child.Location);
203-
Assert.Equal(resultChild["Age"], child.Age);
204-
}
205176
}
206177
}

test/WebJobs.Script.Tests/UtilityTests.cs

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@
33

44
using System;
55
using System.Collections.Generic;
6-
using System.Globalization;
6+
using System.Dynamic;
77
using System.Text;
8+
using Newtonsoft.Json.Linq;
89
using Xunit;
910

1011
namespace Microsoft.Azure.WebJobs.Script.Tests
@@ -151,5 +152,33 @@ public void RemoveUTF8ByteOrderMark_WithNoBOM_ReturnsOriginalString()
151152
Assert.Equal(inputString.Length, result.Length);
152153
Assert.Equal(inputString, result);
153154
}
155+
156+
[Fact]
157+
public void ToJObject_ReturnsExpectedResult()
158+
{
159+
dynamic child = new ExpandoObject();
160+
child.Name = "Mary";
161+
child.Location = "Seattle";
162+
child.Age = 5;
163+
164+
dynamic parent = new ExpandoObject();
165+
parent.Name = "Bob";
166+
parent.Location = "Seattle";
167+
parent.Age = 40;
168+
parent.Children = new object[] { child };
169+
170+
JObject resultParent = Utility.ToJObject(parent);
171+
172+
Assert.Equal(resultParent["Name"], parent.Name);
173+
Assert.Equal(resultParent["Location"], parent.Location);
174+
Assert.Equal(resultParent["Age"], parent.Age);
175+
176+
var children = (JArray)resultParent["Children"];
177+
Assert.Equal(1, children.Count);
178+
var resultChild = (JObject)children[0];
179+
Assert.Equal(resultChild["Name"], child.Name);
180+
Assert.Equal(resultChild["Location"], child.Location);
181+
Assert.Equal(resultChild["Age"], child.Age);
182+
}
154183
}
155184
}

0 commit comments

Comments
 (0)