Fix BodyParser to correctly check for json (#1297)

* Fix BodyParser to correctly check for json

* JsonUtils
This commit is contained in:
Stef Heyenrath
2025-05-21 17:28:29 +02:00
committed by GitHub
parent 1e23c58bf2
commit d628ce2270
7 changed files with 76 additions and 59 deletions

View File

@@ -8,18 +8,8 @@ namespace WireMock.Util;
// ReSharper disable once InconsistentNaming
public static class IBodyDataExtensions
{
public static BodyType GetBodyType(this IBodyData bodyData)
public static BodyType GetDetectedBodyType(this IBodyData bodyData)
{
if (bodyData.DetectedBodyTypeFromContentType is not null and not BodyType.None)
{
return bodyData.DetectedBodyTypeFromContentType.Value;
}
if (bodyData.DetectedBodyType is not null and not BodyType.None)
{
return bodyData.DetectedBodyType.Value;
}
return BodyType.None;
return bodyData.DetectedBodyType ?? BodyType.None;
}
}

View File

@@ -35,7 +35,7 @@ internal static class HttpRequestMessageHelper
}
var bodyData = requestMessage.BodyData;
httpRequestMessage.Content = bodyData?.GetBodyType() switch
httpRequestMessage.Content = bodyData?.DetectedBodyType switch
{
BodyType.Bytes => ByteArrayContentHelper.Create(bodyData.BodyAsBytes!, contentType),
BodyType.Json => StringContentHelper.Create(JsonConvert.SerializeObject(bodyData.BodyAsJson), contentType),

View File

@@ -70,7 +70,7 @@ namespace WireMock.Owin.Mappers
}
var bodyData = responseMessage.BodyData;
if (bodyData?.GetBodyType() == BodyType.SseString)
if (bodyData?.GetDetectedBodyType() == BodyType.SseString)
{
await HandleSseStringAsync(responseMessage, response, bodyData);
return;
@@ -166,7 +166,7 @@ namespace WireMock.Owin.Mappers
private async Task<byte[]?> GetNormalBodyAsync(IResponseMessage responseMessage)
{
var bodyData = responseMessage.BodyData;
switch (bodyData?.GetBodyType())
switch (bodyData?.GetDetectedBodyType())
{
case BodyType.String:
case BodyType.FormUrlEncoded:

View File

@@ -177,7 +177,7 @@ internal static class BodyParser
}
// If string is not null or empty, try to deserialize the string to a JObject
if (settings.DeserializeJson && !string.IsNullOrEmpty(data.BodyAsString))
if (settings.DeserializeJson && JsonUtils.IsJson(data.BodyAsString))
{
try
{
@@ -197,7 +197,7 @@ internal static class BodyParser
return data;
}
private static async Task<(string? ContentType, byte[] Bytes)> ReadBytesAsync(Stream stream, string? contentEncoding = null, bool decompressGZipAndDeflate = true)
{
using var memoryStream = new MemoryStream();

View File

@@ -12,17 +12,23 @@ namespace WireMock.Util;
internal static class JsonUtils
{
public static bool TryParseAsJObject(string? strInput, [NotNullWhen(true)] out JObject? value)
public static bool IsJson(string? value)
{
value = null;
if (strInput == null || string.IsNullOrWhiteSpace(strInput))
if (string.IsNullOrWhiteSpace(value))
{
return false;
}
strInput = strInput.Trim();
if ((!strInput.StartsWith("{") || !strInput.EndsWith("}")) && (!strInput.StartsWith("[") || !strInput.EndsWith("]")))
value = value!.Trim();
return (value.StartsWith("{") && value.EndsWith("}")) || (value.StartsWith("[") && value.EndsWith("]"));
}
public static bool TryParseAsJObject(string? strInput, [NotNullWhen(true)] out JObject? value)
{
value = null;
if (!IsJson(strInput))
{
return false;
}
@@ -30,7 +36,7 @@ internal static class JsonUtils
try
{
// Try to convert this string into a JToken
value = JObject.Parse(strInput);
value = JObject.Parse(strInput!);
return true;
}
catch