Set default for number type 80/105880/30
authortobias.pobocik <tobias.pobocik@pantheon.tech>
Wed, 10 May 2023 11:23:35 +0000 (13:23 +0200)
committerIvan Hrasko <ivan.hrasko@pantheon.tech>
Tue, 22 Aug 2023 07:46:06 +0000 (07:46 +0000)
We should distinguish between "default" and "example" OpenAPI
property.

Set "default" with "example" since it was misused.

JIRA: NETCONF-999
Change-Id: I2fbb6ab7643c6028219f38da3e07d2e67a8f3049
Signed-off-by: tobias.pobocik <tobias.pobocik@pantheon.tech>
restconf/restconf-openapi/src/main/java/org/opendaylight/restconf/openapi/impl/DefinitionGenerator.java

index c7ede1440f0fee54918b04b040090318598c02c9..fa023a07190ff8914c0a0e41cda64b24aa9b3122 100644 (file)
@@ -21,6 +21,7 @@ import com.google.common.collect.RangeSet;
 import dk.brics.automaton.RegExp;
 import java.io.IOException;
 import java.math.BigDecimal;
+import java.math.BigInteger;
 import java.util.Collection;
 import java.util.HashMap;
 import java.util.List;
@@ -763,6 +764,8 @@ public final class DefinitionGenerator {
         }
 
         if (leafTypeDef instanceof DecimalTypeDefinition) {
+            leafTypeDef.getDefaultValue().ifPresent(number ->
+                    setDefaultValue(property, Decimal64.valueOf((String) number).decimalValue()));
             maybeLower.ifPresent(number -> setExampleValue(property, ((Decimal64) number).decimalValue()));
             return NUMBER_TYPE;
         }
@@ -773,14 +776,20 @@ public final class DefinitionGenerator {
                 || leafTypeDef instanceof Int32TypeDefinition) {
 
             property.put(FORMAT_KEY, INT32_FORMAT);
+            leafTypeDef.getDefaultValue().ifPresent(number -> setDefaultValue(property,
+                Integer.valueOf((String) number)));
             maybeLower.ifPresent(number -> setExampleValue(property, Integer.valueOf(number.toString())));
         } else if (leafTypeDef instanceof Uint32TypeDefinition
                 || leafTypeDef instanceof Int64TypeDefinition) {
 
             property.put(FORMAT_KEY, INT64_FORMAT);
+            leafTypeDef.getDefaultValue().ifPresent(number -> setDefaultValue(property,
+                Long.valueOf((String) number)));
             maybeLower.ifPresent(number -> setExampleValue(property, Long.valueOf(number.toString())));
         } else {
             //uint64
+            leafTypeDef.getDefaultValue().ifPresent(number -> setDefaultValue(property,
+                new BigInteger((String) number)));
             setExampleValue(property, 0);
         }
         return INTEGER_TYPE;
@@ -842,10 +851,18 @@ public final class DefinitionGenerator {
         property.put(DEFAULT_KEY, value);
     }
 
+    private static void setDefaultValue(final ObjectNode property, final Integer value) {
+        property.put(DEFAULT_KEY, value);
+    }
+
     private static void setDefaultValue(final ObjectNode property, final Long value) {
         property.put(DEFAULT_KEY, value);
     }
 
+    private static void setDefaultValue(final ObjectNode property, final BigInteger value) {
+        property.put(DEFAULT_KEY, value);
+    }
+
     private static void setDefaultValue(final ObjectNode property, final BigDecimal value) {
         property.put(DEFAULT_KEY, value);
     }