Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="JsonConverter.Abstractions" Version="0.8.0" />
<PackageReference Include="JsonConverter.Abstractions" Version="0.9.0" />
</ItemGroup>

<ItemGroup>
Expand Down
9 changes: 8 additions & 1 deletion src/WireMock.Net.Minimal/Serialization/MappingSerializer.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// 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;
Expand All @@ -10,9 +11,15 @@ 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)
{
return DeserializeObjectToArray<T>(jsonConverter.Deserialize<object>(value)!);
// DeserializeObject
return DeserializeObjectToArray<T>(jsonConverter.Deserialize<object>(value, JsonConverterOptions)!);
}

internal static T[] DeserializeObjectToArray<T>(object value)
Expand Down
4 changes: 0 additions & 4 deletions src/WireMock.Net.Minimal/Server/WireMockServer.Admin.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
// Copyright © WireMock.Net

using System.Linq;
using System.Net;
using System.Text;
using JetBrains.Annotations;
Expand All @@ -26,9 +25,6 @@

namespace WireMock.Server;

/// <summary>
/// The fluent mock server.
/// </summary>
public partial class WireMockServer
{
private const int EnhancedFileSystemWatcherTimeoutMs = 1000;
Expand Down
2 changes: 1 addition & 1 deletion src/WireMock.Net.NUnit/WireMock.Net.NUnit.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="JsonConverter.Newtonsoft.Json" Version="0.8.0" />
<PackageReference Include="JsonConverter.Newtonsoft.Json" Version="0.9.0" />
<PackageReference Include="NUnit" Version="4.4.0" />
<PackageReference Include="Stef.Validation" Version="0.2.0" />
</ItemGroup>
Expand Down
2 changes: 1 addition & 1 deletion src/WireMock.Net.RestClient/WireMock.Net.RestClient.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
</PropertyGroup>

<ItemGroup>
<PackageReference Include="JsonConverter.Newtonsoft.Json" Version="0.8.0" />
<PackageReference Include="JsonConverter.Newtonsoft.Json" Version="0.9.0" />
<PackageReference Include="RestEase" Version="1.6.4" />
<PackageReference Include="Stef.Validation" Version="0.2.0" />
</ItemGroup>
Expand Down
2 changes: 1 addition & 1 deletion src/WireMock.Net.Shared/WireMock.Net.Shared.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -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.8.0" />
<PackageReference Include="JsonConverter.Newtonsoft.Json" Version="0.9.0" />
</ItemGroup>

<ItemGroup>
Expand Down
89 changes: 89 additions & 0 deletions test/WireMock.Net.Tests/Serialization/MappingSerializerTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
using JsonConverter.Newtonsoft.Json;
using WireMock.Admin.Mappings;
using WireMock.Serialization;
using Newtonsoft.Json.Linq;

#if NET8_0_OR_GREATER
using JsonConverter.System.Text.Json;
#endif
Expand Down Expand Up @@ -319,5 +321,92 @@ public void MappingSerializer_DeserializeJsonToArray_WithSystemTextJson_Primitiv
act.Should().Throw<InvalidOperationException>()
.WithMessage("Cannot deserialize the provided value to an array or object.");
}

[Fact]
public void MappingSerializer_DeserializeJsonToArray_WithNewtonsoftJson_DateTimeStringInQueryParamExactMatcherPattern_ShouldPreservePatternAsString()
{
// Arrange
var jsonConverter = new NewtonsoftJsonConverter();
var serializer = new MappingSerializer(jsonConverter);
var mappingJson =
"""
{
"Guid": "12345678-1234-1234-1234-aaaaaaaaaaaa",
"Request": {
"Path": "/api/report",
"Methods": ["GET"],
"Params": [
{
"Name": "asOfDate",
"Matchers": [
{
"Name": "ExactMatcher",
"Pattern": "2021-11-10T13:39:13.705"
}
]
}
]
},
"Response": {
"StatusCode": 200
}
}
""";

// Act
var result = serializer.DeserializeJsonToArray<MappingModel>(mappingJson);

// Assert
result.Should().HaveCount(1);
var matcher = result[0].Request!.Params![0].Matchers![0];
matcher.Name.Should().Be("ExactMatcher");
matcher.Pattern.Should().BeOfType<string>()
.Which.Should().Be("2021-11-10T13:39:13.705",
"datetime-format strings in ExactMatcher Pattern fields must survive deserialization as strings, " +
"not be auto-converted to DateTime by Newtonsoft.Json's DateParseHandling.DateTime");
}

[Fact]
public void MappingSerializer_DeserializeJsonToArray_WithNewtonsoftJson_DateTimeStringInJsonMatcherBodyPattern_ShouldPreservePatternAsString()
{
// Arrange
var jsonConverter = new NewtonsoftJsonConverter();
var serializer = new MappingSerializer(jsonConverter);
// Pattern is an INLINE JSON object (not a string) - this is how WireMock mapping files store
// JsonMatcher patterns when recorded. Newtonsoft with DateParseHandling.DateTime will convert
// the datetime value inside the JObject to JTokenType.Date during deserialization.
var mappingJson =
"""
{
"Guid": "12345678-1234-1234-1234-bbbbbbbbbbbb",
"Request": {
"Path": "/api/report",
"Methods": ["POST"],
"Body": {
"Matcher": {
"Name": "JsonMatcher",
"Pattern": {"Date": "2021-09-30T00:00:00Z", "Names": ["Cash"]}
}
}
},
"Response": {
"StatusCode": 200
}
}
""";

// Act
var result = serializer.DeserializeJsonToArray<MappingModel>(mappingJson);

// Assert - datetime values inside the JObject pattern must remain JTokenType.String.
result.Should().HaveCount(1);
var matcher = result[0].Request!.Body!.Matcher!;
matcher.Name.Should().Be("JsonMatcher");
var patternJObject = matcher.Pattern.Should().BeOfType<JObject>().Subject;
patternJObject["Date"]!.Type.Should().Be(JTokenType.String,
"datetime-format strings inside an inline JsonMatcher body pattern must retain JTokenType.String " +
"after deserialization; if DateParseHandling.DateTime auto-converts them to JTokenType.Date, " +
"JToken.DeepEquals will fail against incoming request bodies parsed with DateParseHandling.None");
}
#endif
}
Loading