final PatternConstraint pattern = type.getPatternConstraints().iterator().next();
String regex = pattern.getJavaPatternString();
regex = regex.substring(1, regex.length() - 1);
- final Generex generex = new Generex(regex);
- setDefaultValue(property, generex.random());
+ String defaultValue = "";
+ try {
+ final Generex generex = new Generex(regex);
+ defaultValue = generex.random();
+ } catch (IllegalArgumentException ex) {
+ LOG.warn("Cannot create example string for type: {} with regex: {}.", stringType.getQName(), regex);
+ }
+ setDefaultValue(property, defaultValue);
} else {
setDefaultValue(property, "Some " + nodeName);
}
}
}
+ @Test
+ public void testStringTypes() throws Exception {
+ Preconditions.checkArgument(this.helper.getModules() != null, "No modules found");
+ Module strTypes = this.helper.getModules().stream()
+ .filter(module -> module.getName().equals("string-types"))
+ .findFirst()
+ .orElseThrow(() -> new IllegalArgumentException("String types module not found"));
+
+ final DefinitionGenerator generator = new DefinitionGenerator();
+ final ObjectNode jsonObject = generator.convertToJsonSchema(strTypes, this.schemaContext, new DefinitionNames(),
+ ApiDocServiceImpl.OAversion.V2_0, true);
+
+ Assert.assertNotNull(jsonObject);
+ }
}
--- /dev/null
+module string-types {
+ yang-version 1;
+ namespace "urn:ietf:params:xml:ns:yang:test:string:types";
+ prefix "str-tp";
+
+ typedef DisplayString {
+ type string {
+ length "0..255";
+ pattern "((\p{IsBasicLatin}{0,255}))";
+ }
+ }
+
+ typedef PhysAddress {
+ type string {
+ pattern "((([0-9A-Fa-f]{2}){1}):)";
+ }
+ }
+
+ typedef MacAddress {
+ type string {
+ length "6";
+ pattern "((([0-9A-Fa-f]{2}){1}):)";
+ }
+ }
+
+ typedef DateAndTime {
+ type string {
+ length "8|11";
+ pattern "((0|[1-9](([0-9]){0,4}))-(0|[1-9](([0-9]){0,2}))-(0|[1-9](([0-9]){0,2})),(0|[1-9](([0-9]){0,2})):(0|[1-9](([0-9]){0,2})):(0|[1-9](([0-9]){0,2})).(0|[1-9](([0-9]){0,2})),((\p{IsBasicLatin}{1})(0|[1-9](([0-9]){0,2})):(0|[1-9](([0-9]){0,2})))?)";
+ }
+ }
+
+ typedef Arrows {
+ type string {
+ length "10";
+ pattern "((\p{IsArrows}{5,255}))";
+ }
+ }
+
+ typedef Thai {
+ type string {
+ length "10";
+ pattern "((\p{IsThai}{8,255}))";
+ }
+ }
+
+ typedef BraillePatterns {
+ type string {
+ length "10";
+ pattern "((\p{IsBraillePatterns}{4,255}))";
+ }
+ }
+
+ typedef MathematicalOperators {
+ type string {
+ length "10";
+ pattern "((\p{IsMathematicalOperators}{4,255}))";
+ }
+ }
+
+ container test {
+ description "Tests various combinations of regex expressions found in snmp yang models,
+ which are causing problems because of isBasicLatin expression.
+
+ According to https://unicode.org/charts/PDF/U0000.pdf basic latin characters are in range
+ 0x00-0x7F ([\x00-\xFF] or [\u0000-\u00FF]). This means it should be safe to replace isBasicLatin
+ in regex expressions for characters in this range.";
+
+ leaf display-string {
+ type DisplayString;
+ }
+ leaf phys-address {
+ type PhysAddress;
+ }
+ leaf mac-address {
+ type MacAddress;
+ }
+ leaf date-and-time {
+ type DateAndTime;
+ }
+ leaf arrows {
+ type Arrows;
+ }
+ leaf thai {
+ type Thai;
+ }
+ leaf braille-patterns {
+ type BraillePatterns;
+ }
+ leaf mathematical-operators {
+ type MathematicalOperators;
+ }
+ }
+}
\ No newline at end of file