Skip to content

Commit 9c30103

Browse files
fix casing issue when sql table is case insensitive (#235)
* add lowercasing of jobject properties * add comments * check for c# type * Refactor * remove unused * fix Co-authored-by: chgagnon <chgagnon@microsoft.com>
1 parent 3452251 commit 9c30103

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed

src/SqlAsyncCollector.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,12 @@ private static void GenerateDataQueryForMerge(TableInformation table, IEnumerabl
315315
else
316316
{
317317
// ToDo: add check for duplicate primary keys once we find a way to get primary keys.
318+
// JObjects ignore serializer settings (https://web.archive.org/web/20171005181503/http://json.codeplex.com/workitem/23853)
319+
// so we have to manually convert property names to lower case before inserting into the query in that case
320+
if (table.Comparer == StringComparer.OrdinalIgnoreCase)
321+
{
322+
(row as JObject).LowercasePropertyNames();
323+
}
318324
rowsToUpsert.Add(row);
319325
}
320326
}

src/Utils.cs

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

44
using System;
5+
using System.Linq;
56
using Microsoft.Extensions.Configuration;
7+
using MoreLinq;
8+
using Newtonsoft.Json.Linq;
69

710
namespace Microsoft.Azure.WebJobs.Extensions.Sql
811
{
@@ -60,5 +63,35 @@ private static bool AsBool(this string str, bool defaultValue = false)
6063
return defaultValue;
6164
}
6265
}
66+
67+
/// <summary>
68+
/// Recursively converts the property names for a JObject to lowercase
69+
/// </summary>
70+
/// <param name="obj">The JToken to convert</param>
71+
public static void LowercasePropertyNames(this JToken token)
72+
{
73+
if (token is JObject jObj)
74+
{
75+
token.LowercasePropertyNames();
76+
}
77+
else if (token is JArray jArray)
78+
{
79+
token.ForEach(x => x.LowercasePropertyNames());
80+
}
81+
}
82+
83+
/// <summary>
84+
/// Recursively converts the property names for a JObject to lowercase
85+
/// </summary>
86+
/// <param name="obj">The JObject to convert</param>
87+
public static void LowercasePropertyNames(this JObject obj)
88+
{
89+
foreach (JProperty property in obj.Properties().ToList())
90+
{
91+
property.Value.LowercasePropertyNames();
92+
// properties are read-only, so we have to replace them
93+
property.Replace(new JProperty(property.Name.ToLowerInvariant(), property.Value));
94+
}
95+
}
6396
}
6497
}

0 commit comments

Comments
 (0)