This commit is contained in:
Stef Heyenrath
2026-04-19 11:05:55 +02:00
parent 65bf469906
commit 21cc70e77b
9 changed files with 46 additions and 64 deletions

View File

@@ -25,7 +25,7 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="JsonConverter.Abstractions" Version="0.9.0" />
<PackageReference Include="JsonConverter.Abstractions" Version="0.10.0" />
</ItemGroup>
<ItemGroup>

View File

@@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<Description>A SystemTextJsonPathMatcher which can be used to match WireMock.Net Requests using JSONPath with System.Text.Json.</Description>
<Description>A SystemTextJsonPathMatcher which can be used to match WireMock.Net Requests using JsonPath.Net.</Description>
<AssemblyTitle>WireMock.Net.Matchers.SystemTextJsonPath</AssemblyTitle>
<Authors>Stef Heyenrath</Authors>
<TargetFrameworks>netstandard2.0;net8.0</TargetFrameworks>

View File

@@ -1,55 +1,31 @@
// Copyright © WireMock.Net
using JsonConverter.Abstractions;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
#if NETSTANDARD2_0_OR_GREATER || NETCOREAPP3_1_OR_GREATER || NET6_0_OR_GREATER || NET461
using System.Text.Json;
#endif
using JsonConverter.Abstractions.Models;
namespace WireMock.Serialization;
internal class MappingSerializer(IJsonConverter jsonConverter)
{
private static readonly JsonConverterOptions JsonConverterOptions = new JsonConverterOptions
{
DateParseHandling = (int) DateParseHandling.None
};
internal T[] DeserializeJsonToArray<T>(string value)
{
// DeserializeObject
return DeserializeObjectToArray<T>(jsonConverter.Deserialize<object>(value, JsonConverterOptions)!);
switch (JsonTypeHelper.GetJsonType(value))
{
case JsonType.Array:
return jsonConverter.Deserialize<T[]>(value, JsonSerializationConstants.JsonConverterOptionsWithDateParsingNone)!;
case JsonType.Object:
var singleResult = jsonConverter.Deserialize<T>(value, JsonSerializationConstants.JsonConverterOptionsWithDateParsingNone);
return [singleResult!];
default:
throw new InvalidOperationException("Cannot deserialize the provided value to an array or object.");
}
}
internal static T[] DeserializeObjectToArray<T>(object value)
internal T[] DeserializeObjectToArray<T>(object value)
{
if (value is JArray jArray)
{
return jArray.ToObject<T[]>()!;
}
if (value is JObject jObject)
{
var singleResult = jObject.ToObject<T>();
return [singleResult!];
}
#if NETSTANDARD2_0_OR_GREATER || NETCOREAPP3_1_OR_GREATER || NET6_0_OR_GREATER || NET461
if (value is JsonElement jElement)
{
if (jElement.ValueKind == JsonValueKind.Array)
{
return jElement.Deserialize<T[]>()!;
}
if (jElement.ValueKind == JsonValueKind.Object)
{
var singleResult = jElement.Deserialize<T>();
return [singleResult!];
}
}
#endif
throw new InvalidOperationException("Cannot deserialize the provided value to an array or object.");
var json = jsonConverter.Serialize(value, JsonSerializationConstants.JsonConverterOptionsWithDateParsingNone);
return DeserializeJsonToArray<T>(json);
}
}

View File

