From 3d7b89592b12b2d98ec2f8360511ad2f9e5099e6 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Sat, 28 Jun 2025 17:10:51 -0500 Subject: move json serialization options and logic into WebhookService to guard against regressions --- src/slskd/Integrations/Webhooks/WebhookService.cs | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) (limited to 'src/slskd/Integrations/Webhooks/WebhookService.cs') diff --git a/src/slskd/Integrations/Webhooks/WebhookService.cs b/src/slskd/Integrations/Webhooks/WebhookService.cs index 62f097b7..b08b0484 100644 --- a/src/slskd/Integrations/Webhooks/WebhookService.cs +++ b/src/slskd/Integrations/Webhooks/WebhookService.cs @@ -25,6 +25,9 @@ using System.Linq; using System.Net; using System.Net.Http; using System.Text; +using System.Text.Encodings.Web; +using System.Text.Json; +using System.Text.Json.Serialization; using System.Threading.Tasks; using Serilog; using slskd.Events; @@ -40,6 +43,15 @@ public class WebhookService OptionsMonitor = optionsMonitor; HttpClientFactory = httpClientFactory; + var json = new JsonSerializerOptions(); + json.Converters.Add(new IPAddressConverter()); + json.Converters.Add(new JsonStringEnumConverter()); + json.PropertyNamingPolicy = JsonNamingPolicy.CamelCase; + json.DefaultIgnoreCondition = JsonIgnoreCondition.Never; // include properties with null values + json.Encoder = JavaScriptEncoder.Default; // IMPORTANT! don't change this to 'UnsafeRelaxedJsonEscaping'; as the name implies, it's unsafe! + + JsonSerializerOptions = json; + Events.Subscribe(nameof(WebhookService), HandleEvent); Log.Debug("{Service} initialized", nameof(WebhookService)); @@ -49,6 +61,7 @@ public class WebhookService private IOptionsMonitor OptionsMonitor { get; } private EventBus Events { get; } private IHttpClientFactory HttpClientFactory { get; } + private JsonSerializerOptions JsonSerializerOptions { get; } private async Task HandleEvent(Event data) { @@ -80,7 +93,10 @@ public class WebhookService http.DefaultRequestHeaders.TryAddWithoutValidation(header.Name, header.Value); } - var content = new StringContent(data.ToJson(), Encoding.UTF8, "application/json"); + var content = new StringContent( + content: JsonSerializer.Serialize(data, JsonSerializerOptions), + encoding: Encoding.UTF8, + mediaType: "application/json"); HttpStatusCode? statusCode = null; -- cgit v1.2.3 From 537f9363b966f2b183c7b312c1299dba1c78bab8 Mon Sep 17 00:00:00 2001 From: JP Dillingham Date: Sun, 6 Jul 2025 16:15:40 -0500 Subject: fix regression in scripts and webhooks causing json to be missing detail --- src/slskd/Integrations/Scripts/ScriptService.cs | 6 +++++- src/slskd/Integrations/Webhooks/WebhookService.cs | 5 ++++- 2 files changed, 9 insertions(+), 2 deletions(-) (limited to 'src/slskd/Integrations/Webhooks/WebhookService.cs') diff --git a/src/slskd/Integrations/Scripts/ScriptService.cs b/src/slskd/Integrations/Scripts/ScriptService.cs index 109cf5ff..f5cc4d4c 100644 --- a/src/slskd/Integrations/Scripts/ScriptService.cs +++ b/src/slskd/Integrations/Scripts/ScriptService.cs @@ -180,7 +180,11 @@ public class ScriptService Log.Debug("Running script '{Script}': \"{Executable}\" {Args} (id: {ProcessId})", script.Key, executable, run.Args ?? string.Join(' ', run.Arglist ?? []), processId); var sw = Stopwatch.StartNew(); - process.StartInfo.EnvironmentVariables["SLSKD_SCRIPT_DATA"] = JsonSerializer.Serialize(data, JsonSerializerOptions); + process.StartInfo.EnvironmentVariables["SLSKD_SCRIPT_DATA"] = JsonSerializer.Serialize( + value: data, + inputType: data.GetType(), // if omitted object is serialized as EventType, losing everything else + options: JsonSerializerOptions); + process.Start(); process.WaitForExit(); diff --git a/src/slskd/Integrations/Webhooks/WebhookService.cs b/src/slskd/Integrations/Webhooks/WebhookService.cs index b08b0484..62739d7a 100644 --- a/src/slskd/Integrations/Webhooks/WebhookService.cs +++ b/src/slskd/Integrations/Webhooks/WebhookService.cs @@ -94,7 +94,10 @@ public class WebhookService } var content = new StringContent( - content: JsonSerializer.Serialize(data, JsonSerializerOptions), + content: JsonSerializer.Serialize( + value: data, + inputType: data.GetType(), // if omitted object is serialized as EventType, losing everything else + options: JsonSerializerOptions), encoding: Encoding.UTF8, mediaType: "application/json"); -- cgit v1.2.3