Fix JsonMatcher (parsing DateTimeOffset) (#358)

* .

* JObject Parse

* JsonUtils.Parse

* fix code comments
This commit is contained in:
Stef Heyenrath
2019-10-09 11:16:39 +02:00
committed by GitHub
parent b2167f85ae
commit 7789f94737
4 changed files with 45 additions and 9 deletions

View File

@@ -1,7 +1,8 @@
using System.Linq;
using JetBrains.Annotations;
using JetBrains.Annotations;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System.Linq;
using WireMock.Util;
using WireMock.Validation;
namespace WireMock.Matchers
@@ -93,7 +94,7 @@ namespace WireMock.Matchers
break;
case string stringValue:
jtokenValue = JToken.Parse(stringValue);
jtokenValue = JsonUtils.Parse(stringValue);
break;
default:

View File

@@ -1,8 +1,9 @@
using System;
using System.Linq;
using HandlebarsDotNet;
using HandlebarsDotNet;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Linq;
using WireMock.Util;
using WireMock.Validation;
namespace WireMock.Transformers
@@ -56,7 +57,7 @@ namespace WireMock.Transformers
switch (arguments[0])
{
case string jsonAsString:
valueToProcess = JObject.Parse(jsonAsString);
valueToProcess = JsonUtils.Parse(jsonAsString);
break;
case JObject jsonAsJObject:
@@ -67,7 +68,7 @@ namespace WireMock.Transformers
throw new NotSupportedException($"The value '{arguments[0]}' with type '{arguments[0]?.GetType()}' cannot be used in Handlebars JsonPath.");
}
return (valueToProcess, (string) arguments[1]);
return (valueToProcess, (string)arguments[1]);
}
}
}

View File

@@ -1,4 +1,5 @@
using Newtonsoft.Json.Linq;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
@@ -8,6 +9,22 @@ namespace WireMock.Util
{
internal static class JsonUtils
{
private static readonly JsonSerializerSettings JsonSerializerSettings = new JsonSerializerSettings
{
DateParseHandling = DateParseHandling.None
};
/// <summary>
/// Load a Newtonsoft.Json.Linq.JObject from a string that contains JSON.
/// Using : DateParseHandling = DateParseHandling.None
/// </summary>
/// <param name="json">A System.String that contains JSON.</param>
/// <returns>A Newtonsoft.Json.Linq.JObject populated from the string that contains JSON.</returns>
public static JObject Parse(string json)
{
return JsonConvert.DeserializeObject<JObject>(json, JsonSerializerSettings);
}
public static T ParseJTokenToObject<T>(object value)
{
switch (value)

View File

@@ -192,5 +192,22 @@ namespace WireMock.Net.Tests.Matchers
// Assert
Assert.Equal(0.0, match);
}
[Fact]
public void JsonMatcher_IsMatch_JObjectWithDateTimeOffsetAsString()
{
// Assign
var matcher = new JsonMatcher("{ \"preferredAt\" : \"2019-11-21T10:32:53.2210009+00:00\" }");
// Act
var jobject = new JObject
{
{ "preferredAt", new JValue("2019-11-21T10:32:53.2210009+00:00") }
};
double match = matcher.IsMatch(jobject);
// Assert
Assert.Equal(1.0, match);
}
}
}