]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
MINOR: sample: Added support for Arrays in sample_conv_json_query in sample.c
authorJens Popp <jens.popp@camline.com>
Mon, 25 Sep 2023 13:30:53 +0000 (13:30 +0000)
committerWilliam Lallemand <wlallemand@haproxy.com>
Fri, 20 Oct 2023 16:42:05 +0000 (18:42 +0200)
Method now returns the content of Json Arrays, if it is specified in
Json Path as String. The start and end character is a square bracket. Any
complex object in the array is returned as Json, so that you might get Arrays
of Array or objects. Only recommended for Arrays of simple types (e.g.,
String or int) which will be returned as CSV String. Also updated
documentation and fixed issue with parenthesis and other changes from
comments.

This patch was discussed in issue #2281.

Signed-off-by: William Lallemand <wlallemand@haproxy.com>
doc/configuration.txt
reg-tests/converter/json_query.vtc
src/sample.c

index 50fe882d078752c64fc3c96aef3757de8f6ff2e6..47c9c57597b3a7be5bcd145fa705c86fc38d6e40 100644 (file)
@@ -18423,10 +18423,18 @@ json([<input-code>])
      {"ip":"127.0.0.1","user-agent":"Very \"Ugly\" UA 1\/2"}
 
 json_query(<json_path>,[<output_type>])
-  The json_query converter supports the JSON types string, boolean and
-  number. Floating point numbers will be returned as a string. By
+  The json_query converter supports the JSON types string, boolean, number
+  and array. Floating point numbers will be returned as a string. By
   specifying the output_type 'int' the value will be converted to an
-  Integer. If conversion is not possible the json_query converter fails.
+  Integer. Arrays will be returned as string, starting and ending with a
+  square brackets. The content is a CSV. Depending on the data type, the
+  array values might be quoted. If the array values are complex types,
+  the string contains the complete json representation of each value
+  separated by a comma. Example result for a roles query to a JWT:
+
+     ["manage-account","manage-account-links","view-profile"]
+
+  If conversion is not possible the json_query converter fails.
 
   <json_path> must be a valid JSON Path string as defined in
   https://datatracker.ietf.org/doc/draft-ietf-jsonpath-base/
index f78c393f0741bdafee9da56e7afe4cf55987615b..f4e3bb203c0eeb9de11660ae4687c5fe5b97780c 100644 (file)
@@ -98,4 +98,10 @@ client c1 -connect ${h1_fe_sock} {
        rxresp
        expect resp.status == 200
        expect resp.http.x-var_body_mykey ~ "myvalue"
-} -run
\ No newline at end of file
+
+       txreq -url "/" \
+        -body "{\"my.key\":[\"val1\",\"val2\",\"val3\"],\"key2\":\"val4\"}"
+       expect resp.status == 200
+       expect resp.http.x-var_body_mykey ~ "[\"val1\",\"val2\",\"val3\"]"
+
+} -run
index f1705ea47d8bdc25bb5dae01d8ddacb966d35a8a..3d9958645b4f466a9adacf83fb2b279be38fcc22 100644 (file)
@@ -4212,8 +4212,15 @@ static int sample_conv_json_query(const struct arg *args, struct sample *smp, vo
 
                        return 1;
                }
+               case MJSON_TOK_ARRAY: {
+                       // We copy the complete array, including square brackets into the return buffer
+                       // result looks like: ["manage-account","manage-account-links","view-profile"]
+                       trash->data = b_putblk(trash, token, token_size);
+                       smp->data.u.str = *trash;
+                       smp->data.type = SMP_T_STR;
+                       return 1;
+               }
                case MJSON_TOK_NULL:
-               case MJSON_TOK_ARRAY:
                case MJSON_TOK_OBJECT:
                        /* We cannot handle these. */
                        return 0;