Skip to content

Commit d77986f

Browse files
committed
Updated ScriptJobHostOptions to implement IOptionsFormatter and
support JSON serialization with a custom converter. Added a Format method for serializing options to JSON. Introduced unit tests to validate serialization logic.
1 parent c97b1ec commit d77986f

File tree

2 files changed

+124
-2
lines changed

2 files changed

+124
-2
lines changed

src/WebJobs.Script/Config/ScriptJobHostOptions.cs

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

44
using System;
55
using System.Collections.Generic;
66
using System.Collections.Immutable;
77
using System.Collections.ObjectModel;
8+
using System.Text.Json;
9+
using System.Text.Json.Serialization;
10+
using Microsoft.Azure.WebJobs.Hosting;
811
using Microsoft.Azure.WebJobs.Script.Description;
912
using Microsoft.Azure.WebJobs.Script.Diagnostics.OpenTelemetry;
1013

1114
namespace Microsoft.Azure.WebJobs.Script
1215
{
13-
public class ScriptJobHostOptions
16+
public class ScriptJobHostOptions : IOptionsFormatter
1417
{
18+
private static readonly JsonSerializerOptions _serializerOptions = new()
19+
{
20+
Converters = { new ScriptJobHostOptionsConverter() },
21+
WriteIndented = true,
22+
};
23+
1524
private string _rootScriptPath;
1625
private ImmutableArray<string> _directorySnapshot;
1726

@@ -145,5 +154,30 @@ public string RootScriptPath
145154
/// Gets or sets a value indicating the timeout duration for the function metadata provider.
146155
/// </summary>
147156
public TimeSpan MetadataProviderTimeout { get; set; } = TimeSpan.Zero;
157+
158+
public string Format()
159+
{
160+
return JsonSerializer.Serialize(this, _serializerOptions);
161+
}
162+
163+
private class ScriptJobHostOptionsConverter : JsonConverter<ScriptJobHostOptions>
164+
{
165+
public override ScriptJobHostOptions Read(
166+
ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
167+
{
168+
throw new NotImplementedException();
169+
}
170+
171+
public override void Write(
172+
Utf8JsonWriter writer, ScriptJobHostOptions value, JsonSerializerOptions options)
173+
{
174+
writer.WriteStartObject();
175+
writer.WriteBoolean(nameof(value.FileWatchingEnabled), value.FileWatchingEnabled);
176+
writer.WriteString(nameof(value.FileLoggingMode), value.FileLoggingMode.ToString());
177+
writer.WriteString(nameof(value.FunctionTimeout), value.FunctionTimeout?.ToString());
178+
writer.WriteString(nameof(value.TelemetryMode), value.TelemetryMode.ToString());
179+
writer.WriteEndObject();
180+
}
181+
}
148182
}
149183
}
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
// Copyright (c) .NET Foundation. All rights reserved.
2+
// Licensed under the MIT License. See License.txt in the project root for license information.
3+
4+
using System;
5+
using System.Text.Json;
6+
using AwesomeAssertions;
7+
using Microsoft.Azure.WebJobs.Script.Diagnostics.OpenTelemetry;
8+
using Xunit;
9+
10+
namespace Microsoft.Azure.WebJobs.Script.Tests.Configuration
11+
{
12+
public class ScriptJobHostOptionsTests
13+
{
14+
[Theory]
15+
[InlineData(FileLoggingMode.Always, 10, true, "Always", "00:10:00", "True", "ApplicationInsights")]
16+
[InlineData(FileLoggingMode.Never, 5, false, "Never", "00:05:00", "False", "OpenTelemetry")]
17+
[InlineData(FileLoggingMode.DebugOnly, 0.5, true, "DebugOnly", "00:00:30", "True", "ApplicationInsights")]
18+
public void Format_SerializesOptionsToJson(
19+
FileLoggingMode loggingMode,
20+
double timeoutMinutes,
21+
bool fileWatchingEnabled,
22+
string expectedLoggingMode,
23+
string expectedTimeout,
24+
string expectedFileWatchingEnabled,
25+
string expectedTelemetryMode)
26+
{
27+
var options = new ScriptJobHostOptions
28+
{
29+
FileLoggingMode = loggingMode,
30+
FunctionTimeout = TimeSpan.FromMinutes(timeoutMinutes),
31+
FileWatchingEnabled = fileWatchingEnabled,
32+
TelemetryMode = Enum.Parse<TelemetryMode>(expectedTelemetryMode)
33+
};
34+
35+
string json = options.Format();
36+
var root = JsonDocument.Parse(json).RootElement;
37+
38+
root.TryGetProperty("FileWatchingEnabled", out var fileWatchingProperty).Should().BeTrue();
39+
fileWatchingProperty.GetBoolean().ToString().Should().Be(expectedFileWatchingEnabled);
40+
41+
root.TryGetProperty("FileLoggingMode", out var fileLoggingProperty).Should().BeTrue();
42+
fileLoggingProperty.GetString().Should().Be(expectedLoggingMode);
43+
44+
root.TryGetProperty("FunctionTimeout", out var timeoutProperty).Should().BeTrue();
45+
timeoutProperty.GetString().Should().Be(expectedTimeout);
46+
47+
root.TryGetProperty("TelemetryMode", out var telemetryModeProperty).Should().BeTrue();
48+
telemetryModeProperty.GetString().Should().Be(expectedTelemetryMode);
49+
}
50+
51+
[Fact]
52+
public void Format_WithNullFunctionTimeout_SerializesAsNull()
53+
{
54+
var options = new ScriptJobHostOptions
55+
{
56+
FileLoggingMode = FileLoggingMode.Never,
57+
FileWatchingEnabled = true,
58+
FunctionTimeout = null
59+
};
60+
61+
string json = options.Format();
62+
var root = JsonDocument.Parse(json).RootElement;
63+
64+
root.TryGetProperty("FunctionTimeout", out var timeoutProperty).Should().BeTrue();
65+
timeoutProperty.ValueKind.Should().Be(JsonValueKind.Null);
66+
}
67+
68+
[Fact]
69+
public void Format_ReturnsValidIndentedJson()
70+
{
71+
var options = new ScriptJobHostOptions
72+
{
73+
FileLoggingMode = FileLoggingMode.Always,
74+
FileWatchingEnabled = true,
75+
FunctionTimeout = TimeSpan.FromMinutes(5)
76+
};
77+
78+
string json = options.Format();
79+
80+
// Should be valid JSON
81+
var exception = Record.Exception(() => JsonDocument.Parse(json));
82+
exception.Should().BeNull();
83+
84+
// Should be indented (contains newlines)
85+
json.Should().Contain(Environment.NewLine);
86+
}
87+
}
88+
}

0 commit comments

Comments
 (0)