Skip to content

Conversation

@steweg
Copy link
Contributor

@steweg steweg commented Sep 28, 2025

This patch introduces ability to use union types, which internally uses leafrefs to be used as deref target nodes. This fullfils the last statement of RFC 7950 section 10.3.1. This behavior is also allowed within leafref paths which uses deref function in it.

This patch also fixes segfault in case of using cyclic deref paths

@steweg
Copy link
Contributor Author

steweg commented Sep 28, 2025

Just some comments on current behavior of libyang. Current version doesn't allows to deref any type of node, just the valid ones, which contradicts the RFC 7950, which says that it should support it but return empty set.

I understand that RFC7950 doesn't explicitly allows usage of unions as deref target nodes, but it make sense in case when you have scenario when yang model allows usage of predefined set of data and refer to it using leafref, and also allows to use completely custom set, using the same kind data model as for predefined ones... on top of that there might be some data which is never predefined and therefore it can have two possible paths to reference some data. Either through predefined set or through custom. Here is a yang model to better explain this:

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

    grouping common-struct {
        leaf k {type string;}
        leaf v1 {type string;}
        // ...
        leaf vX {type string;}
    }

    list predefined_data {key k; uses common-struct;}

    rpc create_action {
        input {
            leaf predefined {
                type union {
                    type leafref {path "/predefined_data/k";}
                    type string {pattern "custom";}
                }
                mandatory true;
            }
            container custom_data {
                uses common-struct;
                when "../predefined = 'custom'";
            }
            leaf additional_selected_data {
                type union {
                    type leafref {path "../custom_data/vX";}
                    type leafref {path "deref(../predefined)/../vX";}
                }
                mandatory true;
            }
        }
    }
}

I know that I have introduced union of leafref and string together, which could be just "optional" leafref. In my case this is not possible as empty value would automatically be consider by UI layer in my project that user wishes to create custom data, which quite a lot of data. This creates user experience to see what all details are needed if he/she wants to defined it manually, and should rather use predefined set. Once he/she changed that UI will hide not needed staff leaving just the additional selected data field to be filled in by user. So we have to introduce something like string literal value, which is not supported by YANG natively, therefore this trick with string with pattern and need to use deref with union

@steweg steweg force-pushed the feature/deref_union branch from bd85a76 to 578357b Compare September 28, 2025 18:35
Copy link
Member

@michalvasko michalvasko left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, seems fine, just some minor issues.

This patch introduces ability to use union types, which internally uses
leafrefs to be used as deref target nodes. This fullfils the last
statement of RFC 7950 section 10.3.1. This behavior is also allowed
within leafref paths which uses deref function in it.

This patch also fixes segfault in case of using cyclic deref paths
@steweg steweg force-pushed the feature/deref_union branch from 578357b to 6afbaad Compare September 29, 2025 08:55
Copy link
Member

@michalvasko michalvasko left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be fine, thanks.

@michalvasko michalvasko merged commit f553dd4 into CESNET:devel Sep 30, 2025
11 checks passed
@steweg steweg deleted the feature/deref_union branch September 30, 2025 11:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants