fix findings

This commit is contained in:
Stef Heyenrath
2026-05-03 09:51:34 +02:00
parent 811a2cd79e
commit 32d8ecd399
4 changed files with 9 additions and 5 deletions

View File

@@ -276,7 +276,7 @@ public static class WireMockServerSettingsParser
var defaultJsonBodyTransformer = parser.GetStringValue(nameof(WireMockServerSettings.DefaultJsonBodyTransformer));
settings.DefaultJsonBodyTransformer = defaultJsonBodyTransformer switch
{
nameof(SystemTextJsonBodyTransformer) => new SystemTextJsonBodyTransformer(),
nameof(SystemTextJsonBodyTransformer) => new SystemTextJsonBodyTransformer(settings),
_ => new NewtonsoftJsonBodyTransformer(settings),
};
}

View File

@@ -86,6 +86,7 @@ public class NewtonsoftJsonBodyTransformer(WireMockServerSettings settings) : IJ
}
catch
{
settings.Logger.Warn("Failed to parse string ''{0}'' as JSON. Returning the original string value.", stringValue);
}
}

View File

@@ -6,6 +6,7 @@ using System.Text.Json.Nodes;
using HandlebarsDotNet.Helpers.Models;
using JetBrains.Annotations;
using JsonConverter.System.Text.Json;
using WireMock.Settings;
using WireMock.Types;
using WireMock.Util;
@@ -15,7 +16,7 @@ namespace WireMock.Transformers;
/// JSON body transformer implementation based on System.Text.Json.
/// </summary>
[PublicAPI]
public class SystemTextJsonBodyTransformer() : IJsonBodyTransformer
public class SystemTextJsonBodyTransformer(WireMockServerSettings settings) : IJsonBodyTransformer
{
private static readonly SystemTextJsonConverter _jsonConverter = new();
@@ -85,7 +86,7 @@ public class SystemTextJsonBodyTransformer() : IJsonBodyTransformer
}
catch
{
// Ignore and return as string.
settings.Logger.Warn("Failed to parse string ''{0}'' as JSON. Returning the original string value.", stringValue);
}
}

View File

@@ -19,15 +19,17 @@ public class JsonBodyTransformerTests
{
get
{
var settings = new WireMockServerSettings();
return
[
new JsonBodyTransformerTestContext(
() => new NewtonsoftJsonBodyTransformer(new WireMockServerSettings()),
() => new NewtonsoftJsonBodyTransformer(settings),
JObject.Parse,
body => ((JToken)body).ToString(Formatting.None)),
new JsonBodyTransformerTestContext(
() => new SystemTextJsonBodyTransformer(),
() => new SystemTextJsonBodyTransformer(settings),
json => JsonNode.Parse(json)!,
body => ((JsonNode)body).ToJsonString())
];