Skip to content

Commit

Permalink
Fix missing identityref namespace (#322)
Browse files Browse the repository at this point in the history
* Add test coverage for identityref namespace fix

* Fix missing namespace in identityref field

When we generate python code from Yang module, the restriction_arg for
the field doesn't include module name as the prefix.

---------

Co-authored-by: guojunzhang <guojunzhang1981@gmail.com>
  • Loading branch information
2 people authored and JoseIgnacioTamayo committed Dec 11, 2023
1 parent 84624eb commit 0e5a837
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 8 deletions.
2 changes: 2 additions & 0 deletions pyangbind/helpers/identity.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ def __init__(self, name):
self.children = []

def add_prefix(self, prefix):
if self.source_module not in self._imported_prefixes:
self._imported_prefixes.append(self.source_module)
if prefix not in self._imported_prefixes:
self._imported_prefixes.append(prefix)

Expand Down
36 changes: 28 additions & 8 deletions tests/identityref/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import unittest

from pyangbind.lib import pybindJSON
from pyangbind.lib.serialise import pybindJSONDecoder
from tests.base import PyangBindTestCase


Expand Down Expand Up @@ -111,7 +112,12 @@ def test_set_ancestral_identities_four(self):
self.assertEqual(allowed, valid)

def test_grouping_identity_inheritance(self):
for address_type, valid in [("source-dest", True), ("lcaf", True), ("unknown", False)]:
for address_type, valid in [
("source-dest", True),
("lcaf", True),
("unknown", False),
("identityref:source-dest", True),
]:
with self.subTest(address_type=address_type, valid=valid):
allowed = True
try:
Expand Down Expand Up @@ -158,14 +164,28 @@ def test_json_ietf_serialise_namespace_handling_remote(self):
)

def test_json_ietf_serialise_namespace_handling_local(self):
self.instance.ak.address_type = "lcaf"
data = json.loads(pybindJSON.dumps(self.instance, mode="ietf"))
# The JSON representation of the identityref may have, or may omit,
# the namespace, as the leaf `address-type` and the identity `lcaf` are
# defined in the same module
for identity in ["lcaf", "identityref:lcaf"]:
with self.subTest(identity=identity):
self.instance.ak.address_type = "lcaf"
data = json.loads(pybindJSON.dumps(self.instance, mode="ietf"))
# The JSON representation of the identityref may have, or may omit,
# the namespace, as the leaf `address-type` and the identity `lcaf` are
# defined in the same module, so accept either form
self.assertIn(
data["identityref:ak"]["address-type"],
["lcaf", "identityref:lcaf"],
)

def test_load_identityref_with_module_prefix(self):
json = {
"identityref:ak": {
"address-type": "identityref:source-dest",
}
}
obj = pybindJSONDecoder.load_ietf_json(json, self.bindings, "identityref")
self.assertIn(
data["identityref:ak"]["address-type"],
["lcaf", "identityref:lcaf"],
obj.ak.address_type,
["identityref:source-dest", "source-dest"],
)


Expand Down

0 comments on commit 0e5a837

Please sign in to comment.