Add keepTrailingNewline option to LegacyOverrides to match Python Jinja2 default behaviour#1311
Add keepTrailingNewline option to LegacyOverrides to match Python Jinja2 default behaviour#1311jmoraleda wants to merge 1 commit intoHubSpot:masterfrom
Conversation
jasmith-hs
left a comment
There was a problem hiding this comment.
Thanks @jmoraleda for the PR. Could you change it so that the default is false like the other legacy override options and rename it something like stripTrailingNewlines?
Also, as Jinjava 3.0 has not been published yet, so this option can be turned on for 3.0 so that the default behaviour of Jinjava 3.0 is as close to Jinja as possible
|
Thank you @jasmith-hs :
|
|
Ah, thanks @jmoraleda, since it's configurable in Jinja, I believe that it would be best to have the option added to the top-level JinjavaConfig. We an have this be defaulting to
|

Description:
By default, Python Jinja2 strips a single trailing newline from rendered output (
keep_trailing_newline=False). Jinjava has historically preserved trailing newlines, causing a subtle but consistent difference when porting templates from Python Jinja2.This PR adds a
keepTrailingNewlineflag toLegacyOverridesthat allows users to opt into Python-compatible behaviour.Changes:
LegacyOverrides: addsisKeepTrailingNewline(), defaulting totrue(preserve existing Jinjava behaviour — no impact on current users).JinjavaInterpreter: applies the strip instripTrailingNewlineIfNeeded(), called at the normal return points of the privaterender()method. Early-exit error paths (OutputTooBigException,CollectionTooBigException) are intentionally unaffected.TrailingNewlineTest: covers both flag values, theNONE/THREE_POINT_0/ALLpresets, and edge cases (no trailing newline, multiple trailing newlines, rendered expressions).Usage:
To opt into Python-compatible behaviour:
Compatibility note:
keepTrailingNewlineis set totrue(legacy behaviour) inTHREE_POINT_0to avoid breaking existing tests. It is set tofalseinALL. Maintainers may wish to move it tofalseinTHREE_POINT_0as well if they consider stripping the trailing newline to be part of the canonical Python-compatible behaviour set — the change is intentionally conservative here to leave that decision to the maintainers.