mirror of
https://github.com/wiremock/WireMock.Net.git
synced 2026-06-06 15:02:47 +02:00
Fix JsonMatcher (parsing DateTimeOffset) (#358)
* . * JObject Parse * JsonUtils.Parse * fix code comments
This commit is contained in:
@@ -1,7 +1,8 @@
|
|||||||
using System.Linq;
|
using JetBrains.Annotations;
|
||||||
using JetBrains.Annotations;
|
|
||||||
using Newtonsoft.Json;
|
using Newtonsoft.Json;
|
||||||
using Newtonsoft.Json.Linq;
|
using Newtonsoft.Json.Linq;
|
||||||
|
using System.Linq;
|
||||||
|
using WireMock.Util;
|
||||||
using WireMock.Validation;
|
using WireMock.Validation;
|
||||||
|
|
||||||
namespace WireMock.Matchers
|
namespace WireMock.Matchers
|
||||||
@@ -93,7 +94,7 @@ namespace WireMock.Matchers
|
|||||||
break;
|
break;
|
||||||
|
|
||||||
case string stringValue:
|
case string stringValue:
|
||||||
jtokenValue = JToken.Parse(stringValue);
|
jtokenValue = JsonUtils.Parse(stringValue);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
default:
|
default:
|
||||||
|
|||||||
@@ -1,8 +1,9 @@
|
|||||||
using System;
|
using HandlebarsDotNet;
|
||||||
using System.Linq;
|
|
||||||
using HandlebarsDotNet;
|
|
||||||
using Newtonsoft.Json;
|
using Newtonsoft.Json;
|
||||||
using Newtonsoft.Json.Linq;
|
using Newtonsoft.Json.Linq;
|
||||||
|
using System;
|
||||||
|
using System.Linq;
|
||||||
|
using WireMock.Util;
|
||||||
using WireMock.Validation;
|
using WireMock.Validation;
|
||||||
|
|
||||||
namespace WireMock.Transformers
|
namespace WireMock.Transformers
|
||||||
@@ -56,7 +57,7 @@ namespace WireMock.Transformers
|
|||||||
switch (arguments[0])
|
switch (arguments[0])
|
||||||
{
|
{
|
||||||
case string jsonAsString:
|
case string jsonAsString:
|
||||||
valueToProcess = JObject.Parse(jsonAsString);
|
valueToProcess = JsonUtils.Parse(jsonAsString);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case JObject jsonAsJObject:
|
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.");
|
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]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,4 +1,5 @@
|
|||||||
using Newtonsoft.Json.Linq;
|
using Newtonsoft.Json;
|
||||||
|
using Newtonsoft.Json.Linq;
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
@@ -8,6 +9,22 @@ namespace WireMock.Util
|
|||||||
{
|
{
|
||||||
internal static class JsonUtils
|
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)
|
public static T ParseJTokenToObject<T>(object value)
|
||||||
{
|
{
|
||||||
switch (value)
|
switch (value)
|
||||||
|
|||||||
@@ -192,5 +192,22 @@ namespace WireMock.Net.Tests.Matchers
|
|||||||
// Assert
|
// Assert
|
||||||
Assert.Equal(0.0, match);
|
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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user