Skip to content

Circular when dependency through a union's leafref member is accepted, and its result depends on declaration order #2615

Open

Description

@lamestllama

libyang version: devel at fab861b (project version 6.4.19). The same output is produced by 6.4.13 (f7e0cc1).

libyang refuses a circular dependency among when expressions at compilation, including one that passes through deref(). deref.yang:

module deref {
  yang-version 1.1;
  namespace "urn:example:deref";
  prefix r;

  leaf e {
    when "boolean(deref(/r:v))";
    type string;
    default "x";
  }
  leaf d {
    when "/r:e";
    type uint8;
    default 3;
  }
  leaf v {
    type leafref {
      path "/r:d";
    }
  }
}
$ yanglint deref.yang
libyang err : When condition cyclic dependency on the node "d". (/deref:e)

The same cycle is accepted when it passes through the value of a union with a leafref member.

Reproducer

a.yang:

module a {
  yang-version 1.1;
  namespace "urn:example:a";
  prefix a;

  leaf e {
    when "string(/a:v) = '3'";
    type string;
    default "x";
  }
  leaf d {
    when "/a:e";
    type uint8;
    default 3;
  }
  leaf v {
    type union {
      type leafref {
        path "/a:d";
      }
      type string;
    }
    default "03";
  }
}

empty.json is {}.

$ yanglint -f json -d all a.yang empty.json
{
  "a:e": "x",
  "a:d": 3,
  "a:v": 3
}

The condition of e reads v. The value of v is the leafref 3 if d exists and the string "03" otherwise, so it depends on the condition of d, which reads e. Both "all three exist" and "only v exists" satisfy every condition; libyang accepts the schema and picks the first.

When no state satisfies the conditions, the result depends on declaration order. b.yang is a.yang with the condition of e changed to string(/b:v) = '03':

module b {
  yang-version 1.1;
  namespace "urn:example:b";
  prefix b;

  leaf e {
    when "string(/b:v) = '03'";
    type string;
    default "x";
  }
  leaf d {
    when "/b:e";
    type uint8;
    default 3;
  }
  leaf v {
    type union {
      type leafref {
        path "/b:d";
      }
      type string;
    }
    default "03";
  }
}
$ yanglint -f json -d all b.yang empty.json
{
  "b:d": 3,
  "b:v": 3
}

With d declared before e (reversed/b.yang, otherwise identical):

$ yanglint -f json -d all reversed/b.yang empty.json
{
  "b:v": "03"
}

Neither result satisfies the conditions: in the first, d exists although e, which its condition reads, does not; in the second, e is missing although its condition, string(/b:v) = '03', is true.

Expected behaviour

Both a.yang and b.yang are refused at compilation, as deref.yang is. 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. There MUST NOT be any circular dependencies among "when" expressions.

The condition of e cannot be evaluated before the condition of d, since the value it reads is decided by whether d exists, and the condition of d cannot be evaluated before that of e. There is no order that satisfies the first sentence, which is what the second sentence rules out.

Where it seems to come from

lys_compile_unres_when_cyclic() in schema_compile.c follows only the nodes that have a when of their own (if ((xp_scnode->type != LYXP_NODE_ELEM) || !lysc_node_when(xp_scnode->scnode)), "no when to check"). v has no when, so the check does not see that its value depends on the existence of d through the leafref member of its union. deref() is caught because xpath_deref() adds the leafref target d itself to the schema set.

At run time, the defaults are created with LYD_WHEN_TRUE already set (the cause described in #2606), so the order of the node_when set appears to decide which condition reads which state.

Why it matters

A schema with circular conditions, which the RFC forbids, is accepted, and the data it produces depend on the order in which leaves are declared, including results that violate the schema's own conditions.

Related

Activity

  1. michalvasko commented on Nov 5, 2026

    @michalvasko
    Member

    Fixed by one of the previous fixes.

  2. added
    is:bug
    status:completed
    on Nov 5, 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:bugstatus:completed

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions