[exim-cvs] Fix json extract operator for unfound case

Inizio della pagina
Delete this message
Reply to this message
Autore: Exim Git Commits Mailing List
Data:  
To: exim-cvs
Oggetto: [exim-cvs] Fix json extract operator for unfound case
Gitweb: https://git.exim.org/exim.git/commitdiff/e73798976812e652320f096870359ef35ed069ff
Commit:     e73798976812e652320f096870359ef35ed069ff
Parent:     ffc92d69bf2618aca35f2c523abde0a76657e3a2
Author:     Jeremy Harris <jgh146exb@???>
AuthorDate: Sat Feb 9 16:56:59 2019 +0000
Committer:  Jeremy Harris <jgh146exb@???>
CommitDate: Mon Feb 11 00:15:46 2019 +0000


    Fix json extract operator for unfound case
---
 doc/doc-docbook/spec.xfpt    |  7 ++++++-
 src/src/expand.c             | 13 ++++++++-----
 test/scripts/0000-Basic/0002 |  3 +++
 test/stdout/0002             |  3 +++
 4 files changed, 20 insertions(+), 6 deletions(-)


diff --git a/doc/doc-docbook/spec.xfpt b/doc/doc-docbook/spec.xfpt
index 46cab7c..c40cf23 100644
--- a/doc/doc-docbook/spec.xfpt
+++ b/doc/doc-docbook/spec.xfpt
@@ -6755,9 +6755,10 @@ of the JSON structure.
If a selector is numeric, it must apply to a JSON array; the (zero-based)
nunbered array element is selected.
Otherwise it must apply to a JSON object; the named element is selected.
-The final resulting object can be a simple JSOM type or a JSON object
+The final resulting element can be a simple JSON type or a JSON object
or array; for the latter two a string-representation os the JSON
is returned.
+For elements of type string, the returned value is de-quoted.
.wen
.next
.cindex "linear search"
@@ -9550,6 +9551,8 @@ The expanded <&'string1'&> must be of the form:
The braces, commas and colons, and the quoting of the member name are required;
the spaces are optional.
Matching of the key against the member names is done case-sensitively.
+If a returned value is a JSON string, it retains its leading and
+trailing quotes.
. XXX should be a UTF-8 compare

The results of matching are handled as above.
@@ -9595,6 +9598,8 @@ apart from leading and trailing white space, which is ignored.

Field selection and result handling is as above;
there is no choice of field separator.
+If a returned value is a JSON string, it retains its leading and
+trailing quotes.


.vitem &*${filter{*&<&'string'&>&*}{*&<&'condition'&>&*}}*&
diff --git a/src/src/expand.c b/src/src/expand.c
index 4dc13ff..d36d376 100644
--- a/src/src/expand.c
+++ b/src/src/expand.c
@@ -3893,7 +3893,8 @@ return NULL;
/* Pull off the leading array or object element, returning
a copy in an allocated string. Update the list pointer.

-The element may itself be an abject or array.
+The element may itself be an object or array.
+Return NULL when the list is empty.
*/

 uschar *
@@ -3915,6 +3916,7 @@ for (item = s;
     case '}': object_depth--; break;
     }
 *list = *s ? s+1 : s;
+if (item == s) return NULL;
 item = string_copyn(item, s - item);
 DEBUG(D_expand) debug_printf_indent("  json ele: '%s'\n", item);
 return US item;
@@ -5776,10 +5778,11 @@ while (*s != 0)
           }
         while (field_number > 0 && (item = json_nextinlist(&list)))
           field_number--;
-        s = item;
-        lookup_value = s;
-        while (*s) s++;
-        while (--s >= lookup_value && isspace(*s)) *s = '\0';
+        if ((lookup_value = s = item))
+          {
+          while (*s) s++;
+          while (--s >= lookup_value && isspace(*s)) *s = '\0';
+          }
         }
       else
         {
diff --git a/test/scripts/0000-Basic/0002 b/test/scripts/0000-Basic/0002
index e192a55..7a9b38d 100644
--- a/test/scripts/0000-Basic/0002
+++ b/test/scripts/0000-Basic/0002
@@ -902,6 +902,9 @@ expect: {"1":116, "2":943, "3":234}
 ${extract json{IDs}{ \{"id": \{"a":101, "b":102\}, "IDs": \{"1":116, "2":943, "3":234\}\} }}
 expect: {"1":116, "2":943, "3":234}


+<${extract json{nonexistent}{ \{"id": \{"a":101, "b":102\}, "IDs": \{"1":116, "2":943, "3":234\}\} }}>
+expect: <>
+
****
# Test "escape" with print_topbitchars
exim -be -DPTBC=print_topbitchars
diff --git a/test/stdout/0002 b/test/stdout/0002
index 949c35e..c2f5f2f 100644
--- a/test/stdout/0002
+++ b/test/stdout/0002
@@ -845,6 +845,9 @@ xyz
> {"1":116, "2":943, "3":234}
> expect: {"1":116, "2":943, "3":234}
>

+> <>
+> expect: <>
+>
>
> escape: B7?F2?
>