Skip to content

Default whose when reads another conditional default depends on declaration order #2606

Open

Description

@lamestllama

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

Activity

  1. added a commit that references this issue on Nov 4, 2026
    d0a9af5
  2. michalvasko commented on Nov 4, 2026

    @michalvasko
    Member

    Was a bit trickier to fix but should now work reliably.

  3. added
    is:bugBug description.
    status:completedFrom the developer perspective, the issue was solved (bug fixed, question answered,...)
    on Nov 4, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    is:bugBug description.status:completedFrom the developer perspective, the issue was solved (bug fixed, question answered,...)

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions