[NETCONF-1123] Union type does not work for number and boolean format Created: 08/Aug/23  Updated: 15/Aug/23  Resolved: 15/Aug/23

Status: Resolved
Project: netconf
Component/s: restconf-openapi
Affects Version/s: None
Fix Version/s: None

Type: Bug Priority: Medium
Reporter: Tobias Pobocik Assignee: Ivan Hrasko
Resolution: Won't Do Votes: 0
Labels: pt
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified


 Description   

Steps to reproduce:

1. on netconf master branch, add this model into restconf/restconf-openapi/src/test/resources/yang/ and name it definition-test.yang

module definition-test {
  namespace "urn:definition-test";
  prefix sample;

  typedef topology-ref {
    type instance-identifier;
  }

  container binary-container {
    leaf binary-data {
      type binary;
      default "SGVsbG8gdGVzdCE=";
    }
  }

  container union-container {
    leaf testUnion1 {
      type union {
        type int32;
        type string;
      }
      default 5;
    }

    leaf testUnion2 {
      type union {
        type string;
        type boolean;
      }
      default false;
    }

    leaf testUnion3 {
      type union {
        type int32;
        type boolean;
      }
      default false;
    }

    leaf testBoolean {
      type boolean;
      default true;
    }
  }

  container number-container {
    leaf testInteger {
      type int32;
      default 42;
    }

    leaf testUnsignedInteger {
      type uint16;
      default 100;
    }

    leaf testDecimal {
      type decimal64 {
        fraction-digits 2;
      }
      default 3.14;
    }

    leaf testDouble {
      type decimal64 {
        fraction-digits 11;
      }
      default 3.14159265359;
    }

    leaf testInt64 {
      type int64;
      default 42;
    }

    leaf testUint64 {
      type uint64;
      default 42;
    }
  }

  container enum-container {
    leaf status {
      type enumeration {
        enum up {
          value 1;
        }
        enum down {
          value 2;
        }
      }
      default "up";
    }
  }

  container network-container {
    leaf network-ref {
      type topology-ref;
      default "/network/nodes[node-id='node1']";
    }
  }
}
 

2.
Add this test into DefinitionGeneratorTest.java

@Test
public void testUnionTypes() throws IOException {
    final var module = context.findModule("definition-test").orElseThrow();
    final var generator = new DefinitionGenerator();
    final var jsonObject = generator.convertToSchemas(module, context, new DefinitionNames(), true);
    assertNotNull(jsonObject);

    final var properties = jsonObject.get("definition-test_config_union-container").properties();
    assertEquals("5", properties.get("testUnion1").get("default").asText());
    assertEquals("string", properties.get("testUnion1").get("type").asText());
    assertEquals("Some testUnion1", properties.get("testUnion1").get("example").asText());
    assertEquals("false", properties.get("testUnion2").get("default").asText());
    assertEquals("string", properties.get("testUnion2").get("type").asText());
    assertEquals("Some testUnion2", properties.get("testUnion2").get("example").asText());
    assertEquals("number", properties.get("testUnion3").get("type").asText());
    assertEquals("0", properties.get("testUnion3").get("example").asText());
    assertEquals("false", properties.get("testUnion3").get("default").asText());
}
 

Run the test. This will cause an exception to be thrown inside:
https://git.opendaylight.org/gerrit/c/netconf/+/105875/24/restconf/restconf-openapi/src/main/java/org/opendaylight/restconf/openapi/impl/DefinitionGenerator.java#876

The exception:

java.lang.NumberFormatException: For input string: "false"
	at java.base/java.lang.NumberFormatException.forInputString(NumberFormatException.java:67)	at java.base/java.lang.Long.parseLong(Long.java:711)	at java.base/java.lang.Long.valueOf(Long.java:1159)	at org.opendaylight.restconf.openapi.impl.DefinitionGenerator.lambda$processUnionType$17(DefinitionGenerator.java:889)	at java.base/java.util.Optional.ifPresent(Optional.java:178)	at org.opendaylight.restconf.openapi.impl.DefinitionGenerator.processUnionType(DefinitionGenerator.java:889)	at org.opendaylight.restconf.openapi.impl.DefinitionGenerator.processTypeDef(DefinitionGenerator.java:625)	at org.opendaylight.restconf.openapi.impl.DefinitionGenerator.processLeafNode(DefinitionGenerator.java:564)	at org.opendaylight.restconf.openapi.impl.DefinitionGenerator.processChildNode(DefinitionGenerator.java:492)	at org.opendaylight.restconf.openapi.impl.DefinitionGenerator.processChildren(DefinitionGenerator.java:442)	at org.opendaylight.restconf.openapi.impl.DefinitionGenerator.processDataNodeContainer(DefinitionGenerator.java:403)	at org.opendaylight.restconf.openapi.impl.DefinitionGenerator.processContainersAndLists(DefinitionGenerator.java:263)	at org.opendaylight.restconf.openapi.impl.DefinitionGenerator.convertToSchemas(DefinitionGenerator.java:150)	at org.opendaylight.restconf.openapi.impl.DefinitionGenerator.convertToSchemas(DefinitionGenerator.java:167) 

And the place inside yang model which causes this is here:

leaf testUnion3 {
  type union {
    type int32;
    type boolean;
  }
  default false;
} 


 Comments   
Comment by Ivan Hrasko [ 15/Aug/23 ]

The logic will be completely removed/reworked in NETCONF-1124.

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