From c63dcc66554951dd53b588518e272061f95eb375 Mon Sep 17 00:00:00 2001 From: Jeremie Leska Date: Tue, 13 Jan 2026 16:40:15 +0100 Subject: [PATCH] schema: remove error after lyd_value_validate_dflt calls libyang returns the realtype field when the store callback return code is LY_SUCCESS and LY_EINCOMPLETE. Don't raise an error when lyd_value_validate_dflt returns LY_EINCOMPLETE. The function can then use the val_type_cdata field. Add a leafref with a default value to yolo-nodetypes.yang. In this case, lyd_value_validate_dflt returns LY_EINCOMPLETE. In test_schema.py, check that the default function returns a correct str value. Link: https://github.com/CESNET/libyang/blob/master/src/tree_data_common.c#L682 Signed-off-by: Jeremie Leska --- libyang/schema.py | 4 ++-- tests/test_schema.py | 6 ++++++ tests/yang/yolo/yolo-nodetypes.yang | 14 ++++++++++++++ 3 files changed, 22 insertions(+), 2 deletions(-) diff --git a/libyang/schema.py b/libyang/schema.py index f104a87..e8bbe70 100644 --- a/libyang/schema.py +++ b/libyang/schema.py @@ -1545,7 +1545,7 @@ def default(self) -> Union[None, bool, int, str, float]: val_type_cdata, ffi.NULL, ) - if ret != lib.LY_SUCCESS: + if ret not in (lib.LY_SUCCESS, lib.LY_EINCOMPLETE): raise self.context.error("Unable to get real type of default value") self.cdata_default_realtype = Type(self.context, val_type_cdata[0], None) @@ -1619,7 +1619,7 @@ def defaults(self) -> Iterator[Union[None, bool, int, str, float]]: val_type_cdata, ffi.NULL, ) - if ret != lib.LY_SUCCESS: + if ret not in (lib.LY_SUCCESS, lib.LY_EINCOMPLETE): raise self.context.error("Unable to get real type of default value") self.cdata_default_realtypes.append( Type(self.context, val_type_cdata[0], None) diff --git a/tests/test_schema.py b/tests/test_schema.py index a1ca412..52a7af7 100644 --- a/tests/test_schema.py +++ b/tests/test_schema.py @@ -732,6 +732,9 @@ def test_must(self): def test_leaf_default(self): leaf = next(self.ctx.find_path("/yolo-nodetypes:conf/percentage")) self.assertIsInstance(leaf.default(), float) + leaf = next(self.ctx.find_path("/yolo-nodetypes:conf/leafref1")) + self.assertIsInstance(leaf.default(), str) + self.assertEqual("ASD", leaf.default()) def test_leaf_parsed(self): leaf = next(self.ctx.find_path("/yolo-nodetypes:conf/percentage")) @@ -789,6 +792,9 @@ def test_leaflist_defaults(self): leaflist = next(self.ctx.find_path("/yolo-nodetypes:conf/integers")) for d in leaflist.defaults(): self.assertIsInstance(d, int) + leaflist3 = next(self.ctx.find_path("/yolo-nodetypes:conf/leaf-list3")) + for d in leaflist3.defaults(): + self.assertIsInstance(d, str) def test_leaf_list_min_max(self): leaflist1 = next(self.ctx.find_path("/yolo-nodetypes:conf/leaf-list1")) diff --git a/tests/yang/yolo/yolo-nodetypes.yang b/tests/yang/yolo/yolo-nodetypes.yang index 5b99475..9926b1a 100644 --- a/tests/yang/yolo/yolo-nodetypes.yang +++ b/tests/yang/yolo/yolo-nodetypes.yang @@ -93,6 +93,20 @@ module yolo-nodetypes { leaf-list leaf-list2 { type string; } + + leaf leafref1 { + type leafref { + path "/records/name"; + } + default "ASD"; + } + + leaf-list leaf-list3 { + type leafref { + path "/records/name"; + } + default "ASD"; + } } leaf test1 {