diff --git a/src/tree_schema.c b/src/tree_schema.c index f7dac079a..1dd9319d3 100644 --- a/src/tree_schema.c +++ b/src/tree_schema.c @@ -715,22 +715,32 @@ lysc_path_until(const struct lysc_node *node, const struct lysc_node *parent, LY } if ((pathtype == LYSC_PATH_DATA_PATTERN) && (iter->nodetype == LYS_LIST)) { + char *predicates = NULL; + key = NULL; while ((key = lys_getnext(key, iter, NULL, 0)) && lysc_is_key(key)) { - s = buffer ? strdup(buffer) : path; + s = predicates; /* print key predicate */ - if (buffer) { - len = snprintf(buffer, buflen, "%s[%s='%%s']", s ? s : "", key->name); - } else { - len = asprintf(&path, "%s[%s='%%s']", s ? s : "", key->name); + asprintf(&predicates, "%s[%s='%%s']", s ? s : "", key->name); + if (s) { + free(s); } - free(s); + } + s = buffer ? strdup(buffer) : path; + if (buffer) { + len = snprintf(buffer, buflen, "%s%s", predicates ? predicates : "", s ? s : ""); + } else { + len = asprintf(&path, "%s%s", predicates ? predicates : "", s ? s : ""); + } + if (predicates) { + free(predicates); + } + free(s); - if (buffer && (buflen <= (size_t)len)) { - /* not enough space in buffer */ - break; - } + if (buffer && (buflen <= (size_t)len)) { + /* not enough space in buffer */ + break; } } diff --git a/tests/utests/schema/test_schema.c b/tests/utests/schema/test_schema.c index 5cb5e4782..2dd7f6dc5 100644 --- a/tests/utests/schema/test_schema.c +++ b/tests/utests/schema/test_schema.c @@ -1873,6 +1873,7 @@ test_lysc_path(void **state) " leaf k {type string;}" " leaf l {type string;}" " leaf m {type string;}" + " leaf n {type string;}" " }" "}}", LYS_IN_YANG, NULL)); @@ -1880,6 +1881,10 @@ test_lysc_path(void **state) path = lysc_path(node, LYSC_PATH_DATA_PATTERN, NULL, 0); assert_string_equal(path, "/b:a/l[k='%s'][l='%s'][m='%s']"); free(path); + node = lys_find_path(UTEST_LYCTX, NULL, "/b:a/l/n", 0); + path = lysc_path(node, LYSC_PATH_DATA_PATTERN, NULL, 0); + assert_string_equal(path, "/b:a/l[k='%s'][l='%s'][m='%s']/n"); + free(path); } int