This commit is contained in:
Stef Heyenrath
2026-04-26 18:59:33 +02:00
parent 64d3e4cbf3
commit 1e07f3f7f3
9 changed files with 36 additions and 69 deletions
@@ -1,10 +1,12 @@
// Copyright © WireMock.Net
using System.Collections;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Stef.Validation;
using WireMock.Extensions;
using WireMock.Serialization;
using WireMock.Util;
using JsonUtils = WireMock.Util.JsonUtils;
namespace WireMock.Matchers;
@@ -68,7 +70,7 @@ public class JsonMatcher : IJsonMatcher
Regex = regex;
Value = value;
_valueAsJToken = JsonUtils.ConvertValueToJToken(value);
_valueAsJToken = ConvertValueToJToken(value);
}
/// <inheritdoc />
@@ -82,7 +84,7 @@ public class JsonMatcher : IJsonMatcher
{
try
{
var inputAsJToken = JsonUtils.ConvertValueToJToken(input);
var inputAsJToken = ConvertValueToJToken(input);
var match = IsMatch(RenameJToken(_valueAsJToken), RenameJToken(inputAsJToken));
score = MatchScores.ToScore(match);
@@ -102,7 +104,7 @@ public class JsonMatcher : IJsonMatcher
return $"new {Name}" +
$"(" +
$"{MatchBehaviour.GetFullyQualifiedEnumValue()}, " +
$"{CSharpFormatter.ConvertToAnonymousObjectDefinition(Value, 3)}, " +
$"{CSharpFormatter.ConvertToAnonymousObjectDefinition(Value, 3)}, " +
$"{CSharpFormatter.ToCSharpBooleanLiteral(IgnoreCase)}, " +
$"{CSharpFormatter.ToCSharpBooleanLiteral(Regex)}" +
$")";
@@ -240,6 +242,18 @@ public class JsonMatcher : IJsonMatcher
return new JObject(renamedProperties);
}
private static JToken ConvertValueToJToken(object value)
{
// Check if JToken, string, IEnumerable or object
return value switch
{
JToken tokenValue => tokenValue,
string stringValue => JsonConvert.DeserializeObject<JToken>(stringValue, JsonSerializationConstants.JsonDeserializerSettingsWithDateParsingNone)!,
IEnumerable enumerableValue => JArray.FromObject(enumerableValue),
_ => JObject.FromObject(value),
};
}
private static string? ToUpper(string? input)
{
return input?.ToUpperInvariant();
@@ -197,7 +197,7 @@ public partial class Response : IResponseBuilder
if (ProxyAndRecordSettings != null && _httpClientForProxy != null)
{
string RemoveFirstOccurrence(string source, string find)
static string RemoveFirstOccurrence(string source, string find)
{
int place = source.IndexOf(find, StringComparison.OrdinalIgnoreCase);
return place >= 0 ? source.Remove(place, find.Length) : source;
@@ -265,7 +265,7 @@ public partial class Response : IResponseBuilder
var decoded = await protoBufMatcher.DecodeAsync(requestMessage.BodyData?.BodyAsBytes).ConfigureAwait(false);
if (decoded != null)
{
requestMessageImplementation.BodyAsJson = JsonUtils.ConvertValueToJToken(decoded);
requestMessageImplementation.BodyAsJson = settings.DefaultJsonSerializer.ToJsonToken(decoded);
}
}
}
@@ -914,16 +914,16 @@ public partial class WireMockServer
};
}
private static T DeserializeObject<T>(IRequestMessage requestMessage)
private T DeserializeObject<T>(IRequestMessage requestMessage)
{
switch (requestMessage.BodyData?.DetectedBodyType)
{
case BodyType.String:
case BodyType.FormUrlEncoded:
return JsonUtils.DeserializeObject<T>(requestMessage.BodyData.BodyAsString!);
case BodyType.String when requestMessage.BodyData?.BodyAsString != null:
case BodyType.FormUrlEncoded when requestMessage.BodyData?.BodyAsString != null:
return _settings.DefaultJsonSerializer.Deserialize<T>(requestMessage.BodyData.BodyAsString)!;
case BodyType.Json when requestMessage.BodyData?.BodyAsJson != null:
return ((JObject)requestMessage.BodyData.BodyAsJson).ToObject<T>()!;
return _settings.DefaultJsonSerializer.ParseJsonToken<T>(requestMessage.BodyData.BodyAsJson)!;
default:
throw new NotSupportedException();
@@ -1,6 +1,5 @@
// Copyright © WireMock.Net
using System.Linq;
using Stef.Validation;
using WireMock.Admin.Mappings;
using WireMock.Matchers;
@@ -153,7 +152,7 @@ public partial class WireMockServer
}
else
{
var clientIPModel = JsonUtils.ParseJTokenToObject<ClientIPModel>(requestModel.ClientIP);
var clientIPModel = _settings.DefaultJsonSerializer.ParseJsonToken<ClientIPModel>(requestModel.ClientIP);
if (clientIPModel.Matchers != null)
{
requestBuilder = requestBuilder.WithPath(clientIPModel.Matchers.Select(_matcherMapper.Map).OfType<IStringMatcher>().ToArray());
@@ -169,7 +168,7 @@ public partial class WireMockServer
}
else
{
var pathModel = JsonUtils.ParseJTokenToObject<PathModel>(requestModel.Path);
var pathModel = _settings.DefaultJsonSerializer.ParseJsonToken<PathModel>(requestModel.Path);
if (pathModel.Matchers != null)
{
var matchOperator = StringUtils.ParseMatchOperator(pathModel.MatchOperator);
@@ -185,7 +184,7 @@ public partial class WireMockServer
}
else
{
var urlModel = JsonUtils.ParseJTokenToObject<UrlModel>(requestModel.Url);
var urlModel = _settings.DefaultJsonSerializer.ParseJsonToken<UrlModel>(requestModel.Url);
if (urlModel.Matchers != null)
{
var matchOperator = StringUtils.ParseMatchOperator(urlModel.MatchOperator);
@@ -271,7 +270,7 @@ public partial class WireMockServer
return requestBuilder;
}
private static IResponseBuilder InitResponseBuilder(ResponseModel responseModel)
private IResponseBuilder InitResponseBuilder(ResponseModel responseModel)
{
var responseBuilder = Response.Create();
@@ -336,7 +335,7 @@ public partial class WireMockServer
}
else
{
var headers = JsonUtils.ParseJTokenToObject<string[]>(entry.Value);
var headers = _settings.DefaultJsonSerializer.ParseJsonToken<string[]>(entry.Value);
responseBuilder.WithHeader(entry.Key, headers);
}
}
@@ -362,7 +361,7 @@ public partial class WireMockServer
}
else
{
var headers = JsonUtils.ParseJTokenToObject<string[]>(entry.Value);
var headers = _settings.DefaultJsonSerializer.ParseJsonToken<string[]>(entry.Value);
responseBuilder.WithTrailingHeader(entry.Key, headers);
}
}