BUG-4638: handle StringTypeDefinition properly 78/29878/2
authorRobert Varga <rovarga@cisco.com>
Wed, 18 Nov 2015 18:10:05 +0000 (19:10 +0100)
committerRobert Varga <rovarga@cisco.com>
Wed, 18 Nov 2015 18:13:01 +0000 (19:13 +0100)
This patch adds the path for StringTypeDefinition, so we do not lose the
length restrictions. I do not believe their mapping is accurate, though.

Change-Id: If400f97f9440dab5e5f77cdc2c1eaddcdb2979f8
Signed-off-by: Robert Varga <rovarga@cisco.com>
opendaylight/restconf/sal-rest-docgen/src/main/java/org/opendaylight/netconf/sal/rest/doc/impl/ModelGenerator.java

index 166f7c8aaa8ce597003cbd265953524071edc38a..c0490c857bd726bb6e713f6d6ab477e55f5431a7 100644 (file)
@@ -471,19 +471,18 @@ public class ModelGenerator {
 
         if (leafTypeDef instanceof ExtendedType) {
             processExtendedType(leafTypeDef, property);
-        } else if (leafTypeDef instanceof EnumTypeDefinition) {
-            processEnumType((EnumTypeDefinition) leafTypeDef, property);
-
+        } else if (leafTypeDef instanceof BinaryTypeDefinition) {
+            processBinaryType((BinaryTypeDefinition) leafTypeDef, property);
         } else if (leafTypeDef instanceof BitsTypeDefinition) {
             processBitsType((BitsTypeDefinition) leafTypeDef, property);
-
-        } else if (leafTypeDef instanceof UnionTypeDefinition) {
-            processUnionType((UnionTypeDefinition) leafTypeDef, property);
-
+        } else if (leafTypeDef instanceof EnumTypeDefinition) {
+            processEnumType((EnumTypeDefinition) leafTypeDef, property);
         } else if (leafTypeDef instanceof IdentityrefTypeDefinition) {
             property.putOpt(TYPE_KEY, ((IdentityrefTypeDefinition) leafTypeDef).getIdentity().getQName().getLocalName());
-        } else if (leafTypeDef instanceof BinaryTypeDefinition) {
-            processBinaryType((BinaryTypeDefinition) leafTypeDef, property);
+        } else if (leafTypeDef instanceof StringTypeDefinition) {
+            processStringType((StringTypeDefinition) leafTypeDef, property);
+        } else if (leafTypeDef instanceof UnionTypeDefinition) {
+            processUnionType((UnionTypeDefinition) leafTypeDef, property);
         } else {
             String jsonType = jsonTypeFor(leafTypeDef);
             if (jsonType == null) {
@@ -561,6 +560,26 @@ public class ModelGenerator {
         property.put(ITEMS_KEY, itemsValue);
     }
 
+    private static void processStringType(final StringTypeDefinition stringType, final JSONObject property) throws JSONException {
+        StringTypeDefinition type = stringType;
+        List<LengthConstraint> lengthConstraints = stringType.getLengthConstraints();
+        while (lengthConstraints.isEmpty() && type.getBaseType() != null) {
+           type = type.getBaseType();
+           lengthConstraints = type.getLengthConstraints();
+        }
+
+        // FIXME: json-schema is not expressive enough to capture min/max laternatives. We should find the true minimum
+        //        and true maximum implied by the constraints and use that.
+        for (LengthConstraint lengthConstraint : lengthConstraints) {
+            Number min = lengthConstraint.getMin();
+            Number max = lengthConstraint.getMax();
+            property.putOpt(MIN_LENGTH_KEY, min);
+            property.putOpt(MAX_LENGTH_KEY, max);
+        }
+
+        property.put(TYPE_KEY, STRING);
+    }
+
     /**
      *
      * @param unionType