Skip to content

Add keepTrailingNewline option to LegacyOverrides to match Python Jinja2 default behaviour#1311

Open
jmoraleda wants to merge 1 commit intoHubSpot:masterfrom
jmoraleda:keep_trailing_newline
Open

Add keepTrailingNewline option to LegacyOverrides to match Python Jinja2 default behaviour#1311
jmoraleda wants to merge 1 commit intoHubSpot:masterfrom
jmoraleda:keep_trailing_newline

Conversation

@jmoraleda
Copy link
Copy Markdown

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 keepTrailingNewline flag to LegacyOverrides that allows users to opt into Python-compatible behaviour.

Changes:

  • LegacyOverrides: adds isKeepTrailingNewline(), defaulting to true (preserve existing Jinjava behaviour — no impact on current users).
  • JinjavaInterpreter: applies the strip in stripTrailingNewlineIfNeeded(), called at the normal return points of the private render() method. Early-exit error paths (OutputTooBigException, CollectionTooBigException) are intentionally unaffected.
  • TrailingNewlineTest: covers both flag values, the NONE/THREE_POINT_0/ALL presets, and edge cases (no trailing newline, multiple trailing newlines, rendered expressions).

Usage:

To opt into Python-compatible behaviour:

JinjavaConfig config = JinjavaConfig.newBuilder()
    .withLegacyOverrides(
        LegacyOverrides.newBuilder()
            .withKeepTrailingNewline(false)
            .build()
    )
    .build();

Compatibility note:

keepTrailingNewline is set to true (legacy behaviour) in THREE_POINT_0 to avoid breaking existing tests. It is set to false in ALL. Maintainers may wish to move it to false in THREE_POINT_0 as 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant