Skip to content

Commit db08ce5

Browse files
committed
xpath BUGFIX ignore module for *
Test included. Fixes sysrepo/sysrepo#2601
1 parent 0ca400f commit db08ce5

File tree

2 files changed

+52
-31
lines changed

2 files changed

+52
-31
lines changed

src/xpath.c

Lines changed: 6 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -5419,7 +5419,6 @@ moveto_root(struct lyxp_set *set, uint32_t options)
54195419
* @brief Check @p node as a part of NameTest processing.
54205420
*
54215421
* @param[in] node Node to check.
5422-
* @param[in] ctx_node Context node.
54235422
* @param[in] set Set to read general context from.
54245423
* @param[in] node_name Node name in the dictionary to move to, NULL for any node.
54255424
* @param[in] moveto_mod Expected module of the node, NULL for no prefix.
@@ -5428,36 +5427,14 @@ moveto_root(struct lyxp_set *set, uint32_t options)
54285427
* LY_EINVAL if netither node nor any children match)
54295428
*/
54305429
static LY_ERR
5431-
moveto_node_check(const struct lyd_node *node, const struct lyd_node *ctx_node, const struct lyxp_set *set,
5432-
const char *node_name, const struct lys_module *moveto_mod, uint32_t options)
5430+
moveto_node_check(const struct lyd_node *node, const struct lyxp_set *set, const char *node_name,
5431+
const struct lys_module *moveto_mod, uint32_t options)
54335432
{
54345433
if (!node->schema) {
54355434
/* opaque node never matches */
54365435
return LY_ENOT;
54375436
}
54385437

5439-
if (!moveto_mod && (!node_name || strcmp(node_name, "*"))) {
5440-
switch (set->format) {
5441-
case LY_VALUE_SCHEMA:
5442-
case LY_VALUE_SCHEMA_RESOLVED:
5443-
/* use current module */
5444-
moveto_mod = set->cur_mod;
5445-
break;
5446-
case LY_VALUE_JSON:
5447-
case LY_VALUE_LYB:
5448-
/* inherit module of the context node, if any */
5449-
if (ctx_node) {
5450-
moveto_mod = ctx_node->schema->module;
5451-
}
5452-
break;
5453-
case LY_VALUE_CANON:
5454-
case LY_VALUE_XML:
5455-
/* not defined */
5456-
LOGINT(set->ctx);
5457-
return LY_EINVAL;
5458-
}
5459-
}
5460-
54615438
/* module check */
54625439
if (moveto_mod && (node->schema->module != moveto_mod)) {
54635440
return LY_ENOT;
@@ -5555,7 +5532,7 @@ static LY_ERR
55555532
moveto_node(struct lyxp_set *set, const struct lys_module *moveto_mod, const char *ncname, uint32_t options)
55565533
{
55575534
uint32_t i;
5558-
const struct lyd_node *siblings, *sub, *ctx_node;
5535+
const struct lyd_node *siblings, *sub;
55595536
LY_ERR rc;
55605537

55615538
if (options & LYXP_SKIP_EXPR) {
@@ -5574,16 +5551,14 @@ moveto_node(struct lyxp_set *set, const struct lys_module *moveto_mod, const cha
55745551
assert(!set->val.nodes[i].node);
55755552

55765553
/* search in all the trees */
5577-
ctx_node = NULL;
55785554
siblings = set->tree;
55795555
} else {
55805556
/* search in children */
5581-
ctx_node = set->val.nodes[i].node;
5582-
siblings = lyd_child(ctx_node);
5557+
siblings = lyd_child(set->val.nodes[i].node);
55835558
}
55845559

55855560
for (sub = siblings; sub; sub = sub->next) {
5586-
rc = moveto_node_check(sub, ctx_node, set, ncname, moveto_mod, options);
5561+
rc = moveto_node_check(sub, set, ncname, moveto_mod, options);
55875562
if (rc == LY_SUCCESS) {
55885563
if (!replaced) {
55895564
set_replace_node(set, sub, 0, LYXP_NODE_ELEM, i);
@@ -5831,7 +5806,7 @@ moveto_node_alldesc(struct lyxp_set *set, const struct lys_module *moveto_mod, c
58315806
/* TREE DFS */
58325807
start = set->val.nodes[i].node;
58335808
for (elem = next = start; elem; elem = next) {
5834-
rc = moveto_node_check(elem, start, set, ncname, moveto_mod, options);
5809+
rc = moveto_node_check(elem, set, ncname, moveto_mod, options);
58355810
if (!rc) {
58365811
/* add matching node into result set */
58375812
set_insert_node(&ret_set, elem, 0, LYXP_NODE_ELEM, ret_set.used);

tests/utests/basic/test_xpath.c

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -391,6 +391,51 @@ test_derived_from(void **state)
391391
lyd_free_all(tree);
392392
}
393393

394+
static void
395+
test_augment(void **state)
396+
{
397+
const char *schema_b =
398+
"module b {\n"
399+
" namespace urn:tests:b;\n"
400+
" prefix b;\n"
401+
" yang-version 1.1;\n"
402+
"\n"
403+
" import a {\n"
404+
" prefix a;\n"
405+
" }\n"
406+
"\n"
407+
" augment /a:c {\n"
408+
" leaf a {\n"
409+
" type uint16;\n"
410+
" }\n"
411+
" }\n"
412+
"}";
413+
const char *data =
414+
"<c xmlns=\"urn:tests:a\">\n"
415+
" <x>value</x>\n"
416+
" <ll>\n"
417+
" <a>key</a>\n"
418+
" </ll>\n"
419+
" <a xmlns=\"urn:tests:b\">25</a>\n"
420+
" <ll2>c1</ll2>\n"
421+
"</c>";
422+
struct lyd_node *tree;
423+
struct ly_set *set;
424+
425+
UTEST_ADD_MODULE(schema_b, LYS_IN_YANG, NULL, NULL);
426+
427+
assert_int_equal(LY_SUCCESS, lyd_parse_data_mem(UTEST_LYCTX, data, LYD_XML, LYD_PARSE_STRICT, LYD_VALIDATE_PRESENT, &tree));
428+
assert_non_null(tree);
429+
430+
/* get all children ignoring their module */
431+
assert_int_equal(LY_SUCCESS, lyd_find_xpath(tree, "/a:c/*", &set));
432+
assert_int_equal(4, set->count);
433+
434+
ly_set_free(set, NULL);
435+
436+
lyd_free_all(tree);
437+
}
438+
394439
int
395440
main(void)
396441
{
@@ -401,6 +446,7 @@ main(void)
401446
UTEST(test_atomize, setup),
402447
UTEST(test_canonize, setup),
403448
UTEST(test_derived_from, setup),
449+
UTEST(test_augment, setup),
404450
};
405451

406452
return cmocka_run_group_tests(tests, NULL, NULL);

0 commit comments

Comments
 (0)