Repository navigation
Description
libyang version: devel at fab861b (project version 6.4.19). The same output is produced by 6.4.13 (f7e0cc1).
Reproducer
a.yang:
module a {
yang-version 1.1;
namespace "urn:example:a";
prefix a;
leaf c {
when "false()";
type string;
default "on";
}
leaf b {
when "/a:c = 'on'";
type string;
default "on";
}
}empty.json:
{}$ yanglint -f json -d all a.yang empty.json
{
"a:b": "on"
}
With the two leaves declared in the opposite order (b first, then c), the output is empty:
$ yanglint -f json -d all reversed/a.yang empty.json
{
}
c is never in use, since its condition is false(). The condition of b reads c, so b cannot be in use either, yet b is created with the first declaration order. Inside a container the same schema with a third leaf leaf test { type string; must "not(../b)"; } refuses <test>x</test> with Must condition "not(../b)" not satisfied. and accepts it in the reversed order.
Expected behaviour
The result does not depend on declaration order: c is evaluated first, its condition is false, so b's condition is false and neither default is in use.
RFC 7950, section 7.21.5:
If the XPath expression references any node that also has associated "when" statements, those "when" expressions MUST be evaluated first.
Section 7.6.1:
Note that if the leaf or any of its ancestors has a "when" condition or "if-feature" expression that evaluates to "false", then the default value is not in use.
Where it seems to come from
lyd_new_implicit_r() in tree_data_new.c creates default nodes with LYD_WHEN_TRUE already set. moveto_node_check() in xpath.c returns LY_EINCOMPLETE only for a node with neither LYD_WHEN_TRUE nor LYD_WHEN_FALSE, so when b's condition is evaluated before c's, c is read as existing instead of the evaluation being deferred. The order of the node_when set then decides the outcome.
Why it matters
Reordering leaves in a module, which should have no effect on data, changes which defaults exist and therefore whether data are valid.
Related
- Internal error when
whendepends on a node whose ownwhenevaluated to false #2516 and PR Fix spurious cascading errors on when-false nodes in multi-error validation #2528 (when depending on a node whose own when is false;LYD_WHEN_FALSEin multi-error validation). - Implicit defaults without validation #2437 (implicit defaults and when evaluation), open.
Activity
validation BUGFIX order-independent when evaluation
michalvasko commentedon Nov 4, 2026
Was a bit trickier to fix but should now work reliably.