From: Aleš Mrázek Date: Mon, 28 Aug 2023 13:11:07 +0000 (+0200) Subject: modeling/base_schema.py: empty lists not allowed X-Git-Tag: v6.0.3~8^2~1 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=60baef24a1f4c127568ea3d53198f39b849164bc;p=thirdparty%2Fknot-resolver.git modeling/base_schema.py: empty lists not allowed --- diff --git a/manager/knot_resolver_manager/utils/modeling/base_schema.py b/manager/knot_resolver_manager/utils/modeling/base_schema.py index 14c97d2f9..b9b584c3c 100644 --- a/manager/knot_resolver_manager/utils/modeling/base_schema.py +++ b/manager/knot_resolver_manager/utils/modeling/base_schema.py @@ -294,6 +294,8 @@ class ObjectMapper: try: for i, val in enumerate(obj): res.append(self.map_object(inner_type, val, object_path=f"{object_path}[{i}]")) + if len(res) == 0: + raise DataValidationError("empty list is not allowed", object_path) except DataValidationError as e: errs.append(e) except TypeError as e: diff --git a/manager/tests/unit/utils/modeling/test_base_schema.py b/manager/tests/unit/utils/modeling/test_base_schema.py index ca41572d1..07e278bbc 100644 --- a/manager/tests/unit/utils/modeling/test_base_schema.py +++ b/manager/tests/unit/utils/modeling/test_base_schema.py @@ -53,6 +53,14 @@ def test_parsing_str_invalid(): _TestStr(parse_yaml("v: false")) # bool +def test_parsing_list_empty(): + class ListSchema(ConfigSchema): + empty: List[Any] + + with raises(DataValidationError): + ListSchema(parse_yaml("empty: []")) + + @pytest.mark.parametrize("typ,val", [(_TestInt, 5), (_TestBool, False), (_TestStr, "test")]) def test_parsing_nested(typ: Type[ConfigSchema], val: Any): class UpperSchema(ConfigSchema):