Skip to content

Commit 60eba99

Browse files
authored
Ignore casing on property names by default (#261)
* Ignore casing on property names by default change tests to check for case insensitivity more message docs only configure serializer if user has not * fix merge * make sure we always have a serializer * remove unused using statements * configure jsonserializeroptions * updates to test
1 parent 9c91a5f commit 60eba99

File tree

12 files changed

+37
-44
lines changed

12 files changed

+37
-44
lines changed

src/DotNetWorker.Core/Hosting/ServiceCollectionExtensions.cs

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

44
using System;
5-
using System.Threading.Channels;
5+
using System.Text.Json;
6+
using Azure.Core.Serialization;
67
using Microsoft.Azure.Functions.Worker;
78
using Microsoft.Azure.Functions.Worker.Configuration;
89
using Microsoft.Azure.Functions.Worker.Context.Features;
@@ -12,6 +13,7 @@
1213
using Microsoft.Azure.Functions.Worker.Pipeline;
1314
using Microsoft.Extensions.DependencyInjection.Extensions;
1415
using Microsoft.Extensions.Hosting;
16+
using Microsoft.Extensions.Options;
1517

1618
namespace Microsoft.Extensions.DependencyInjection
1719
{
@@ -56,6 +58,16 @@ public static IFunctionsWorkerApplicationBuilder AddFunctionsWorkerCore(this ISe
5658
// Worker initialization service
5759
services.AddSingleton<IHostedService, WorkerHostedService>();
5860

61+
// Default serializer settings
62+
services.AddOptions<WorkerOptions>()
63+
.PostConfigure<IOptions<JsonSerializerOptions>>((workerOptions, serializerOptions) =>
64+
{
65+
if (workerOptions.Serializer is null)
66+
{
67+
workerOptions.Serializer = new JsonObjectSerializer(serializerOptions.Value);
68+
}
69+
});
70+
5971
if (configure != null)
6072
{
6173
services.Configure(configure);

src/DotNetWorker.Core/Hosting/WorkerOptions.cs

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,10 @@ namespace Microsoft.Azure.Functions.Worker
1111
/// </summary>
1212
public class WorkerOptions
1313
{
14-
private ObjectSerializer? _serializer;
15-
1614
/// <summary>
1715
/// The <see cref="ObjectSerializer"/> to use for all JSON serialization and deserialization. By default,
1816
/// this is a default <see cref="JsonObjectSerializer"/> with default <see cref="JsonSerializerOptions"/>.
1917
/// </summary>
20-
public ObjectSerializer Serializer
21-
{
22-
get => _serializer ??= new JsonObjectSerializer();
23-
set => _serializer = value;
24-
}
18+
public ObjectSerializer? Serializer { get; set; }
2519
}
2620
}

src/DotNetWorker/Hosting/ServiceCollectionExtensions.cs

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

44
using System;
5+
using System.Text.Json;
56
using Microsoft.Azure.Functions.Worker;
67

78
namespace Microsoft.Extensions.DependencyInjection
@@ -14,6 +15,7 @@ public static class ServiceCollectionExtensions
1415
/// <summary>
1516
/// Adds the core set of services for the Azure Functions worker.
1617
/// This call also adds the default set of binding converters and gRPC support.
18+
/// This call also adds a default ObjectSerializer that treats property names as case insensitive.
1719
/// </summary>
1820
/// <param name="services">The <see cref="IServiceCollection"/>.</param>
1921
/// <param name="configure">The action used to configure <see cref="WorkerOptions"/>.</param>
@@ -28,6 +30,12 @@ public static IFunctionsWorkerApplicationBuilder AddFunctionsWorkerDefaults(this
2830
// Converters
2931
services.RegisterDefaultConverters();
3032

33+
// Default Json serialization should ignore casing on property names
34+
services.Configure<JsonSerializerOptions>(options =>
35+
{
36+
options.PropertyNameCaseInsensitive = true;
37+
});
38+
3139
// Core services registration
3240
var builder = services.AddFunctionsWorkerCore(configure);
3341

src/DotNetWorker/Hosting/WorkerHostBuilderExtensions.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ public static class WorkerHostBuilderExtensions
1717
/// The following defaults are configured:
1818
/// <list type="bullet">
1919
/// <item><description>A default set of converters.</description></item>
20+
/// <item><description>Set default serializer to ignore casing on property names.</description></item>
2021
/// <item><description>Integration with Azure Functions logging.</description></item>
2122
/// <item><description>Output binding middleware and features.</description></item>
2223
/// <item><description>Function execution middleware.</description></item>

test/DotNetWorkerTests/Configuration/WorkerOptionsTests.cs

Lines changed: 0 additions & 23 deletions
This file was deleted.

test/DotNetWorkerTests/Converters/JsonPocoConverterTests.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System;
55
using System.Collections.Generic;
66
using System.Text;
7+
using System.Text.Json;
78
using Azure.Core.Serialization;
89
using Microsoft.Azure.Functions.Worker.Converters;
910
using Microsoft.Extensions.Options;
@@ -19,6 +20,10 @@ public class JsonPocoConverterTests
1920
public JsonPocoConverterTests()
2021
{
2122
var options = new WorkerOptions();
23+
options.Serializer = new JsonObjectSerializer(new JsonSerializerOptions()
24+
{
25+
PropertyNameCaseInsensitive = true
26+
});
2227
var wrapper = new OptionsWrapper<WorkerOptions>(options);
2328
_jsonPocoConverter = new JsonPocoConverter(wrapper);
2429
}

test/DotNetWorkerTests/DotNetWorkerTests.csproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,4 +27,8 @@
2727
<ProjectReference Include="..\..\src\DotNetWorker\DotNetWorker.csproj" />
2828
</ItemGroup>
2929

30+
<ItemGroup>
31+
<Folder Include="Configuration\" />
32+
</ItemGroup>
33+
3034
</Project>

test/DotNetWorkerTests/HttpRequestDataExtensionsTests.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ private FunctionContext CreateContext(ObjectSerializer serializer = null)
5757

5858
var services = new ServiceCollection();
5959
services.AddOptions();
60+
services.AddFunctionsWorkerDefaults();
6061

6162
if (serializer != null)
6263
{

test/DotNetWorkerTests/HttpResponseDataExtensionsTests.cs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -120,14 +120,12 @@ private FunctionContext CreateContext(ObjectSerializer serializer = null)
120120

121121
var services = new ServiceCollection();
122122
services.AddOptions();
123+
services.AddFunctionsWorkerCore();
123124

124-
if (serializer != null)
125+
services.Configure<WorkerOptions>(c =>
125126
{
126-
services.Configure<WorkerOptions>(c =>
127-
{
128-
c.Serializer = serializer;
129-
});
130-
}
127+
c.Serializer = serializer;
128+
});
131129

132130
context.InstanceServices = services.BuildServiceProvider();
133131

test/E2ETests/E2EApps/E2EApp/Cosmos/CosmosFunction.cs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,16 +38,12 @@ public static object CosmosTrigger([CosmosDBTrigger(
3838

3939
public class MyDocument
4040
{
41-
[JsonPropertyName("id")]
4241
public string Id { get; set; }
4342

44-
[JsonPropertyName("text")]
4543
public string Text { get; set; }
4644

47-
[JsonPropertyName("number")]
4845
public int Number { get; set; }
4946

50-
[JsonPropertyName("boolean")]
5147
public bool Boolean { get; set; }
5248
}
5349
}

0 commit comments

Comments
 (0)