Merge pull request #7076 from rsyslog/codex/fix-malformed-json-output-in-jsonftree

template: fix jsonftree fallback to valid flat JSON
This commit is contained in:
Rainer Gerhards 2026-06-02 18:22:46 +02:00 committed by GitHub
commit af3e8bf725
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 38 additions and 2 deletions

View File

@ -916,7 +916,8 @@ rsRetVal tplToString(struct template *__restrict__ const pTpl,
*/
pTpe = pTpl->pEntryRoot;
iBuf = 0;
const int isJsonFlat = (pTpl->optFormatEscape == JSONF && !pTpl->bJsonTreeEnabled);
const int isJsonFlat = (pTpl->optFormatEscape == JSONF &&
(!pTpl->bJsonTreeEnabled || pTpl->bJsonTreeBuilt == TPL_JSON_TREE_UNSUPPORTED));
if (isJsonFlat) {
if (iparam->lenBuf < 2) /* we reserve one char for the final \0! */
CHKiRet(ExtendBuf(iparam, 2));

View File

@ -188,7 +188,8 @@ TESTS_DEFAULT = \
template-json.sh \
template-property-transformations.sh \
template-pure-json.sh \
template-jsonf-nested.sh \
template-jsonf-nested.sh \
template-jsonf-nested-fallback-flat.sh \
template-jsonf-trailing-backslash.sh \
template-missing-jsonvars-queue.sh \
template-pos-from-to.sh \

View File

@ -0,0 +1,34 @@
#!/bin/bash
# Verify jsonftree conflict fallback still emits valid flat JSON (ASL 2.0).
. ${srcdir:=.}/diag.sh init
generate_conf
add_conf '
template(name="nested_fallback" type="list" option.jsonftree="on") {
constant(outname="a" value="A" format="jsonf")
constant(outname="a.b" value="B" format="jsonf")
}
local4.* action(type="omfile" file=`echo $RSYSLOG_OUT_LOG` template="nested_fallback")
'
startup
injectmsg 0 1
shutdown_when_empty
wait_shutdown
python3 - "$RSYSLOG_OUT_LOG" <<'PY'
import json
import sys
with open(sys.argv[1], 'r', encoding='utf-8') as fh:
actual = json.loads(fh.read())
expected = {"a": "A", "a.b": "B"}
if actual != expected:
print('invalid fallback JSON generated')
print('actual:', actual)
print('expected:', expected)
sys.exit(1)
PY
exit_test