Repository navigation
Description
libyang version: devel at fab861b (project version 6.4.19). The same output is produced by 6.4.13 (f7e0cc1).
Reproducer
case.yang:
module case {
yang-version 1.1;
namespace "urn:example:case";
prefix k;
container c {
presence "test";
choice ch {
default s;
case s {
when "boolean(deref(v))";
leaf d {
type uint8;
default 3;
}
}
}
leaf v {
type leafref {
path "../d";
}
}
}
}case.xml:
<c xmlns="urn:example:case">
<v>3</v>
</c>$ yanglint -f json -d all case.yang case.xml
{
"case:c": {
"d": 3,
"v": 3
}
}
The condition of case s is true only because it finds d, a node added by case s itself.
The same schema with d added instead by uses g { when "boolean(deref(v))"; } (grouping g holding d) or by an augment of c with that when is refused at compilation with When condition cyclic dependency on the node "d"., so for those statements the evaluation is shown through a default union instead, which the compile-time check does not follow. uses-union.yang:
module uses-union {
yang-version 1.1;
namespace "urn:example:uses-union";
prefix u;
grouping g {
leaf d {
type uint8;
default 3;
}
}
container c {
presence "test";
uses g {
when "string(v) = '3'";
}
leaf v {
type union {
type leafref {
path "../d";
}
type string;
}
default "03";
}
}
}uses-union.xml is <c xmlns="urn:example:uses-union"/>.
$ yanglint -f json -d all uses-union.yang uses-union.xml
{
"uses-union:c": {
"d": 3,
"v": 3
}
}
v's default "03" takes the leafref member, canonical 3, only because d exists. The same output is produced when d is added by an augment of c with when "string(v) = '3'" instead of uses.
Expected behaviour
RFC 7950, section 7.21.5:
If the "when" statement is a child of a "uses", "choice", or "case" statement, then the context node is the closest ancestor node to the node with the "when" statement that is also a data node. If no such node exists, the context node is the root node. The accessible tree is tentatively altered during the processing of the XPath expression by removing all instances (if any) of the nodes added by the "uses", "choice", or "case" statement.
and for augment:
The accessible tree is tentatively altered during the processing of the XPath expression by removing all instances (if any) of the nodes added by the "augment" statement.
With d removed, deref(v) finds nothing in case.yang, so case s is not in use, d is not created and v has no target: case.xml is refused (section 9.9). In uses-union.yang the leafref member of v cannot match without d, so v is the string "03", the condition is false and the expected output is {"uses-union:c": {"v": "03"}}.
Because the RFC removes those nodes before evaluating the condition, the condition cannot depend on them; the compile-time "cyclic dependency" refusal of the uses and augment forms follows from evaluating with the nodes present.
Where it seems to come from
lyd_validate_node_when() in validation.c evaluates every condition, including those inherited from a case, choice, uses or augment (where when->context is the parent), on the unaltered data tree.
Why it matters
Whether a case, grouping or augmentation is active can depend on its own content, which the RFC rules out, so data are accepted or refused against the specified result.
Related
- ly2 vs. ly1: different XPath context evaluation in
uses#1710 (XPath context ofwheninuses), When evaluation from uses #2426 (evaluatingwhenfromuses), Cannot install module augmenting a uses statement with when condition #1779 (augmenting auseswithwhen). None of them covers the removal of the added nodes.
Activity
schema compile UPDATE check when and lref cyclic dependency
michalvasko commentedon Nov 5, 2026
I have fixed the first use-case so that the behavior is uniform and generates an error. I have similar forbidden the second use-case. The reason for this is that there is a cyclic dependency that may affect the data and that should be prevented. RFC does not forbid that.
I have also added a check for runtime cyclic validation dependencies but it only prints a warning in case some is found.