rsyslog/tests/json-omitifzero.sh
Rainer Gerhards 15ad71074a
template: add omitIfZero option to property()
Why:
Ensures high-performance JSON emission can comply with ECS (Elastic Common
Schema) requirements where numerical zero values should often be omitted
rather than emitted as '0'.

Impact:
Adds a new property() parameter 'omitIfZero' that affects templates using
format='jsonf' and dataType='number'. No change to existing templates.

Before/After:
Previously, numerical properties in jsonf mode always emitted their value
(e.g., '"field":0'); with this change, they can be completely omitted.

Technical Overview:
- Extended templateEntry options in template.h with bOmitIfZero bitfield.
- Updated template.c to parse the 'omitifzero' binary parameter.
- Implemented omission logic in tplJsonRenderValue (template.c) and
  jsonField (runtime/msg.c).
- Standardized memory safety by using the project-standard CHKmalloc()
  macro for all es_str2cstr() allocations and other memory checks.
- Standardized error handling by replacing explicit gotos with the
  FINALIZE; macro across affected areas.
- Formatted modified files using devtools/format-code.sh for full
  compliance with project style rules.
- Registered tests/json-omitifzero.sh following the "Define at Top,
  Distribute Unconditionally, Register Conditionally" pattern.

Issue: https://github.com/rsyslog/rsyslog/issues/6176

With the help of AI-Agents: Antigravity
2026-01-25 15:02:32 +01:00

28 lines
995 B
Bash
Executable File

#!/bin/bash
# added 2026-01-24 by Rainer Gerhards; Released under ASL 2.0
. ${srcdir:=.}/diag.sh init
generate_conf
add_conf '
template(name="outfmt" type="list" option.jsonf="on") {
property(name="$!zero" outname="zero_default" format="jsonf" dataType="number")
property(name="$!zero" outname="zero_omit" format="jsonf" dataType="number" omitIfZero="on")
property(name="$!nonzero" outname="nonzero_omit" format="jsonf" dataType="number" omitIfZero="on")
property(name="$!zero" outname="zero_string_omit" format="jsonf" dataType="string" omitIfZero="on")
property(name="$!empty" outname="empty_omit" format="jsonf" dataType="number" omitIfZero="on" onEmpty="skip")
}
set $!zero = 0;
set $!nonzero = 42;
set $!empty = "";
local4.* action(type="omfile" file=`echo $RSYSLOG_OUT_LOG` template="outfmt")
'
startup
injectmsg
shutdown_when_empty
wait_shutdown
export EXPECTED='{"zero_default":0, "nonzero_omit":42, "zero_string_omit":"0"}'
cmp_exact "$RSYSLOG_OUT_LOG"
exit_test