Skip to content

when reads a union value before its member is resolved; XML and JSON give opposite verdicts #2607

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 t {
    type uint8;
  }
  leaf d {
    type union {
      type leafref {
        path "/a:t";
      }
      type string;
    }
  }
  leaf y {
    when "/a:d = '01'";
    type string;
  }
}

data.xml:

<d xmlns="urn:example:a">01</d>
<y xmlns="urn:example:a">z</y>

data.json, the same data:

{
  "a:d": "01",
  "a:y": "z"
}
$ yanglint -f json a.yang data.xml
libyang err : When condition "/a:d = '01'" not satisfied. (/a:y)
YANGLINT[E]: Failed to parse input data file "data.xml".
$ yanglint -f json a.yang data.json
{
  "a:d": "01",
  "a:y": "z"
}

With the condition changed to when "/a:d = '1'" the verdicts swap: XML is accepted and prints "a:d": "01", the value the condition did not match, while JSON is refused.

t does not exist, so the leafref member cannot match and d is the string "01" in both encodings. Without y, the XML document is accepted and d is printed as the string "01". The when on y nevertheless sees a different value in XML: "01" is lexically a valid uint8, so the value is first stored with the leafref member (canonical 1) and marked incomplete, and lyd_validate_unres() evaluates the node_when set before it resolves the node_types set. In JSON, "01" is a JSON string, the uint8 member refuses it at once and the string member is stored immediately.

Expected behaviour

The XML and JSON documents are valid, and give the same result. The condition reads the value of d, which is the string "01" because the leafref member does not match.

RFC 7950, section 9.12:

When interpreting an XML encoding, a value is validated consecutively against each member type, in the order they are specified in the "type" statement, until a match is found. The type that matched will be the type of the value for the node that was validated, and the encoding is interpreted according to the rules for that type.

Section 7.21.5 also requires a referenced node to be settled before a condition reads it ("If the XPath expression references any node that also has associated "when" statements, those "when" expressions MUST be evaluated first."); a value whose member is still provisional should likewise not be read as if it were final.

Why it matters

The same configuration is accepted in one encoding and refused in the other, and the condition is decided on a value that the node never has in the final tree.

Related

Activity

  1. added a commit that references this issue on Nov 5, 2026
    422f6af
  2. michalvasko commented on Nov 5, 2026

    @michalvasko
    Member

    This was a really complex use-case we did not account for so major changes were required but it should work now and be fairly robust, thanks.

  3. added
    is:bugBug description.
    status:completedFrom the developer perspective, the issue was solved (bug fixed, question answered,...)
    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: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