Skip to content

Commit

Permalink
Fix JsonMatcher if IgnoreCase and Regex are used
Browse files Browse the repository at this point in the history
  • Loading branch information
StefH committed Jun 8, 2024
1 parent 4538f6c commit 86f8877
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 23 deletions.
65 changes: 42 additions & 23 deletions src/WireMock.Net/Matchers/JsonMatcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ public class JsonMatcher : IJsonMatcher
public bool Regex { get; }

private readonly JToken _valueAsJToken;
private readonly Func<JToken, JToken> _jTokenConverter;

/// <summary>
/// Initializes a new instance of the <see cref="JsonMatcher"/> class.
Expand Down Expand Up @@ -70,7 +69,6 @@ public JsonMatcher(MatchBehaviour matchBehaviour, object value, bool ignoreCase

Value = value;
_valueAsJToken = JsonUtils.ConvertValueToJToken(value);
_jTokenConverter = ignoreCase ? Rename : jToken => jToken;
}

/// <inheritdoc />
Expand All @@ -86,7 +84,7 @@ public MatchResult IsMatch(object? input)
{
var inputAsJToken = JsonUtils.ConvertValueToJToken(input);

var match = IsMatch(_jTokenConverter(_valueAsJToken), _jTokenConverter(inputAsJToken));
var match = IsMatch(RenameJToken(_valueAsJToken), RenameJToken(inputAsJToken));
score = MatchScores.ToScore(match);
}
catch (Exception ex)
Expand Down Expand Up @@ -179,38 +177,59 @@ protected virtual bool IsMatch(JToken value, JToken? input)
}
}

private static string? ToUpper(string? input)
// https://stackoverflow.com/questions/11679804/json-net-rename-properties
private JToken RenameJToken(JToken input)
{
return input?.ToUpperInvariant();
if (!IgnoreCase)
{
return input;
}

return input switch
{
JProperty property => RenameJProperty(property),
JArray array => RenameJArray(array),
JObject obj => RenameJObject(obj),
_ => input
};
}

// https://stackoverflow.com/questions/11679804/json-net-rename-properties
private static JToken Rename(JToken json)
private JProperty RenameJProperty(JProperty property)
{
if (json is JProperty property)
if (!IgnoreCase)
{
JToken propertyValue = property.Value;
if (propertyValue.Type == JTokenType.String)
{
string stringValue = propertyValue.Value<string>()!;
propertyValue = ToUpper(stringValue);
}

return new JProperty(ToUpper(property.Name)!, Rename(propertyValue));
return property;
}

if (json is JArray array)
var propertyValue = property.Value;
if (propertyValue.Type == JTokenType.String && !Regex)
{
var renamedValues = array.Select(Rename);
return new JArray(renamedValues);
var stringValue = propertyValue.Value<string>()!;
propertyValue = ToUpper(stringValue);
}

if (json is JObject obj)
return new JProperty(ToUpper(property.Name)!, RenameJToken(propertyValue));
}

private JArray RenameJArray(JArray array)
{
if (Regex)
{
var renamedProperties = obj.Properties().Select(Rename);
return new JObject(renamedProperties);
return array;
}

return json;
var renamedValues = array.Select(RenameJToken);
return new JArray(renamedValues);
}

private JObject RenameJObject(JObject obj)
{
var renamedProperties = obj.Properties().Select(RenameJProperty);
return new JObject(renamedProperties);
}

private static string? ToUpper(string? input)
{
return input?.ToUpperInvariant();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -385,4 +385,22 @@ public void JsonPartialWildcardMatcher_IsMatch_ValueAsJPathInvalidMatch(string v
// Assert
Assert.Equal(0.0, match);
}

[Fact]
public void JsonPartialWildcardMatcher_IsMatch_WithIgnoreCaseTrueAndRegexTrue_JObject()
{
// Assign
var matcher = new JsonPartialWildcardMatcher(new { id = 1, Number = "^\\d+$" }, ignoreCase: true, regex: true);

// Act
var jObject = new JObject
{
{ "Id", new JValue(1) },
{ "Number", new JValue(1) }
};
double match = matcher.IsMatch(jObject).Score;

// Assert
Assert.Equal(1.0, match);
}
}

0 comments on commit 86f8877

Please sign in to comment.