Set example for enum type 52/105652/9
authorIvan Hrasko <ivan.hrasko@pantheon.tech>
Tue, 25 Apr 2023 12:12:58 +0000 (14:12 +0200)
committerIvan Hrasko <ivan.hrasko@pantheon.tech>
Fri, 16 Jun 2023 12:19:50 +0000 (12:19 +0000)
We should distinguish between "default" and "example" OpenAPI
property.

Set "default" only when enum has defined default value in YANG model.
Otherwise set "example" as a first element of enumeration.

JIRA: NETCONF-999
Change-Id: I1873115deacb3f7b06537d1427c1b2e3d4ea82cd
Signed-off-by: Ivan Hrasko <ivan.hrasko@pantheon.tech>
restconf/restconf-openapi/src/main/java/org/opendaylight/restconf/openapi/impl/DefinitionGenerator.java

index d8d6cad45723b52e511e2f96ff32c19260659216..910e8e1f1731049fc65229e7832baacc881a4cb2 100644 (file)
@@ -103,6 +103,7 @@ public class DefinitionGenerator {
     private static final String ENUM_KEY = "enum";
     private static final String TITLE_KEY = "title";
     private static final String DEFAULT_KEY = "default";
+    private static final String EXAMPLE_KEY = "example";
     private static final String FORMAT_KEY = "format";
     private static final String NAMESPACE_KEY = "namespace";
     public static final String INPUT = "input";
@@ -659,7 +660,8 @@ public class DefinitionGenerator {
         }
 
         property.set(ENUM_KEY, enumNames);
-        setDefaultValue(property, enumLeafType.getValues().iterator().next().getName());
+        enumLeafType.getDefaultValue().ifPresent(v -> setDefaultValue(property, ((String) v)));
+        setExampleValue(property, enumLeafType.getValues().iterator().next().getName());
         return STRING_TYPE;
     }
 
@@ -892,6 +894,10 @@ public class DefinitionGenerator {
         }
     }
 
+    private static void setExampleValue(final ObjectNode property, final String value) {
+        property.put(EXAMPLE_KEY, value);
+    }
+
     private static void setDefaultValue(final ObjectNode property, final String value) {
         property.put(DEFAULT_KEY, value);
     }