[MDSAL-405] Generated unions fail to enforce patterns Created: 14/Dec/18  Updated: 25/Jan/24

Status: In Progress
Project: mdsal
Component/s: Binding codegen
Affects Version/s: 3.0.2
Fix Version/s: 14.0.0

Type: Bug Priority: Medium
Reporter: Robert Varga Assignee: Ivan Hrasko
Resolution: Unresolved Votes: 1
Labels: pt
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Issue Links:
Relates
relates to MDSAL-361 Deal with restricted types in unions Confirmed
relates to MDSAL-502 mdsal source generator resolves "-" ... In Progress
relates to MDSAL-503 Wrongly generated java code using der... Confirmed

 Description   

The following snippet, taken from openconfig-bgp-policy.yang:

  typedef bgp-set-med-type {
    type union {
      type uint32;
      type string {
        pattern "^[+-][0-9]+";
      }
      type enumeration {
        enum IGP {
          description "set the MED value to the IGP cost toward the
          next hop for the route";
        }
      }
    }
    description
      "Type definition for specifying how the BGP MED can
      be set in BGP policy actions. The three choices are to set
      the MED directly, increment/decrement using +/- notation,
      and setting it to the IGP cost (predefined value).";
  }

results in the following code:

    public static final List<String> PATTERN_CONSTANTS = ImmutableList.of("^(?:\\^[+-][0-9]+)$");
    private static final Pattern patterns = Pattern.compile(PATTERN_CONSTANTS.get(0));
    private static final String regexes = "^[+-][0-9]+";
    private final Long _uint32;
    private final String _string;
    private final Enumeration _enumeration;


    private static void checkUint32Range(final long value) {
        if (value >= 0L && value <= 4294967295L) {
            return;
        }
        CodeHelpers.throwInvalidRange("[[0..4294967295]]", value);
    }
    public BgpSetMedType(Long _uint32) {
        super();
        checkUint32Range(_uint32);
        
        this._uint32 = _uint32;
        this._string = null;
        this._enumeration = null;
    }
    
    private static void check_stringLength(final String value) {
    }
    public BgpSetMedType(String _string) {
        super();
        check_stringLength(_string);
        
        this._string = _string;
        this._uint32 = null;
        this._enumeration = null;
    }

Note there is a useless check_stringLength() emitted, which is probably fine. What is not fine is that the String enforcement is completely missing, eventhough we have generated the appropriate regexs constant.



 Comments   
Comment by Robert Varga [ 14/Dec/18 ]

PATTERN_CONSTANTS needs to remain publicly-visible, though deprecated. We need to define a  new per-leaf constant schema, which will mimic what is being done, albeit in private. Furthermore PATTERN_CONSTANTS should not be compiled into enforcement patters.

Comment by Robert Varga [ 16/Feb/21 ]

This and related issues may be resolved as part of adoption of yangtools-7. We'll see when that effort is complete.

Comment by Robert Varga [ 18/Jan/24 ]

So this really needs to be addressed in one go with MDSAL-361 – there are just too many open cases we need to cover. See mdsal405.yang for detailed use cases.

Generated at Wed Feb 07 20:09:43 UTC 2024 using Jira 8.20.10#820010-sha1:ace47f9899e9ee25d7157d59aa17ab06aee30d3d.