[Cps Path Parser] Don't quote numbers in comparison 27/139527/2
authordanielhanrahan <daniel.hanrahan@est.tech>
Thu, 21 Nov 2024 18:05:05 +0000 (18:05 +0000)
committerdanielhanrahan <daniel.hanrahan@est.tech>
Mon, 25 Nov 2024 18:00:28 +0000 (18:00 +0000)
Presently, Cps Path Parser will wrap numbers in quotes when
normalizing or getting parent path, which can lead to invalid paths.
For example:
  /books[@price > 15]
will be normalized to:
  /books[@price>'15']

This causes trouble when running queries on normalized paths:
(CpsPathException: "can use only > with integer")

Note this patch will still normalize numbers to string for equality:
  /books[@price='15']
Much existing code relies on this, and it does not cause problems.

Issue-ID: CPS-2365
Signed-off-by: danielhanrahan <daniel.hanrahan@est.tech>
Change-Id: I15d326ad7db2311b64c636283a4d52623a125176

cps-path-parser/src/main/java/org/onap/cps/cpspath/parser/CpsPathBuilder.java
cps-path-parser/src/test/groovy/org/onap/cps/cpspath/parser/CpsPathQuerySpec.groovy

index ed7dbec..b67d708 100644 (file)
@@ -180,12 +180,12 @@ public class CpsPathBuilder extends CpsPathBaseListener {
         if (!isStartOfExpression) {
             currentNormalizedPathBuilder.append(" ").append(getLastElement(booleanOperators)).append(" ");
         }
-        currentNormalizedPathBuilder.append("@")
-                                    .append(name)
-                                    .append(operator)
-                                    .append("'")
-                                    .append(value.toString().replace("'", "''"))
-                                    .append("'");
+        currentNormalizedPathBuilder.append("@").append(name).append(operator);
+        if (operator.equals("=")) {
+            currentNormalizedPathBuilder.append(wrapValueInSingleQuotes(value));
+        } else {
+            currentNormalizedPathBuilder.append(value);
+        }
     }
 
     private static String getLastElement(final List<String> listOfStrings) {
@@ -202,6 +202,10 @@ public class CpsPathBuilder extends CpsPathBaseListener {
         }
     }
 
+    private static String wrapValueInSingleQuotes(final Object value) {
+        return "'" + value.toString().replace("'", "''") + "'";
+    }
+
     private static String stripFirstAndLastCharacter(final String wrappedString) {
         return wrappedString.substring(1, wrappedString.length() - 1);
     }
index 16430d2..a1bf289 100644 (file)
@@ -87,10 +87,10 @@ class CpsPathQuerySpec extends Specification {
             'yang container'                                              | '/cps-path'                                                    || '/cps-path'
             'descendant anywhere'                                         | '//cps-path'                                                   || '//cps-path'
             'descendant with leaf condition'                              | '//cps-path[@key=1]'                                           || "//cps-path[@key='1']"
-            'descendant with leaf condition has ">" operator'             | '//cps-path[@key>9]'                                           || "//cps-path[@key>'9']"
-            'descendant with leaf condition has "<" operator'             | '//cps-path[@key<10]'                                          || "//cps-path[@key<'10']"
-            'descendant with leaf condition has ">=" operator'            | '//cps-path[@key>=8]'                                          || "//cps-path[@key>='8']"
-            'descendant with leaf condition has "<=" operator'            | '//cps-path[@key<=12]'                                         || "//cps-path[@key<='12']"
+            'descendant with leaf condition has ">" operator'             | '//cps-path[@key>9]'                                           || "//cps-path[@key>9]"
+            'descendant with leaf condition has "<" operator'             | '//cps-path[@key<10]'                                          || "//cps-path[@key<10]"
+            'descendant with leaf condition has ">=" operator'            | '//cps-path[@key>=8]'                                          || "//cps-path[@key>=8]"
+            'descendant with leaf condition has "<=" operator'            | '//cps-path[@key<=12]'                                         || "//cps-path[@key<=12]"
             'descendant with leaf value and ancestor'                     | '//cps-path[@key=1]/ancestor::parent[@key=1]'                  || "//cps-path[@key='1']/ancestor::parent[@key='1']"
             'parent & child'                                              | '/parent/child'                                                || '/parent/child'
             'parent leaf of type Integer & child'                         | '/parent/child[@code=1]/child2'                                || "/parent/child[@code='1']/child2"