Skip to content

Commit 2be59b9

Browse files
improve: SettingsData.FromJson null safety + GatewayUrlHelper static array (#148)
Two small coding improvements: 1. SettingsData.FromJson(string? json) — null/empty guard - Change parameter from non-nullable 'string' to 'string?' - Return null immediately for null or empty input rather than forwarding to JsonSerializer.Deserialize which would throw ArgumentNullException (not caught by the existing JsonException handler) - Adds two regression tests: null and empty string both return null 2. GatewayUrlHelper.RemoveUserInfo — static readonly char array - Replace the inline 'new[] { '/', '?', '#' }' array literal in RemoveUserInfo with a 'private static readonly' field - The method is called on every URL normalisation and display sanitisation; the previous form allocated a new char array on each call, which is now eliminated Co-authored-by: github-actions[bot] <github-actions[bot]@users.noreply.github.com> Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
1 parent b202642 commit 2be59b9

File tree

3 files changed

+14
-2
lines changed

3 files changed

+14
-2
lines changed

src/OpenClaw.Shared/GatewayUrlHelper.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ public static class GatewayUrlHelper
66
{
77
public const string ValidationMessage = "Gateway URL must be a valid URL (ws://, wss://, http://, or https://).";
88

9+
private static readonly char[] s_authorityTerminators = { '/', '?', '#' };
10+
911
public static bool IsValidGatewayUrl(string? gatewayUrl) =>
1012
TryNormalizeWebSocketUrl(gatewayUrl, out _);
1113

@@ -141,7 +143,7 @@ private static string RemoveUserInfo(string url)
141143
}
142144

143145
var authorityStart = schemeSeparator + 3;
144-
var authorityEnd = url.IndexOfAny(new[] { '/', '?', '#' }, authorityStart);
146+
var authorityEnd = url.IndexOfAny(s_authorityTerminators, authorityStart);
145147
if (authorityEnd < 0)
146148
{
147149
authorityEnd = url.Length;

src/OpenClaw.Shared/SettingsData.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,10 @@ public class SettingsData
3737

3838
public string ToJson() => JsonSerializer.Serialize(this, s_options);
3939

40-
public static SettingsData? FromJson(string json)
40+
public static SettingsData? FromJson(string? json)
4141
{
42+
if (string.IsNullOrEmpty(json))
43+
return null;
4244
try
4345
{
4446
return JsonSerializer.Deserialize<SettingsData>(json);

tests/OpenClaw.Tray.Tests/SettingsRoundTripTests.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,14 @@ public void InvalidJson_ReturnsNull()
170170
Assert.Null(SettingsData.FromJson("not json at all"));
171171
}
172172

173+
[Theory]
174+
[InlineData(null)]
175+
[InlineData("")]
176+
public void NullOrEmptyJson_ReturnsNull(string? json)
177+
{
178+
Assert.Null(SettingsData.FromJson(json));
179+
}
180+
173181
[Fact]
174182
public void EmptyUserRules_RoundTrips()
175183
{

0 commit comments

Comments
 (0)