mirror of
https://github.com/home-assistant/core.git
synced 2025-07-14 16:57:10 +00:00
Prevent template changing options (#20775)
* Prevent complex template validation changing input value * Remove deprecation warnings
This commit is contained in:
parent
e6cd04d711
commit
59393ab085
@ -436,13 +436,15 @@ def template(value):
|
|||||||
def template_complex(value):
|
def template_complex(value):
|
||||||
"""Validate a complex jinja2 template."""
|
"""Validate a complex jinja2 template."""
|
||||||
if isinstance(value, list):
|
if isinstance(value, list):
|
||||||
for idx, element in enumerate(value):
|
return_value = value.copy()
|
||||||
value[idx] = template_complex(element)
|
for idx, element in enumerate(return_value):
|
||||||
return value
|
return_value[idx] = template_complex(element)
|
||||||
|
return return_value
|
||||||
if isinstance(value, dict):
|
if isinstance(value, dict):
|
||||||
for key, element in value.items():
|
return_value = value.copy()
|
||||||
value[key] = template_complex(element)
|
for key, element in return_value.items():
|
||||||
return value
|
return_value[key] = template_complex(element)
|
||||||
|
return return_value
|
||||||
|
|
||||||
return template(value)
|
return template(value)
|
||||||
|
|
||||||
|
@ -402,8 +402,7 @@ def test_template():
|
|||||||
schema = vol.Schema(cv.template)
|
schema = vol.Schema(cv.template)
|
||||||
|
|
||||||
for value in (None, '{{ partial_print }', '{% if True %}Hello', ['test']):
|
for value in (None, '{{ partial_print }', '{% if True %}Hello', ['test']):
|
||||||
with pytest.raises(vol.Invalid,
|
with pytest.raises(vol.Invalid):
|
||||||
message='{} not considered invalid'.format(value)):
|
|
||||||
schema(value)
|
schema(value)
|
||||||
|
|
||||||
options = (
|
options = (
|
||||||
@ -433,6 +432,15 @@ def test_template_complex():
|
|||||||
for value in options:
|
for value in options:
|
||||||
schema(value)
|
schema(value)
|
||||||
|
|
||||||
|
# ensure the validator didn't mutate the input
|
||||||
|
assert options == (
|
||||||
|
1, 'Hello',
|
||||||
|
'{{ beer }}',
|
||||||
|
'{% if 1 == 1 %}Hello{% else %}World{% endif %}',
|
||||||
|
{'test': 1, 'test2': '{{ beer }}'},
|
||||||
|
['{{ beer }}', 1]
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
def test_time_zone():
|
def test_time_zone():
|
||||||
"""Test time zone validation."""
|
"""Test time zone validation."""
|
||||||
|
Loading…
x
Reference in New Issue
Block a user