Skip to content

Commit d8549c8

Browse files
allena29rjarry
authored andcommitted
Instantiate ly_ctx_new with LY_CTX_NO_YANG_LIB
When instantiating a Context without a yang library loading data will trigger the following validation error *unless* the context is instantiated correctly *or* the validation flags `LYD_VALIDATE_NO_STATE` or `LYD_VALIDATE_PRESENT` are used. ``` 'Data location "/ietf-yang-library:yang-library".: Mandatory node "content-id" instance does not exist.' ```
1 parent bff1462 commit d8549c8

File tree

2 files changed

+41
-0
lines changed

2 files changed

+41
-0
lines changed

libyang/context.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ def __init__(
6161
search_path = ":".join(search_paths) if search_paths else None
6262

6363
if yanglib_path is None:
64+
options |= lib.LY_CTX_NO_YANGLIBRARY
6465
if lib.ly_ctx_new(str2c(search_path), options, ctx) != lib.LY_SUCCESS:
6566
raise self.error("cannot create context")
6667
else:

tests/test_data.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,46 @@ def test_data_parse_config_json(self):
7878
finally:
7979
dnode.free()
8080

81+
JSON_CONFIG_WITH_STATE = """{
82+
"yolo-system:state": {
83+
"speed": 4321
84+
},
85+
"yolo-system:conf": {
86+
"hostname": "foo",
87+
"url": [
88+
{
89+
"proto": "https",
90+
"host": "github.com",
91+
"path": "/CESNET/libyang-python",
92+
"enabled": false
93+
},
94+
{
95+
"proto": "http",
96+
"host": "foobar.com",
97+
"port": 8080,
98+
"path": "/index.html",
99+
"enabled": true
100+
}
101+
],
102+
"number": [
103+
1000,
104+
2000,
105+
3000
106+
],
107+
"speed": 1234
108+
}
109+
}
110+
"""
111+
112+
def test_data_parse_config_json_without_yang_lib(self):
113+
dnode = self.ctx.parse_data_mem(self.JSON_CONFIG, "json")
114+
self.assertIsInstance(dnode, DContainer)
115+
try:
116+
j = dnode.print_mem("json", with_siblings=True)
117+
self.assertEqual(j, self.JSON_CONFIG_WITH_STATE)
118+
finally:
119+
dnode.free()
120+
81121
JSON_CONFIG_ADD_LIST_ITEM = """{
82122
"yolo-system:conf": {
83123
"hostname": "foo",

0 commit comments

Comments
 (0)