Improve union type processing 34/107334/4
authorIvan Hrasko <ivan.hrasko@pantheon.tech>
Tue, 8 Aug 2023 09:02:43 +0000 (11:02 +0200)
committerIvan Hrasko <ivan.hrasko@pantheon.tech>
Tue, 15 Aug 2023 14:08:50 +0000 (14:08 +0000)
Instead of introducing cumbersome logic for union processing,
choose first one.

JIRA: NETCONF-1124
Change-Id: I6f79cfc0edafd9cf3cdd25dff4a751b69ad15953
Signed-off-by: Ivan Hrasko <ivan.hrasko@pantheon.tech>
Signed-off-by: Matej Sramcik <matej.sramcik@pantheon.tech>
restconf/restconf-openapi/src/main/java/org/opendaylight/restconf/openapi/impl/DefinitionGenerator.java

index ba21d469763ee3a8ed0ddbb94f68a47e6068ca84..465e65d54e7c6fa85f5e788d34a96b55167d1dee 100644 (file)
@@ -599,7 +599,8 @@ public final class DefinitionGenerator {
         } else if (leafTypeDef instanceof StringTypeDefinition stringType) {
             jsonType = processStringType(stringType, property, node.getQName().getLocalName());
         } else if (leafTypeDef instanceof UnionTypeDefinition unionType) {
-            jsonType = processUnionType(unionType, property, node.getQName().getLocalName());
+            jsonType = processTypeDef(unionType.getTypes().iterator().next(), node, property, stack, definitions,
+                definitionNames);
         } else if (leafTypeDef instanceof EmptyTypeDefinition) {
             jsonType = OBJECT_TYPE;
         } else if (leafTypeDef instanceof LeafrefTypeDefinition leafrefType) {
@@ -825,50 +826,6 @@ public final class DefinitionGenerator {
         return STRING_TYPE;
     }
 
-    private static String processUnionType(final UnionTypeDefinition unionType, final ObjectNode property,
-            final String nodeName) {
-        boolean isStringTakePlace = false;
-        boolean isNumberTakePlace = false;
-        boolean isBooleanTakePlace = false;
-        for (final TypeDefinition<?> typeDef : unionType.getTypes()) {
-            if (!isStringTakePlace) {
-                if (typeDef instanceof StringTypeDefinition
-                        || typeDef instanceof BitsTypeDefinition
-                        || typeDef instanceof BinaryTypeDefinition
-                        || typeDef instanceof IdentityrefTypeDefinition
-                        || typeDef instanceof EnumTypeDefinition
-                        || typeDef instanceof LeafrefTypeDefinition
-                        || typeDef instanceof UnionTypeDefinition) {
-                    isStringTakePlace = true;
-                } else if (!isNumberTakePlace && typeDef instanceof RangeRestrictedTypeDefinition) {
-                    isNumberTakePlace = true;
-                } else if (!isBooleanTakePlace && typeDef instanceof BooleanTypeDefinition) {
-                    isBooleanTakePlace = true;
-                }
-            }
-        }
-        if (isStringTakePlace) {
-            unionType.getDefaultValue().ifPresent(v -> setDefaultValue(property, (String) v));
-            setExampleValue(property, "Some " + nodeName);
-            return STRING_TYPE;
-        }
-        if (isBooleanTakePlace) {
-            if (isNumberTakePlace) {
-                // FIXME deal with other number formats
-                unionType.getDefaultValue().ifPresent(v -> setDefaultValue(property, Long.valueOf((String) v)));
-                setExampleValue(property, 0);
-                return NUMBER_TYPE;
-            }
-            unionType.getDefaultValue().ifPresent(v -> setDefaultValue(property, Boolean.valueOf((String) v)));
-            setExampleValue(property, true);
-            return BOOLEAN_TYPE;
-        }
-        // FIXME deal with other number formats
-        unionType.getDefaultValue().ifPresent(v -> setDefaultValue(property, Long.valueOf((String) v)));
-        setExampleValue(property, 0);
-        return NUMBER_TYPE;
-    }
-
     private static ObjectNode buildXmlParameter(final SchemaNode node) {
         final ObjectNode xml = JsonNodeFactory.instance.objectNode();
         final QName qName = node.getQName();