Prevent nesting sections in data entry flows (#124645)
This commit is contained in:
parent
e39b3796f3
commit
174f22aa2f
2 changed files with 35 additions and 1 deletions
|
@ -1090,6 +1090,11 @@ def key_dependency[_KT: Hashable, _VT](
|
||||||
|
|
||||||
|
|
||||||
def custom_serializer(schema: Any) -> Any:
|
def custom_serializer(schema: Any) -> Any:
|
||||||
|
"""Serialize additional types for voluptuous_serialize."""
|
||||||
|
return _custom_serializer(schema, allow_section=True)
|
||||||
|
|
||||||
|
|
||||||
|
def _custom_serializer(schema: Any, *, allow_section: bool) -> Any:
|
||||||
"""Serialize additional types for voluptuous_serialize."""
|
"""Serialize additional types for voluptuous_serialize."""
|
||||||
from .. import data_entry_flow # pylint: disable=import-outside-toplevel
|
from .. import data_entry_flow # pylint: disable=import-outside-toplevel
|
||||||
from . import selector # pylint: disable=import-outside-toplevel
|
from . import selector # pylint: disable=import-outside-toplevel
|
||||||
|
@ -1104,10 +1109,15 @@ def custom_serializer(schema: Any) -> Any:
|
||||||
return {"type": "boolean"}
|
return {"type": "boolean"}
|
||||||
|
|
||||||
if isinstance(schema, data_entry_flow.section):
|
if isinstance(schema, data_entry_flow.section):
|
||||||
|
if not allow_section:
|
||||||
|
raise ValueError("Nesting expandable sections is not supported")
|
||||||
return {
|
return {
|
||||||
"type": "expandable",
|
"type": "expandable",
|
||||||
"schema": voluptuous_serialize.convert(
|
"schema": voluptuous_serialize.convert(
|
||||||
schema.schema, custom_serializer=custom_serializer
|
schema.schema,
|
||||||
|
custom_serializer=functools.partial(
|
||||||
|
_custom_serializer, allow_section=False
|
||||||
|
),
|
||||||
),
|
),
|
||||||
"expanded": not schema.options["collapsed"],
|
"expanded": not schema.options["collapsed"],
|
||||||
}
|
}
|
||||||
|
|
|
@ -1098,3 +1098,27 @@ def test_section_in_serializer() -> None:
|
||||||
],
|
],
|
||||||
"type": "expandable",
|
"type": "expandable",
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
def test_nested_section_in_serializer() -> None:
|
||||||
|
"""Test section with custom_serializer."""
|
||||||
|
with pytest.raises(
|
||||||
|
ValueError, match="Nesting expandable sections is not supported"
|
||||||
|
):
|
||||||
|
cv.custom_serializer(
|
||||||
|
data_entry_flow.section(
|
||||||
|
vol.Schema(
|
||||||
|
{
|
||||||
|
vol.Required("section_1"): data_entry_flow.section(
|
||||||
|
vol.Schema(
|
||||||
|
{
|
||||||
|
vol.Optional("option_1", default=False): bool,
|
||||||
|
vol.Required("option_2"): int,
|
||||||
|
}
|
||||||
|
)
|
||||||
|
)
|
||||||
|
}
|
||||||
|
),
|
||||||
|
{"collapsed": False},
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
Loading…
Add table
Reference in a new issue