Compare commits

..

2 Commits

Author SHA1 Message Date
Stef Heyenrath
9c51548d2b 1.5.32 2023-07-15 09:41:05 +02:00
Stef Heyenrath
98b8ede826 Fixed JsonPathMatcher to match nested objects (#966)
* Fixed JsonPathMatcher to match nested objects

* fix

* .

* 100%
2023-07-15 09:29:13 +02:00
6 changed files with 90 additions and 16 deletions

View File

@@ -1,3 +1,8 @@
# 1.5.32 (15 July 2023)
- [#966](https://github.com/WireMock-Net/WireMock.Net/pull/966) - Fixed JsonPathMatcher to match nested objects [bug] contributed by [StefH](https://github.com/StefH)
- [#965](https://github.com/WireMock-Net/WireMock.Net/issues/965) - JsonPathMatcher does not match json body nested objects [bug]
- [#967](https://github.com/WireMock-Net/WireMock.Net/issues/967) - ⭐10 million downloads ! ⭐ [feature]
# 1.5.31 (08 July 2023)
- [#964](https://github.com/WireMock-Net/WireMock.Net/pull/964) - Add GraphQL Schema matching [feature] contributed by [StefH](https://github.com/StefH)

View File

@@ -4,7 +4,7 @@
</PropertyGroup>
<PropertyGroup>
<VersionPrefix>1.5.31</VersionPrefix>
<VersionPrefix>1.5.32</VersionPrefix>
<PackageIcon>WireMock.Net-Logo.png</PackageIcon>
<PackageProjectUrl>https://github.com/WireMock-Net/WireMock.Net</PackageProjectUrl>
<PackageLicenseExpression>Apache-2.0</PackageLicenseExpression>

View File

@@ -1,6 +1,6 @@
rem https://github.com/StefH/GitHubReleaseNotes
SET version=1.5.31
SET version=1.5.32
GitHubReleaseNotes --output CHANGELOG.md --skip-empty-releases --exclude-labels question invalid doc duplicate --version %version% --token %GH_TOKEN%

View File

@@ -1,4 +1,6 @@
# 1.5.31 (08 July 2023)
- #964 Add GraphQL Schema matching [feature]
# 1.5.32 (15 July 2023)
- #966 Fixed JsonPathMatcher to match nested objects [bug]
- #965 JsonPathMatcher does not match json body nested objects [bug]
- #967 &#11088;10 million downloads ! &#11088; [feature]
The full release notes can be found here: https://github.com/WireMock-Net/WireMock.Net/blob/master/CHANGELOG.md

View File

@@ -121,6 +121,32 @@ public class JsonPathMatcher : IStringMatcher, IObjectMatcher
private double IsMatch(JToken jToken)
{
return MatchScores.ToScore(_patterns.Select(pattern => jToken.SelectToken(pattern.GetPattern()) != null).ToArray(), MatchOperator);
var array = ConvertJTokenToJArrayIfNeeded(jToken);
return MatchScores.ToScore(_patterns.Select(pattern => array.SelectToken(pattern.GetPattern())?.Any() == true).ToArray(), MatchOperator);
}
// https://github.com/WireMock-Net/WireMock.Net/issues/965
// https://stackoverflow.com/questions/66922188/newtonsoft-jsonpath-with-c-sharp-syntax
// Filtering using SelectToken() isn't guaranteed to work for objects inside objects -- only objects inside arrays.
// So this code checks if it's an JArray, if it's not an array, construct a new JArray.
private static JToken ConvertJTokenToJArrayIfNeeded(JToken jToken)
{
if (jToken.Count() == 1)
{
var property = jToken.First();
var item = property.First();
if (item is JArray)
{
return jToken;
}
return new JObject
{
[property.Path] = new JArray(item)
};
}
return jToken;
}
}

View File

@@ -11,7 +11,7 @@ public class JsonPathMatcherTests
[Fact]
public void JsonPathMatcher_GetName()
{
// Assign
// Arrange
var matcher = new JsonPathMatcher("X");
// Act
@@ -24,7 +24,7 @@ public class JsonPathMatcherTests
[Fact]
public void JsonPathMatcher_GetPatterns()
{
// Assign
// Arrange
var matcher = new JsonPathMatcher("X");
// Act
@@ -37,7 +37,7 @@ public class JsonPathMatcherTests
[Fact]
public void JsonPathMatcher_IsMatch_ByteArray()
{
// Assign
// Arrange
var bytes = EmptyArray<byte>.Value;
var matcher = new JsonPathMatcher("");
@@ -51,7 +51,7 @@ public class JsonPathMatcherTests
[Fact]
public void JsonPathMatcher_IsMatch_NullString()
{
// Assign
// Arrange
string? s = null;
var matcher = new JsonPathMatcher("");
@@ -65,7 +65,7 @@ public class JsonPathMatcherTests
[Fact]
public void JsonPathMatcher_IsMatch_NullObject()
{
// Assign
// Arrange
object? o = null;
var matcher = new JsonPathMatcher("");
@@ -79,7 +79,7 @@ public class JsonPathMatcherTests
[Fact]
public void JsonPathMatcher_IsMatch_String_Exception_Mismatch()
{
// Assign
// Arrange
var matcher = new JsonPathMatcher("xxx");
// Act
@@ -92,7 +92,7 @@ public class JsonPathMatcherTests
[Fact]
public void JsonPathMatcher_IsMatch_Object_Exception_Mismatch()
{
// Assign
// Arrange
var matcher = new JsonPathMatcher("");
// Act
@@ -105,7 +105,7 @@ public class JsonPathMatcherTests
[Fact]
public void JsonPathMatcher_IsMatch_AnonymousObject()
{
// Assign
// Arrange
var matcher = new JsonPathMatcher("$..[?(@.Id == 1)]");
// Act
@@ -115,10 +115,51 @@ public class JsonPathMatcherTests
Check.That(match).IsEqualTo(1);
}
[Fact]
public void JsonPathMatcher_IsMatch_AnonymousObject_WithNestedObject()
{
// Arrange
var matcher = new JsonPathMatcher("$.things[?(@.name == 'x')]");
// Act
double match = matcher.IsMatch(new { things = new { name = "x" } });
// Assert
Check.That(match).IsEqualTo(1);
}
[Fact]
public void JsonPathMatcher_IsMatch_String_WithNestedObject()
{
// Arrange
var json = "{ \"things\": { \"name\": \"x\" } }";
var matcher = new JsonPathMatcher("$.things[?(@.name == 'x')]");
// Act
double match = matcher.IsMatch(json);
// Assert
Check.That(match).IsEqualTo(1);
}
[Fact]
public void JsonPathMatcher_IsNoMatch_String_WithNestedObject()
{
// Arrange
var json = "{ \"things\": { \"name\": \"y\" } }";
var matcher = new JsonPathMatcher("$.things[?(@.name == 'x')]");
// Act
double match = matcher.IsMatch(json);
// Assert
Check.That(match).IsEqualTo(0);
}
[Fact]
public void JsonPathMatcher_IsMatch_JObject()
{
// Assign
// Arrange
string[] patterns = { "$..[?(@.Id == 1)]" };
var matcher = new JsonPathMatcher(patterns);
@@ -137,7 +178,7 @@ public class JsonPathMatcherTests
[Fact]
public void JsonPathMatcher_IsMatch_JObject_Parsed()
{
// Assign
// Arrange
var matcher = new JsonPathMatcher("$..[?(@.Id == 1)]");
// Act
@@ -150,7 +191,7 @@ public class JsonPathMatcherTests
[Fact]
public void JsonPathMatcher_IsMatch_RejectOnMatch()
{
// Assign
// Arrange
var matcher = new JsonPathMatcher(MatchBehaviour.RejectOnMatch, false, MatchOperator.Or, "$..[?(@.Id == 1)]");
// Act