Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't prefix a property value that is already prefixed #313

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion pyangbind/lib/serialise.py
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,9 @@ def yangt_identityref(self, obj):
try:
emod = obj._enumeration_dict[obj]["@module"]
if emod != obj._defining_module:
return "%s:%s" % (obj._enumeration_dict[obj]["@module"], obj)
# if not already prefixed with the type namespace
if not obj.startswith("%s" % emod):
return "%s:%s" % (emod, obj)
except KeyError:
pass
return six.text_type(obj)
Expand Down
26 changes: 26 additions & 0 deletions tests/identityref/run.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
#!/usr/bin/env python

import json
import unittest

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


Expand Down Expand Up @@ -142,6 +144,30 @@ def test_set_identityref_from_imported_module_referencing_local(self):
allowed = False
self.assertEqual(allowed, valid)

def test_json_ietf_serialise_namespace_handling_remote(self):
for identity in ["remote-id", "remote-two:remote-id"]:
with self.subTest(identity=identity):
self.instance.ietfint.ref = identity
data = json.loads(pybindJSON.dumps(self.instance, mode="ietf"))
# The JSON representation of the identityref must have a namespace, as
# the leaf `ref` and the identity `remote-id` are defined in two separate
# modules
self.assertEqual(
data["identityref:ietfint"]["ref"],
"remote-two:remote-id",
)

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
self.assertIn(
data["identityref:ak"]["address-type"],
["lcaf", "identityref:lcaf"],
)


if __name__ == "__main__":
unittest.main()