Skip to content

Conversation

@dwoz
Copy link
Contributor

@dwoz dwoz commented Dec 14, 2025

When using import_yaml to load YAML data with multi-line scalars (using |), the newlines were being rendered as literal \n instead of actual newlines when passed through Jinja templates.

This fix adds an _unescape_newlines function that recursively converts literal \n sequences back to actual newlines in the context dictionary before it's used for templating.

  • Added _unescape_newlines function to salt/utils/templates.py
  • Applied unescape to context before creating decoded_context
  • Updated test to remove to_dict filter reference

Fixes #30690

When using import_yaml to load YAML data with multi-line scalars (using |),
the newlines were being rendered as literal \n instead of actual newlines
when passed through Jinja templates.

This fix adds an _unescape_newlines function that recursively converts
literal \n sequences back to actual newlines in the context dictionary
before it's used for templating.

- Added _unescape_newlines function to salt/utils/templates.py
- Applied unescape to context before creating decoded_context
- Updated test to remove to_dict filter reference
return [_unescape_newlines(item) for item in data]
elif isinstance(data, str):
# Convert literal \n (backslash-n) to actual newline
return data.replace("\\n", "\n")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What happens if someone had escaped the \ in the data like \\n? With this change is there a way to get a literal \n anymore?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What happens on Windows? Does it need to also look for \\r\\n? Maybe need to use os.linesep instead?

from salt.loader.context import NamedLoaderContext
from salt.loader.dunder import __file_client__
from salt.utils.decorators.jinja import JinjaFilter, JinjaGlobal, JinjaTest
from salt.utils.odict import OrderedDict
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't look right. Wasn't odict deprecated?

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

Labels

test:full Run the full test suite

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Newlines may be rendered as literal \n for multi-line scalar variables in some circumstances

3 participants