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 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
- PR union NEW store value for a specific member type index #2585 (store a union value for a specific member index; "value type does not change during validation").
- ietf-inet-types union usage (ordering inside unions) #2470 (member ordering inside unions), non-existent leafref inheriting error-message and error-app-tag #2359.
Activity
validation UPDATE validating unres independent of order
michalvasko commentedon Nov 5, 2026
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.