@@ -338,7 +338,7 @@ public partial class WireMockServer
}
o.CorsPolicyOptions = corsPolicyOptions;
o.ClientCertificateMode = (Microsoft.AspNetCore.Server.Kestrel.Https.ClientCertificateMode) _settings.ClientCertificateMode;
o.ClientCertificateMode = (Microsoft.AspNetCore.Server.Kestrel.Https.ClientCertificateMode)_settings.ClientCertificateMode;
o.AcceptAnyClientCertificate = _settings.AcceptAnyClientCertificate;
});
@@ -834,6 +834,18 @@ public partial class WireMockServer
};
}
private T[] DeserializeRequestMessageToArray<T>(IRequestMessage requestMessage)
{
if (requestMessage.BodyData?.DetectedBodyType == BodyType.Json && requestMessage.BodyData.BodyAsJson != null)
{
var bodyAsJson = requestMessage.BodyData.BodyAsJson!;
return _mappingSerializer.DeserializeObjectToArray<T>(bodyAsJson);
}
throw new NotSupportedException();
}
private static Encoding? ToEncoding(EncodingModel? encodingModel)
{
return encodingModel != null ? Encoding.GetEncoding(encodingModel.CodePage) : null;
@@ -853,18 +865,6 @@ public partial class WireMockServer
};
}
private static T[] DeserializeRequestMessageToArray<T>(IRequestMessage requestMessage)
{
if (requestMessage.BodyData?.DetectedBodyType == BodyType.Json && requestMessage.BodyData.BodyAsJson != null)
{
var bodyAsJson = requestMessage.BodyData.BodyAsJson!;
return MappingSerializer.DeserializeObjectToArray<T>(bodyAsJson);
}
throw new NotSupportedException();
}
private static T DeserializeObject<T>(IRequestMessage requestMessage)
{
switch (requestMessage.BodyData?.DetectedBodyType)

View File

@@ -25,7 +25,7 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="JsonConverter.Newtonsoft.Json" Version="0.9.0" />
<PackageReference Include="JsonConverter.Newtonsoft.Json" Version="0.10.0" />
<PackageReference Include="NUnit" Version="4.4.0" />
<PackageReference Include="Stef.Validation" Version="0.2.0" />
</ItemGroup>

View File

@@ -33,7 +33,7 @@
</PropertyGroup>
<ItemGroup>
<PackageReference Include="JsonConverter.Newtonsoft.Json" Version="0.9.0" />
<PackageReference Include="JsonConverter.Newtonsoft.Json" Version="0.10.0" />
<PackageReference Include="RestEase" Version="1.6.4" />
<PackageReference Include="Stef.Validation" Version="0.2.0" />
</ItemGroup>

View File

@@ -14,11 +14,17 @@ internal static class JsonSerializationConstants
IgnoreNullValues = true
};
//internal static readonly JsonSerializerSettings JsonSerializerSettingsDefault = new()
//{
// Formatting = Formatting.Indented,
// NullValueHandling = NullValueHandling.Ignore
//};
internal static readonly JsonConverterOptions JsonConverterOptionsIncludeNullValues = new()
{
WriteIndented = true,
IgnoreNullValues = false
};
internal static readonly JsonConverterOptions JsonConverterOptionsWithDateParsingNone = new()
{
WriteIndented = true,
DateParseHandling = 0
};
internal static readonly JsonSerializerSettings JsonSerializerSettingsIncludeNullValues = new()
{

View File

@@ -30,7 +30,7 @@
<PackageReference Include="Stef.Validation" Version="0.2.0" />
<PackageReference Include="AnyOf" Version="0.5.0.1" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="8.0.2" />
<PackageReference Include="JsonConverter.Newtonsoft.Json" Version="0.9.0" />
<PackageReference Include="JsonConverter.Newtonsoft.Json" Version="0.10.0" />
</ItemGroup>
<ItemGroup>

View File

@@ -76,7 +76,7 @@
</PackageReference>
<PackageReference Include="Moq" Version="4.20.72" />
<PackageReference Include="SimMetrics.Net" Version="1.0.5" />
<PackageReference Include="JsonConverter.System.Text.Json" Version="0.8.0" />
<PackageReference Include="JsonConverter.System.Text.Json" Version="0.10.0" />
<PackageReference Include="Google.Protobuf" Version="3.33.5" />
<PackageReference Include="Grpc.Net.Client" Version="2.76.0" />
<PackageReference Include="Grpc.Tools" Version="2.78.0">