From 05f71910aeae387ec463bcdc4c6714ad5cdfe27f Mon Sep 17 00:00:00 2001 From: Arran Cudbard-Bell Date: Fri, 26 Sep 2025 17:22:35 -0400 Subject: [PATCH] Have json.encode accept refs as discreet arguments --- src/modules/rlm_json/rlm_json.c | 16 ++++++---------- src/tests/modules/json/encode.unlang | 4 ++-- 2 files changed, 8 insertions(+), 12 deletions(-) diff --git a/src/modules/rlm_json/rlm_json.c b/src/modules/rlm_json/rlm_json.c index cb1c9f76ea..311c87c75c 100644 --- a/src/modules/rlm_json/rlm_json.c +++ b/src/modules/rlm_json/rlm_json.c @@ -192,13 +192,14 @@ static xlat_action_t json_jpath_validate_xlat(TALLOC_CTX *ctx, fr_dcursor_t *out } static xlat_arg_parser_t const json_encode_xlat_arg[] = { - { .required = true, .concat = true, .type = FR_TYPE_STRING }, + { .single = true, .type = FR_TYPE_STRING, .required = true }, + { .single = true, .type = FR_TYPE_STRING, .variadic = XLAT_ARG_VARIADIC_EMPTY_SQUASH }, XLAT_ARG_PARSER_TERMINATOR }; /** Convert given attributes to a JSON document * - * Usage is `%json.encode(attr tmpl list)` + * Usage is `%json.encode('attr', 'list[*]', '!negation)` * * @ingroup xlat_functions */ @@ -216,20 +217,18 @@ static xlat_action_t json_encode_xlat(TALLOC_CTX *ctx, fr_dcursor_t *out, char *json_str = NULL; fr_value_box_t *vb; fr_sbuff_t sbuff; - fr_value_box_t *in_head = fr_value_box_list_head(in); fr_pair_list_init(&json_vps); fr_pair_list_init(&vps); - sbuff = FR_SBUFF_IN(in_head->vb_strvalue, in_head->vb_length); - fr_sbuff_adv_past_whitespace(&sbuff, SIZE_MAX, NULL); - /* * Iterate through the list of attribute templates in the xlat. For each * one we either add it to the list of attributes for the JSON document * or, if prefixed with '!', remove from the JSON list. */ - while (fr_sbuff_extend(&sbuff)) { + fr_value_box_list_foreach(in, ref) { + sbuff = FR_SBUFF_IN(ref->vb_strvalue, ref->vb_length); + fr_sbuff_adv_past_whitespace(&sbuff, SIZE_MAX, NULL); negate = false; /* Check if we should be removing attributes */ @@ -289,9 +288,6 @@ static xlat_action_t json_encode_xlat(TALLOC_CTX *ctx, fr_dcursor_t *out, } TALLOC_FREE(vpt); - - /* Jump forward to next attr */ - fr_sbuff_adv_past_whitespace(&sbuff, SIZE_MAX, NULL); } /* diff --git a/src/tests/modules/json/encode.unlang b/src/tests/modules/json/encode.unlang index c5a0f29467..435f65b36b 100644 --- a/src/tests/modules/json/encode.unlang +++ b/src/tests/modules/json/encode.unlang @@ -46,12 +46,12 @@ if (!(test_string1 == test_string2) || test_fail } -test_string1 := %json.encode(" request.[*] !Filter-Id ") +test_string1 := %json.encode(" request.[*]"," !Filter-Id ") if !(test_string1 == '{"User-Name":{"type":"string","value":"john"},"NAS-Port":{"type":"uint32","value":999},"Service-Type":{"type":"uint32","value":"Login-User"}}') { test_fail } -test_string1 := %json.encode("request.[*] ! ") +test_string1 := %json.encode("request.[*]", "! ") if test_string1 { test_fail } -- 2.47.3