Change default keyword to example 46/106546/6
authortobias.pobocik <tobias.pobocik@pantheon.tech>
Tue, 20 Jun 2023 08:43:43 +0000 (10:43 +0200)
committerIvan Hrasko <ivan.hrasko@pantheon.tech>
Fri, 7 Jul 2023 14:40:59 +0000 (14:40 +0000)
In many cases we misuse default swagger property to set an example
value. Instead, use example property as specified in:
https://swagger.io/docs/specification/adding-examples/

Also, adjust the existing tests to match the changes.

JIRA: NETCONF-999
Signed-off-by: tobias.pobocik <tobias.pobocik@pantheon.tech>
Change-Id: Ic1d168b119e4b2226b1fa46c0bf86629ebd990d1

restconf/restconf-openapi/src/main/java/org/opendaylight/restconf/openapi/impl/DefinitionGenerator.java
restconf/restconf-openapi/src/test/java/org/opendaylight/restconf/openapi/impl/DefinitionGeneratorTest.java

index 543fcd7e68d8be767bd3f41cfc5c30dd71557b89..2c1cc366ff837fbb528eecbed6bee4ade0f375fd 100644 (file)
@@ -575,7 +575,7 @@ public class DefinitionGenerator {
         property.put(DESCRIPTION_KEY, leafDescription);
 
         final String localName = leafNode.getQName().getLocalName();
-        setDefaultValue(property, String.format("<%s> ... </%s>", localName, localName));
+        setExampleValue(property, String.format("<%s> ... </%s>", localName, localName));
         property.put(TYPE_KEY, STRING_TYPE);
         property.set(XML_KEY, buildXmlParameter(leafNode));
         processMandatory(leafNode, name, required);
@@ -592,7 +592,7 @@ public class DefinitionGenerator {
         property.put(DESCRIPTION_KEY, leafDescription);
 
         final String localName = leafNode.getQName().getLocalName();
-        setDefaultValue(property, String.format("<%s> ... </%s>", localName, localName));
+        setExampleValue(property, String.format("<%s> ... </%s>", localName, localName));
         property.put(TYPE_KEY, STRING_TYPE);
         property.set(XML_KEY, buildXmlParameter(leafNode));
         processMandatory(leafNode, name, required);
@@ -631,7 +631,7 @@ public class DefinitionGenerator {
                 stack, definitions, definitionNames);
         } else if (leafTypeDef instanceof BooleanTypeDefinition) {
             jsonType = BOOLEAN_TYPE;
-            setDefaultValue(property, true);
+            setExampleValue(property, true);
         } else if (leafTypeDef instanceof RangeRestrictedTypeDefinition) {
             jsonType = processNumberType((RangeRestrictedTypeDefinition<?, ?>) leafTypeDef, property);
         } else if (leafTypeDef instanceof InstanceIdentifierTypeDefinition) {
@@ -780,9 +780,9 @@ public class DefinitionGenerator {
             } catch (IllegalArgumentException ex) {
                 LOG.warn("Cannot create example string for type: {} with regex: {}.", stringType.getQName(), regex);
             }
-            setDefaultValue(property, defaultValue);
+            setExampleValue(property, defaultValue);
         } else {
-            setDefaultValue(property, "Some " + nodeName);
+            setExampleValue(property, "Some " + nodeName);
         }
         return STRING_TYPE;
     }
@@ -797,7 +797,7 @@ public class DefinitionGenerator {
         }
 
         if (leafTypeDef instanceof DecimalTypeDefinition) {
-            maybeLower.ifPresent(number -> setDefaultValue(property, ((Decimal64) number).decimalValue()));
+            maybeLower.ifPresent(number -> setExampleValue(property, ((Decimal64) number).decimalValue()));
             return NUMBER_TYPE;
         }
         if (leafTypeDef instanceof Uint8TypeDefinition
@@ -807,15 +807,15 @@ public class DefinitionGenerator {
                 || leafTypeDef instanceof Int32TypeDefinition) {
 
             property.put(FORMAT_KEY, INT32_FORMAT);
-            maybeLower.ifPresent(number -> setDefaultValue(property, Integer.valueOf(number.toString())));
+            maybeLower.ifPresent(number -> setExampleValue(property, Integer.valueOf(number.toString())));
         } else if (leafTypeDef instanceof Uint32TypeDefinition
                 || leafTypeDef instanceof Int64TypeDefinition) {
 
             property.put(FORMAT_KEY, INT64_FORMAT);
-            maybeLower.ifPresent(number -> setDefaultValue(property, Long.valueOf(number.toString())));
+            maybeLower.ifPresent(number -> setExampleValue(property, Long.valueOf(number.toString())));
         } else {
             //uint64
-            setDefaultValue(property, 0);
+            setExampleValue(property, 0);
         }
         return INTEGER_TYPE;
     }
@@ -837,7 +837,7 @@ public class DefinitionGenerator {
             final var container = module.orElseThrow().getChildNodes().stream()
                     .filter(n -> n instanceof ContainerSchemaNode)
                     .findFirst();
-            container.ifPresent(c -> setDefaultValue(property, String.format("/%s:%s", module.orElseThrow().getPrefix(),
+            container.ifPresent(c -> setExampleValue(property, String.format("/%s:%s", module.orElseThrow().getPrefix(),
                     c.getQName().getLocalName())));
         }
 
@@ -911,11 +911,23 @@ public class DefinitionGenerator {
         property.put(EXAMPLE_KEY, value);
     }
 
-    private static void setDefaultValue(final ObjectNode property, final String value) {
-        property.put(DEFAULT_KEY, value);
+    private static void setExampleValue(final ObjectNode property, final Integer value) {
+        property.put(EXAMPLE_KEY, value);
     }
 
-    private static void setDefaultValue(final ObjectNode property, final Integer value) {
+    private static void setExampleValue(final ObjectNode property, final Long value) {
+        property.put(EXAMPLE_KEY, value);
+    }
+
+    private static void setExampleValue(final ObjectNode property, final BigDecimal value) {
+        property.put(EXAMPLE_KEY, value);
+    }
+
+    private static void setExampleValue(final ObjectNode property, final Boolean value) {
+        property.put(EXAMPLE_KEY, value);
+    }
+
+    private static void setDefaultValue(final ObjectNode property, final String value) {
         property.put(DEFAULT_KEY, value);
     }
 
index 2f3fd96a966bc454209c5e4db65870558ee44788..d9eda64149b089bf67a238a985a95482658277cf 100644 (file)
@@ -63,8 +63,8 @@ public final class DefinitionGeneratorTest {
         assertNotNull(jsonObject);
 
         var properties = jsonObject.get("strings-from-regex_test").properties();
-        assertEquals("00:00:00:00:00:00", properties.get("mac-address").get("default").asText());
-        assertEquals("0000-00-00T00:00:00Z", properties.get("login-date-time").get("default").asText());
-        assertEquals("0.0.0.0", properties.get("ipv4-address").get("default").asText());
+        assertEquals("00:00:00:00:00:00", properties.get("mac-address").get("example").asText());
+        assertEquals("0000-00-00T00:00:00Z", properties.get("login-date-time").get("example").asText());
+        assertEquals("0.0.0.0", properties.get("ipv4-address").get("example").asText());
     }
 }