This is an automated email from the ASF dual-hosted git repository.

fschumacher pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/jmeter.git


The following commit(s) were added to refs/heads/master by this push:
     new 278255d  Fail JSONPath Assertion on indefinite paths and no assertion 
value
278255d is described below

commit 278255de85829c850d36ad1655d1541e66d5368f
Author: Felix Schumacher <[email protected]>
AuthorDate: Sat Jan 15 18:11:36 2022 +0100

    Fail JSONPath Assertion on indefinite paths and no assertion value
    
    The docs for JSONPath Assertion state, that it will fail the assertion,
    when no element is found with the given JSON path. This was currently
    not followed, when an indefinite path was used. In such a case, the
    JSONPath library would return an empty list, which the assertion logic
    would log as OK.
    
    With this change we let the assertion fail, when
     * an indefinite path was given
     * and an empty list is extracted
     * and no assertion value is given
    
    Bugzilla Id: 65794
---
 .../java/org/apache/jmeter/assertions/JSONPathAssertion.java   | 10 ++++++++--
 .../org/apache/jmeter/assertions/TestJSONPathAssertion.java    |  6 +++++-
 xdocs/changes.xml                                              |  2 +-
 xdocs/usermanual/component_reference.xml                       |  4 +++-
 4 files changed, 17 insertions(+), 5 deletions(-)

diff --git 
a/src/components/src/main/java/org/apache/jmeter/assertions/JSONPathAssertion.java
 
b/src/components/src/main/java/org/apache/jmeter/assertions/JSONPathAssertion.java
index 2e135b4..c2986df 100644
--- 
a/src/components/src/main/java/org/apache/jmeter/assertions/JSONPathAssertion.java
+++ 
b/src/components/src/main/java/org/apache/jmeter/assertions/JSONPathAssertion.java
@@ -30,12 +30,12 @@ import org.apache.oro.text.regex.Pattern;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
+import com.jayway.jsonpath.JsonPath;
+
 import net.minidev.json.JSONArray;
 import net.minidev.json.JSONObject;
 import net.minidev.json.JSONValue;
 
-import com.jayway.jsonpath.JsonPath;
-
 /**
  * This is main class for JSONPath Assertion which verifies assertion on
  * previous sample result using JSON path expression
@@ -112,6 +112,12 @@ public class JSONPathAssertion extends AbstractTestElement 
implements Serializab
         Object value = JsonPath.read(jsonString, getJsonPath());
 
         if (!isJsonValidationBool()) {
+            if (value instanceof JSONArray) {
+                JSONArray arrayValue = (JSONArray) value;
+                if (arrayValue.isEmpty() && 
!JsonPath.isPathDefinite(getJsonPath())) {
+                    throw new IllegalStateException("JSONPath is indefinite 
and the extracted Value is an empty Array. Please use an assertion value, to be 
sure to get a correct result. " + getExpectedValue());
+                }
+            }
             return;
         }
 
diff --git 
a/src/components/src/test/java/org/apache/jmeter/assertions/TestJSONPathAssertion.java
 
b/src/components/src/test/java/org/apache/jmeter/assertions/TestJSONPathAssertion.java
index f4245f2..a6738c3 100644
--- 
a/src/components/src/test/java/org/apache/jmeter/assertions/TestJSONPathAssertion.java
+++ 
b/src/components/src/test/java/org/apache/jmeter/assertions/TestJSONPathAssertion.java
@@ -22,6 +22,7 @@ import static org.junit.jupiter.api.Assertions.assertEquals;
 import static org.junit.jupiter.api.Assertions.assertFalse;
 import static org.junit.jupiter.api.Assertions.assertTrue;
 
+import java.nio.charset.Charset;
 import java.util.Locale;
 
 import org.apache.jmeter.samplers.SampleResult;
@@ -270,6 +271,9 @@ class TestJSONPathAssertion {
 
     @Test
     void testGetResult_list_empty_novalidate() {
+        // With bug 65794 the outcome of this test has changed
+        // we now consider an indefinite path with no assertion value
+        // an error and set the AssertionResult to failure
         SampleResult samplerResult = new SampleResult();
         samplerResult.setResponseData("{\"myval\": []}".getBytes());
 
@@ -279,7 +283,7 @@ class TestJSONPathAssertion {
         AssertionResult expResult = new AssertionResult("");
         AssertionResult result = instance.getResult(samplerResult);
         assertEquals(expResult.getName(), result.getName());
-        assertFalse(result.isFailure());
+        assertTrue(result.isFailure());
     }
 
     @Test
diff --git a/xdocs/changes.xml b/xdocs/changes.xml
index 6f56b60..89c936b 100644
--- a/xdocs/changes.xml
+++ b/xdocs/changes.xml
@@ -237,6 +237,7 @@ however, the profile can't be updated while the test is 
running.
   <li><bug>65681</bug>Use default values for null values when extracting with 
JSONPostProcessor</li>
   <li>Allow setters in ConstantThroughputTimer to updating the values during 
the run time</li>
   <li><bug>65782</bug>Use correct message format for MessageFormat in 
HTMLAssertion</li>
+  <li><bug>65794</bug>JSON Assertion always successful with indefinite 
paths</li>
 </ul>
 
 <h3>Functions</h3>
@@ -253,7 +254,6 @@ however, the profile can't be updated while the test is 
running.
 
 <h3>Documentation</h3>
 <ul>
-  <li><bug>65794</bug>JSON Assertion always successful with indefinite 
paths</li>
 </ul>
 
 <h3>General</h3>
diff --git a/xdocs/usermanual/component_reference.xml 
b/xdocs/usermanual/component_reference.xml
index 985918c..d1fdfb8 100644
--- a/xdocs/usermanual/component_reference.xml
+++ b/xdocs/usermanual/component_reference.xml
@@ -5037,7 +5037,9 @@ please ensure that you select "<code>Store the message 
using MIME (raw)</code>"
             Note that if the path will return array object, it will be 
iterated and if expected value is found, the assertion will succeed. To 
validate empty array use <code>[]</code> string. Also, if patch will return 
dictionary object, it will be converted to string before comparison.
            <note>When using <a 
href="https://github.com/json-path/JsonPath#what-is-returned-when";>indefinite 
JSON Paths</a>
              you must assert the value due to the existing JSON library 
implementation, otherwise the assertion could always
-             return successful</note>
+             return successful.<br>
+             Since JMeter version 5.5 the assertion will fail, if an 
indefinite path is given, an empty list is extracted and
+             no assertion value is set.</note>
         </p>
     </description>
     <properties>

Reply via email to