Re: [PATCH] Added support for Arrays in sample_conv_json_query in sample.c
On Mon, Sep 25, 2023 at 01:30:53PM +, Jens Popp wrote: > 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. > Hello Jens, Thank you for you patch, I just pushed it into master. I also added a reg-test into the patch: https://github.com/haproxy/haproxy/commit/f216b45bb94475aafcdd855fbf358cc812eb1d33#diff-c29792e4360cebe8528660d3a2da5c0401201d3c0490bf72cfe7b6cef0cb34e3R101 Don't forget to add one next time you contribute so we can easily test if we broke the feature by accident! Regards, -- William Lallemand
RE: RE: RE: [PATCH] Added support for Arrays in sample_conv_json_query in sample.c
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. --- doc/configuration.txt | 14 +++--- src/sample.c | 9 - 2 files changed, 19 insertions(+), 4 deletions(-) diff --git a/doc/configuration.txt b/doc/configuration.txt index d49d359a2..785cbb77c 100644 --- a/doc/configuration.txt +++ b/doc/configuration.txt @@ -18356,10 +18356,18 @@ json([]) {"ip":"127.0.0.1","user-agent":"Very \"Ugly\" UA 1\/2"} json_query(,[]) - 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. must be a valid JSON Path string as defined in https://datatracker.ietf.org/doc/draft-ietf-jsonpath-base/ diff --git a/src/sample.c b/src/sample.c index 07c881dcf..a937d5283 100644 --- a/src/sample.c +++ b/src/sample.c @@ -4159,8 +4159,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; -- 2.39.3 [X] An Elisa camLine Holding GmbH company - www.camline.com camLine GmbH - Fraunhoferring 9, 85238 Petershausen, Germany Amtsgericht München HRB 88821 Managing Directors: Frank Bölstler, Evelyn Tag, Bernhard Völker The content of this message is CAMLINE CONFIDENTIAL. If you are not the intended recipient, please notify me, delete this email and do not use or distribute this email.
Re: RE: RE: [PATCH] Added support for Arrays in sample_conv_json_query in sample.c
Hello, On 25/09/2023 13:55, Jens Popp wrote: 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 --- doc/configuration.txt | 14 +++--- src/sample.c | 11 ++- 2 files changed, 21 insertions(+), 4 deletions(-) diff --git a/doc/configuration.txt b/doc/configuration.txt index d49d359a2..785cbb77c 100644 --- a/doc/configuration.txt +++ b/doc/configuration.txt @@ -18356,10 +18356,18 @@ json([]) {"ip":"127.0.0.1","user-agent":"Very \"Ugly\" UA 1\/2"} json_query(,[]) - 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. must be a valid JSON Path string as defined in https://datatracker.ietf.org/doc/draft-ietf-jsonpath-base/ diff --git a/src/sample.c b/src/sample.c index 07c881dcf..2751ad3a0 100644 --- a/src/sample.c +++ b/src/sample.c @@ -4159,8 +4159,17 @@ 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"] + int len; + len = b_putblk(trash, token, token_size); + trash->data = len; + 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; -- 2.39.3 You don't need the intermediary 'len' variable (which is not of the same type as what b_putblk returns). You can simply replace it by trash->data directly. Apart from that the patch looks good to me. Rémi LB
RE: RE: [PATCH] Added support for Arrays in sample_conv_json_query in sample.c
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 --- doc/configuration.txt | 14 +++--- src/sample.c | 11 ++- 2 files changed, 21 insertions(+), 4 deletions(-) diff --git a/doc/configuration.txt b/doc/configuration.txt index d49d359a2..785cbb77c 100644 --- a/doc/configuration.txt +++ b/doc/configuration.txt @@ -18356,10 +18356,18 @@ json([]) {"ip":"127.0.0.1","user-agent":"Very \"Ugly\" UA 1\/2"} json_query(,[]) - 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. must be a valid JSON Path string as defined in https://datatracker.ietf.org/doc/draft-ietf-jsonpath-base/ diff --git a/src/sample.c b/src/sample.c index 07c881dcf..2751ad3a0 100644 --- a/src/sample.c +++ b/src/sample.c @@ -4159,8 +4159,17 @@ 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"] + int len; + len = b_putblk(trash, token, token_size); + trash->data = len; + 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; -- 2.39.3 [X] An Elisa camLine Holding GmbH company - www.camline.com camLine GmbH - Fraunhoferring 9, 85238 Petershausen, Germany Amtsgericht München HRB 88821 Managing Directors: Frank Bölstler, Evelyn Tag, Bernhard Völker The content of this message is CAMLINE CONFIDENTIAL. If you are not the intended recipient, please notify me, delete this email and do not use or distribute this email.
RE: [PATCH] Added support for Arrays in sample_conv_json_query in sample.c
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 --- doc/configuration.txt | 14 +++--- src/sample.c | 10 +- 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/doc/configuration.txt b/doc/configuration.txt index d49d359a2..785cbb77c 100644 --- a/doc/configuration.txt +++ b/doc/configuration.txt @@ -18356,10 +18356,18 @@ json([]) {"ip":"127.0.0.1","user-agent":"Very \"Ugly\" UA 1\/2"} json_query(,[]) - 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. must be a valid JSON Path string as defined in https://datatracker.ietf.org/doc/draft-ietf-jsonpath-base/ diff --git a/src/sample.c b/src/sample.c index 07c881dcf..c6a89bc90 100644 --- a/src/sample.c +++ b/src/sample.c @@ -4159,8 +4159,16 @@ static int sample_conv_json_query(const struct arg *args, struct sample *smp, vo return 1; } - case MJSON_TOK_NULL: 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"] + int len; + len = b_putblk(trash, token, token_size); + trash->data = len; + smp->data.u.str = *trash; + smp->data.type = SMP_T_STR; + return 1; + case MJSON_TOK_NULL: case MJSON_TOK_OBJECT: /* We cannot handle these. */ return 0; -- 2.39.3 [X] An Elisa camLine Holding GmbH company - www.camline.com camLine GmbH - Fraunhoferring 9, 85238 Petershausen, Germany Amtsgericht München HRB 88821 Managing Directors: Frank Bölstler, Evelyn Tag, Bernhard Völker The content of this message is CAMLINE CONFIDENTIAL. If you are not the intended recipient, please notify me, delete this email and do not use or distribute this email.
Re: [PATCH] Added support for Arrays in sample_conv_json_query in sample.c
Hello, On 18/09/2023 10:27, Jens Popp wrote: 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. --- src/sample.c | 10 +- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/sample.c b/src/sample.c index 07c881dcf..ecc4a961d 100644 --- a/src/sample.c +++ b/src/sample.c @@ -4159,8 +4159,16 @@ static int sample_conv_json_query(const struct arg *args, struct sample *smp, vo return 1; } - case MJSON_TOK_NULL: 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"] + strncpy( trash->area, token, token_size); + trash->data = token_size; + trash->size = token_size; + smp->data.u.str = *trash; + smp->data.type = SMP_T_STR; + return 1; + case MJSON_TOK_NULL: case MJSON_TOK_OBJECT: /* We cannot handle these. */ return 0; -- 2.39.3 [X] You might want to use the b_putblk function instead of strncpy since you don't manage the case where you don't have enough space in the trash buffer. The trash->size member should never be changed as well since it represents the buffer size, not the number of bytes written in it. Please update the "json_query" documentation as well. Thanks Rémi LB