mirror of
https://github.com/wiremock/WireMock.Net.git
synced 2026-04-23 09:18:55 +02:00
json
This commit is contained in:
@@ -236,7 +236,7 @@ public partial class WireMockServer
|
|||||||
|
|
||||||
if (FileHelper.TryReadMappingFileWithRetryAndDelay(_settings.FileSystemHandler, path, out var value))
|
if (FileHelper.TryReadMappingFileWithRetryAndDelay(_settings.FileSystemHandler, path, out var value))
|
||||||
{
|
{
|
||||||
var mappingModels = DeserializeJsonToArray<MappingModel>(value);
|
var mappingModels = _mappingSerializer.DeserializeJsonToArray<MappingModel>(value);
|
||||||
if (mappingModels.Length == 1 && Guid.TryParse(filenameWithoutExtension, out var guidFromFilename))
|
if (mappingModels.Length == 1 && Guid.TryParse(filenameWithoutExtension, out var guidFromFilename))
|
||||||
{
|
{
|
||||||
ConvertMappingAndRegisterAsRespondProvider(mappingModels[0], guidFromFilename, path);
|
ConvertMappingAndRegisterAsRespondProvider(mappingModels[0], guidFromFilename, path);
|
||||||
@@ -859,6 +859,18 @@ 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) where T : new()
|
private static T DeserializeObject<T>(IRequestMessage requestMessage) where T : new()
|
||||||
{
|
{
|
||||||
switch (requestMessage.BodyData?.DetectedBodyType)
|
switch (requestMessage.BodyData?.DetectedBodyType)
|
||||||
@@ -874,32 +886,4 @@ public partial class WireMockServer
|
|||||||
throw new NotSupportedException();
|
throw new NotSupportedException();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private static T[] DeserializeRequestMessageToArray<T>(IRequestMessage requestMessage)
|
|
||||||
{
|
|
||||||
if (requestMessage.BodyData?.DetectedBodyType == BodyType.Json && requestMessage.BodyData.BodyAsJson != null)
|
|
||||||
{
|
|
||||||
var bodyAsJson = requestMessage.BodyData.BodyAsJson;
|
|
||||||
|
|
||||||
return DeserializeObjectToArray<T>(bodyAsJson);
|
|
||||||
}
|
|
||||||
|
|
||||||
throw new NotSupportedException();
|
|
||||||
}
|
|
||||||
|
|
||||||
private static T[] DeserializeJsonToArray<T>(string value)
|
|
||||||
{
|
|
||||||
return DeserializeObjectToArray<T>(JsonUtils.DeserializeObject(value));
|
|
||||||
}
|
|
||||||
|
|
||||||
private static T[] DeserializeObjectToArray<T>(object value)
|
|
||||||
{
|
|
||||||
if (value is JArray jArray)
|
|
||||||
{
|
|
||||||
return jArray.ToObject<T[]>()!;
|
|
||||||
}
|
|
||||||
|
|
||||||
var singleResult = ((JObject)value).ToObject<T>();
|
|
||||||
return new[] { singleResult! };
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
@@ -31,7 +31,7 @@ public partial class WireMockServer
|
|||||||
|
|
||||||
if (FileHelper.TryReadMappingFileWithRetryAndDelay(_settings.FileSystemHandler, path, out var value))
|
if (FileHelper.TryReadMappingFileWithRetryAndDelay(_settings.FileSystemHandler, path, out var value))
|
||||||
{
|
{
|
||||||
var mappings = DeserializeJsonToArray<OrgMapping>(value);
|
var mappings = _mappingSerializer.DeserializeJsonToArray<OrgMapping>(value);
|
||||||
foreach (var mapping in mappings)
|
foreach (var mapping in mappings)
|
||||||
{
|
{
|
||||||
if (mappings.Length == 1 && Guid.TryParse(filenameWithoutExtension, out var guidFromFilename))
|
if (mappings.Length == 1 && Guid.TryParse(filenameWithoutExtension, out var guidFromFilename))
|
||||||
|
|||||||
@@ -8,9 +8,11 @@ using System.Collections.Generic;
|
|||||||
using System.Linq;
|
using System.Linq;
|
||||||
using System.Net;
|
using System.Net;
|
||||||
using System.Net.Http;
|
using System.Net.Http;
|
||||||
|
using System.Runtime;
|
||||||
using System.Threading;
|
using System.Threading;
|
||||||
using AnyOfTypes;
|
using AnyOfTypes;
|
||||||
using JetBrains.Annotations;
|
using JetBrains.Annotations;
|
||||||
|
using JsonConverter.Newtonsoft.Json;
|
||||||
using Newtonsoft.Json;
|
using Newtonsoft.Json;
|
||||||
using Stef.Validation;
|
using Stef.Validation;
|
||||||
using WireMock.Admin.Mappings;
|
using WireMock.Admin.Mappings;
|
||||||
@@ -47,6 +49,7 @@ public partial class WireMockServer : IWireMockServer
|
|||||||
private readonly MappingBuilder _mappingBuilder;
|
private readonly MappingBuilder _mappingBuilder;
|
||||||
private readonly IGuidUtils _guidUtils = new GuidUtils();
|
private readonly IGuidUtils _guidUtils = new GuidUtils();
|
||||||
private readonly IDateTimeUtils _dateTimeUtils = new DateTimeUtils();
|
private readonly IDateTimeUtils _dateTimeUtils = new DateTimeUtils();
|
||||||
|
private readonly MappingSerializer _mappingSerializer;
|
||||||
|
|
||||||
/// <inheritdoc />
|
/// <inheritdoc />
|
||||||
[PublicAPI]
|
[PublicAPI]
|
||||||
@@ -357,6 +360,8 @@ public partial class WireMockServer : IWireMockServer
|
|||||||
{
|
{
|
||||||
_settings = Guard.NotNull(settings);
|
_settings = Guard.NotNull(settings);
|
||||||
|
|
||||||
|
_mappingSerializer = new MappingSerializer(settings.MappingJsonSerializer ?? new NewtonsoftJsonConverter());
|
||||||
|
|
||||||
// Set default values if not provided
|
// Set default values if not provided
|
||||||
_settings.Logger = settings.Logger ?? new WireMockNullLogger();
|
_settings.Logger = settings.Logger ?? new WireMockNullLogger();
|
||||||
_settings.FileSystemHandler = settings.FileSystemHandler ?? new LocalFileSystemHandler();
|
_settings.FileSystemHandler = settings.FileSystemHandler ?? new LocalFileSystemHandler();
|
||||||
@@ -639,7 +644,7 @@ public partial class WireMockServer : IWireMockServer
|
|||||||
[PublicAPI]
|
[PublicAPI]
|
||||||
public IWireMockServer WithMapping(string mappings)
|
public IWireMockServer WithMapping(string mappings)
|
||||||
{
|
{
|
||||||
var mappingModels = DeserializeJsonToArray<MappingModel>(mappings);
|
var mappingModels = _mappingSerializer.DeserializeJsonToArray<MappingModel>(mappings);
|
||||||
foreach (var mappingModel in mappingModels)
|
foreach (var mappingModel in mappingModels)
|
||||||
{
|
{
|
||||||
ConvertMappingAndRegisterAsRespondProvider(mappingModel, mappingModel.Guid ?? Guid.NewGuid());
|
ConvertMappingAndRegisterAsRespondProvider(mappingModel, mappingModel.Guid ?? Guid.NewGuid());
|
||||||
|
|||||||
@@ -57,8 +57,8 @@
|
|||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageReference Include="JsonConverter.Abstractions" Version="0.7.0" />
|
<!--<PackageReference Include="JsonConverter.Abstractions" Version="0.7.0" />-->
|
||||||
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
|
<PackageReference Include="JsonConverter.Newtonsoft.Json" Version="0.7.0" />
|
||||||
<PackageReference Include="NJsonSchema.Extensions" Version="0.1.0" />
|
<PackageReference Include="NJsonSchema.Extensions" Version="0.1.0" />
|
||||||
<PackageReference Include="NSwag.Core" Version="13.16.1" />
|
<PackageReference Include="NSwag.Core" Version="13.16.1" />
|
||||||
<PackageReference Include="SimMetrics.Net" Version="1.0.5" />
|
<PackageReference Include="SimMetrics.Net" Version="1.0.5" />
|
||||||
|
|||||||
@@ -14,6 +14,7 @@ using WireMock.RegularExpressions;
|
|||||||
using WireMock.Types;
|
using WireMock.Types;
|
||||||
using System.Globalization;
|
using System.Globalization;
|
||||||
using WireMock.Models;
|
using WireMock.Models;
|
||||||
|
using JsonConverter.Abstractions;
|
||||||
|
|
||||||
#if USE_ASPNETCORE
|
#if USE_ASPNETCORE
|
||||||
using Microsoft.Extensions.DependencyInjection;
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
@@ -338,4 +339,14 @@ public class WireMockServerSettings
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
[PublicAPI]
|
[PublicAPI]
|
||||||
public HandlebarsSettings? HandlebarsSettings { get; set; }
|
public HandlebarsSettings? HandlebarsSettings { get; set; }
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Gets or sets the JSON converter used for MappingModel serialization.
|
||||||
|
/// </summary>
|
||||||
|
/// <remarks>
|
||||||
|
/// Set this property to customize how objects are serialized to and deserialized from JSON during mapping.
|
||||||
|
/// If not set, the NewtonsoftJsonConverter will be used.
|
||||||
|
/// </remarks>
|
||||||
|
[PublicAPI]
|
||||||
|
public IJsonConverter? MappingJsonSerializer { get; set; }
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user