-
Notifications
You must be signed in to change notification settings - Fork 5.6k
Fix #30690: Preserve newlines in import_yaml multi-line scalars #68535
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: 3006.x
Are you sure you want to change the base?
Conversation
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") |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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?
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.
Fixes #30690