Skip to content

Commit 68cab91

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 68cab91

File tree

2 files changed

+123
-2
lines changed

2 files changed

+123
-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: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
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", "ApplicationInsights")]
16+
[InlineData(FileLoggingMode.Never, 5, false, "Never", "00:05:00", "OpenTelemetry")]
17+
[InlineData(FileLoggingMode.DebugOnly, 0.5, true, "DebugOnly", "00:00:30", "ApplicationInsights")]
18+
public void Format_SerializesOptionsToJson(
19+
FileLoggingMode loggingMode,
20+
double timeoutMinutes,
21+
bool fileWatchingEnabled,
22+
string expectedLoggingMode,
23+
string expectedTimeout,
24+
string expectedTelemetryMode)
25+
{
26+
var options = new ScriptJobHostOptions
27+
{
28+
FileLoggingMode = loggingMode,
29+
FunctionTimeout = TimeSpan.FromMinutes(timeoutMinutes),
30+
FileWatchingEnabled = fileWatchingEnabled,
31+
TelemetryMode = Enum.Parse<TelemetryMode>(expectedTelemetryMode)
32+
};
33+
34+
string json = options.Format();
35+
var root = JsonDocument.Parse(json).RootElement;
36+
37+
root.TryGetProperty("FileWatchingEnabled", out var fileWatchingProperty).Should().BeTrue();
38+
fileWatchingProperty.GetBoolean().Should().Be(fileWatchingEnabled);
39+
40+
root.TryGetProperty("FileLoggingMode", out var fileLoggingProperty).Should().BeTrue();
41+
fileLoggingProperty.GetString().Should().Be(expectedLoggingMode);
42+
43+
root.TryGetProperty("FunctionTimeout", out var timeoutProperty).Should().BeTrue();
44+
timeoutProperty.GetString().Should().Be(expectedTimeout);
45+
46+
root.TryGetProperty("TelemetryMode", out var telemetryModeProperty).Should().BeTrue();
47+
telemetryModeProperty.GetString().Should().Be(expectedTelemetryMode);
48+
}
49+
50+
[Fact]
51+
public void Format_WithNullFunctionTimeout_SerializesAsNull()
52+
{
53+
var options = new ScriptJobHostOptions
54+
{
55+
FileLoggingMode = FileLoggingMode.Never,
56+
FileWatchingEnabled = true,
57+
FunctionTimeout = null
58+
};
59+
60+
string json = options.Format();
61+
var root = JsonDocument.Parse(json).RootElement;
62+
63+
root.TryGetProperty("FunctionTimeout", out var timeoutProperty).Should().BeTrue();
64+
timeoutProperty.ValueKind.Should().Be(JsonValueKind.Null);
65+
}
66+
67+
[Fact]
68+
public void Format_ReturnsValidIndentedJson()
69+
{
70+
var options = new ScriptJobHostOptions
71+
{
72+
FileLoggingMode = FileLoggingMode.Always,
73+
FileWatchingEnabled = true,
74+
FunctionTimeout = TimeSpan.FromMinutes(5)
75+
};
76+
77+
string json = options.Format();
78+
79+
// Should be valid JSON
80+
var exception = Record.Exception(() => JsonDocument.Parse(json));
81+
exception.Should().BeNull();
82+
83+
// Should be indented (contains newlines)
84+
json.Should().Contain(Environment.NewLine);
85+
}
86+
}
87+
}

0 commit comments

Comments
 (0)