diff --git a/pyangbind/lib/serialise.py b/pyangbind/lib/serialise.py index c771385..f1d4d78 100644 --- a/pyangbind/lib/serialise.py +++ b/pyangbind/lib/serialise.py @@ -429,7 +429,8 @@ def generate_ietf_tree(obj, parent_namespace=None, flt=False, with_defaults=None d[yname] = generate_ietf_tree( element, parent_namespace=element._namespace, flt=flt, with_defaults=with_defaults ) - if not len(d[yname]): + present = getattr(element, "_cpresent", False) + if not len(d[yname]) and not present: del d[yname] elif generated_by == "YANGListType": d[yname] = [ diff --git a/tests/presence/run.py b/tests/presence/run.py index c2191a8..5aba6c6 100755 --- a/tests/presence/run.py +++ b/tests/presence/run.py @@ -97,28 +97,35 @@ def test_009_presence_get(self): def test_010_presence_serialise(self): self.instance.parent.child._set_present() + self.instance.p_container._set_present() expectedJ = """ { "parent": { "child": {} - } + }, + "p-container": {} }""" self.assertEqual(json.loads(pbJ.dumps(self.instance)), json.loads(expectedJ)) self.instance.parent.child._set_present(False) - expectedJ = "{}" + expectedJ = """ + { + "p-container": {} + }""" self.assertEqual(json.loads(pbJ.dumps(self.instance)), json.loads(expectedJ)) def test_011_presence_serialise_ietf(self): self.instance.parent.child._set_present() + self.instance.p_container._set_present() expectedJ = """ { "presence:parent": { "child": {} - } + }, + "presence:p-container": {} }""" self.assertEqual(json.loads(pbJ.dumps(self.instance, mode="ietf")), json.loads(expectedJ)) self.instance.parent.child._set_present(False) - expectedJ = "{}" + expectedJ = """{"presence:p-container": {}}""" self.assertEqual(json.loads(pbJ.dumps(self.instance, mode="ietf")), json.loads(expectedJ)) def test_012_presence_deserialise(self): @@ -126,20 +133,24 @@ def test_012_presence_deserialise(self): { "parent": { "child": {} - } + }, + "p-container": {} }""" x = pbJ.loads(inputJ, self.bindings, "presence") self.assertIs(x.parent.child._present(), True) + self.assertIs(x.p_container._present(), True) def test_013_presence_deserialise(self): inputJ = """ { "presence:parent": { "child": {} - } + }, + "presence:p-container": {} }""" x = pbJ.loads_ietf(inputJ, self.bindings, "presence") self.assertIs(x.parent.child._present(), True) + self.assertIs(x.p_container._present(), True) class SplitClassesPresenceTests(PresenceTests):