Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix JsonMatcher (parsing DateTimeOffset) #358

Merged
merged 4 commits into from
Oct 9, 2019
Merged
Show file tree
Hide file tree
Changes from 3 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
12 changes: 9 additions & 3 deletions src/WireMock.Net/Matchers/JsonMatcher.cs
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -11,6 +12,11 @@ namespace WireMock.Matchers
/// </summary>
public class JsonMatcher : IValueMatcher, IIgnoreCaseMatcher
{
private readonly JsonSerializerSettings _jsonSerializerSettings = new JsonSerializerSettings()
{
DateParseHandling = DateParseHandling.None
};

/// <inheritdoc cref="IValueMatcher.Value"/>
public object Value { get; }

Expand Down Expand Up @@ -93,7 +99,7 @@ public double IsMatch(object input)
break;

case string stringValue:
jtokenValue = JToken.Parse(stringValue);
jtokenValue = JsonUtils.Parse(stringValue, _jsonSerializerSettings);
break;

default:
Expand Down
16 changes: 11 additions & 5 deletions src/WireMock.Net/Transformers/HandleBarsJsonPath.cs
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
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
{
internal static class HandleBarsJsonPath
{
private static readonly JsonSerializerSettings JsonSerializerSettings = new JsonSerializerSettings()
{
DateParseHandling = DateParseHandling.None
};
StefH marked this conversation as resolved.
Show resolved Hide resolved

public static void Register(IHandlebars handlebarsContext)
{
handlebarsContext.RegisterHelper("JsonPath.SelectToken", (writer, context, arguments) =>
Expand Down Expand Up @@ -56,7 +62,7 @@ private static (JObject valueToProcess, string jsonpath) ParseArguments(object[]
switch (arguments[0])
{
case string jsonAsString:
valueToProcess = JObject.Parse(jsonAsString);
valueToProcess = JsonUtils.Parse(jsonAsString, JsonSerializerSettings);
break;

case JObject jsonAsJObject:
Expand All @@ -67,7 +73,7 @@ private static (JObject valueToProcess, string jsonpath) ParseArguments(object[]
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]);
}
}
}
14 changes: 13 additions & 1 deletion src/WireMock.Net/Util/JsonUtils.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Newtonsoft.Json.Linq;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
Expand All @@ -8,6 +9,17 @@ namespace WireMock.Util
{
internal static class JsonUtils
{
/// <summary>
/// Load a Newtonsoft.Json.Linq.JObject from a string that contains JSON.
/// </summary>
/// <param name="json">A System.String that contains JSON.</param>
/// <param name="settings">The JsonSerializerSettings.</param>
/// <returns>A Newtonsoft.Json.Linq.JObject populated from the string that contains JSON.</returns>
public static JObject Parse(string json, JsonSerializerSettings settings)
{
return JsonConvert.DeserializeObject<JObject>(json, settings);
}

public static T ParseJTokenToObject<T>(object value)
{
switch (value)
Expand Down
17 changes: 17 additions & 0 deletions test/WireMock.Net.Tests/Matchers/JsonMatcherTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -192,5 +192,22 @@ public void JsonMatcher_IsMatch_JObjectAsString_RejectOnMatch()
// 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);
}
}
}