diff --git a/src/WireMock.Net.Minimal/Server/WireMockServer.Admin.cs b/src/WireMock.Net.Minimal/Server/WireMockServer.Admin.cs index 5c248a26..96ce9ad1 100644 --- a/src/WireMock.Net.Minimal/Server/WireMockServer.Admin.cs +++ b/src/WireMock.Net.Minimal/Server/WireMockServer.Admin.cs @@ -236,7 +236,7 @@ public partial class WireMockServer if (FileHelper.TryReadMappingFileWithRetryAndDelay(_settings.FileSystemHandler, path, out var value)) { - var mappingModels = DeserializeJsonToArray(value); + var mappingModels = _mappingSerializer.DeserializeJsonToArray(value); if (mappingModels.Length == 1 && Guid.TryParse(filenameWithoutExtension, out var guidFromFilename)) { ConvertMappingAndRegisterAsRespondProvider(mappingModels[0], guidFromFilename, path); @@ -859,6 +859,18 @@ public partial class WireMockServer }; } + private static T[] DeserializeRequestMessageToArray(IRequestMessage requestMessage) + { + if (requestMessage.BodyData?.DetectedBodyType == BodyType.Json && requestMessage.BodyData.BodyAsJson != null) + { + var bodyAsJson = requestMessage.BodyData.BodyAsJson!; + + return MappingSerializer.DeserializeObjectToArray(bodyAsJson); + } + + throw new NotSupportedException(); + } + private static T DeserializeObject(IRequestMessage requestMessage) where T : new() { switch (requestMessage.BodyData?.DetectedBodyType) @@ -874,32 +886,4 @@ public partial class WireMockServer throw new NotSupportedException(); } } - - private static T[] DeserializeRequestMessageToArray(IRequestMessage requestMessage) - { - if (requestMessage.BodyData?.DetectedBodyType == BodyType.Json && requestMessage.BodyData.BodyAsJson != null) - { - var bodyAsJson = requestMessage.BodyData.BodyAsJson; - - return DeserializeObjectToArray(bodyAsJson); - } - - throw new NotSupportedException(); - } - - private static T[] DeserializeJsonToArray(string value) - { - return DeserializeObjectToArray(JsonUtils.DeserializeObject(value)); - } - - private static T[] DeserializeObjectToArray(object value) - { - if (value is JArray jArray) - { - return jArray.ToObject()!; - } - - var singleResult = ((JObject)value).ToObject(); - return new[] { singleResult! }; - } } \ No newline at end of file diff --git a/src/WireMock.Net.Minimal/Server/WireMockServer.ImportWireMockOrg.cs b/src/WireMock.Net.Minimal/Server/WireMockServer.ImportWireMockOrg.cs index 1cc29b61..10c6751d 100644 --- a/src/WireMock.Net.Minimal/Server/WireMockServer.ImportWireMockOrg.cs +++ b/src/WireMock.Net.Minimal/Server/WireMockServer.ImportWireMockOrg.cs @@ -31,7 +31,7 @@ public partial class WireMockServer if (FileHelper.TryReadMappingFileWithRetryAndDelay(_settings.FileSystemHandler, path, out var value)) { - var mappings = DeserializeJsonToArray(value); + var mappings = _mappingSerializer.DeserializeJsonToArray(value); foreach (var mapping in mappings) { if (mappings.Length == 1 && Guid.TryParse(filenameWithoutExtension, out var guidFromFilename)) diff --git a/src/WireMock.Net.Minimal/Server/WireMockServer.cs b/src/WireMock.Net.Minimal/Server/WireMockServer.cs index b7d46738..71a8efb7 100644 --- a/src/WireMock.Net.Minimal/Server/WireMockServer.cs +++ b/src/WireMock.Net.Minimal/Server/WireMockServer.cs @@ -8,9 +8,11 @@ using System.Collections.Generic; using System.Linq; using System.Net; using System.Net.Http; +using System.Runtime; using System.Threading; using AnyOfTypes; using JetBrains.Annotations; +using JsonConverter.Newtonsoft.Json; using Newtonsoft.Json; using Stef.Validation; using WireMock.Admin.Mappings; @@ -47,6 +49,7 @@ public partial class WireMockServer : IWireMockServer private readonly MappingBuilder _mappingBuilder; private readonly IGuidUtils _guidUtils = new GuidUtils(); private readonly IDateTimeUtils _dateTimeUtils = new DateTimeUtils(); + private readonly MappingSerializer _mappingSerializer; /// [PublicAPI] @@ -357,6 +360,8 @@ public partial class WireMockServer : IWireMockServer { _settings = Guard.NotNull(settings); + _mappingSerializer = new MappingSerializer(settings.MappingJsonSerializer ?? new NewtonsoftJsonConverter()); + // Set default values if not provided _settings.Logger = settings.Logger ?? new WireMockNullLogger(); _settings.FileSystemHandler = settings.FileSystemHandler ?? new LocalFileSystemHandler(); @@ -639,7 +644,7 @@ public partial class WireMockServer : IWireMockServer [PublicAPI] public IWireMockServer WithMapping(string mappings) { - var mappingModels = DeserializeJsonToArray(mappings); + var mappingModels = _mappingSerializer.DeserializeJsonToArray(mappings); foreach (var mappingModel in mappingModels) { ConvertMappingAndRegisterAsRespondProvider(mappingModel, mappingModel.Guid ?? Guid.NewGuid()); diff --git a/src/WireMock.Net.Minimal/WireMock.Net.Minimal.csproj b/src/WireMock.Net.Minimal/WireMock.Net.Minimal.csproj index 0708611b..57b60323 100644 --- a/src/WireMock.Net.Minimal/WireMock.Net.Minimal.csproj +++ b/src/WireMock.Net.Minimal/WireMock.Net.Minimal.csproj @@ -57,8 +57,8 @@ - - + + diff --git a/src/WireMock.Net.Shared/Settings/WireMockServerSettings.cs b/src/WireMock.Net.Shared/Settings/WireMockServerSettings.cs index 86f99f38..bf1cbd8a 100644 --- a/src/WireMock.Net.Shared/Settings/WireMockServerSettings.cs +++ b/src/WireMock.Net.Shared/Settings/WireMockServerSettings.cs @@ -14,6 +14,7 @@ using WireMock.RegularExpressions; using WireMock.Types; using System.Globalization; using WireMock.Models; +using JsonConverter.Abstractions; #if USE_ASPNETCORE using Microsoft.Extensions.DependencyInjection; @@ -338,4 +339,14 @@ public class WireMockServerSettings /// [PublicAPI] public HandlebarsSettings? HandlebarsSettings { get; set; } + + /// + /// Gets or sets the JSON converter used for MappingModel serialization. + /// + /// + /// Set this property to customize how objects are serialized to and deserialized from JSON during mapping. + /// If not set, the NewtonsoftJsonConverter will be used. + /// + [PublicAPI] + public IJsonConverter? MappingJsonSerializer { get; set; } } \ No newline at end of file