OpenApi: Fix 'min-elements' for XML Leaf-List 94/108394/7
authorlubos-cicut <lubos.cicut@pantheon.tech>
Fri, 13 Oct 2023 12:21:56 +0000 (14:21 +0200)
committerIvan Hrasko <ivan.hrasko@pantheon.tech>
Tue, 24 Oct 2023 06:11:13 +0000 (06:11 +0000)
Manually set examples with the expected number of elements in the
leaf-list, rather than relying on OpenAPI to duplicate them for us.

JIRA: NETCONF-1171
Change-Id: I883c92a396f88c610067861dc1e31bdd8dfcceb3
Signed-off-by: lubos-cicut <lubos.cicut@pantheon.tech>
Signed-off-by: Ivan Hrasko <ivan.hrasko@pantheon.tech>
restconf/restconf-openapi/src/main/java/org/opendaylight/restconf/openapi/impl/DefinitionGenerator.java
restconf/restconf-openapi/src/test/java/org/opendaylight/restconf/openapi/impl/OpenApiGeneratorRFC8040Test.java
restconf/restconf-openapi/src/test/java/org/opendaylight/restconf/openapi/mountpoints/MountPointOpenApiTest.java

index 09b60c7d26d8aecb062b8d9595cfb9a2802a0411..f575b686a0e826ec75c4843999ffd6569a1f9ea5 100644 (file)
@@ -399,7 +399,17 @@ public final class DefinitionGenerator {
 
         processTypeDef(listNode.getType(), listNode, itemsVal, stack);
 
-        props.items(itemsVal.build());
+        final Property itemsValue = itemsVal.build();
+        final Property propsValue = props.build();
+        props.items(itemsValue);
+
+        if (itemsValue.example() != null && propsValue.minItems() != null) {
+            final List<Object> listOfExamples = new ArrayList<>();
+            for (int i = 0; i < propsValue.minItems(); i++) {
+                listOfExamples.add(itemsValue.example());
+            }
+            props.example(listOfExamples);
+        }
         props.description(listNode.getDescription().orElse(""));
 
         return props.build();
index 4d2da507e474a1d1c2288f84b529c3eee8f077a7..ffad5e96d92921bedcca1df42a21b91bfb6f50e8 100644 (file)
@@ -435,6 +435,34 @@ public final class OpenApiGeneratorRFC8040Test {
         assertEquals("urn:ietf:params:xml:ns:yang:test:action:types", namespace);
     }
 
+    /**
+     * Test that number of elements in payload is correct.
+     */
+    @SuppressWarnings("unchecked")
+    @Test
+    public void testLeafListWithMinElementsPayload() {
+        final var doc = generator.getApiDeclaration(MANDATORY_TEST, null, uriInfo);
+        assertNotNull(doc);
+        final var paths = doc.paths();
+        final var path = paths.get("/rests/data/mandatory-test:root-container/mandatory-container");
+        assertNotNull(path);
+        final var requestBody = path.put().requestBody().content();
+        assertNotNull(requestBody);
+        final var jsonRef = requestBody.get("application/json").schema().properties()
+            .get("mandatory-test:mandatory-container").ref();
+        assertNotNull(jsonRef);
+        final var xmlRef = requestBody.get("application/xml").schema().ref();
+        assertNotNull(xmlRef);
+        final var schema = doc.components().schemas().get("mandatory-test_root-container_mandatory-container");
+        assertNotNull(schema);
+        final var minItems = schema.properties().get("leaf-list-with-min-elements").minItems();
+        assertNotNull(minItems);
+        final var listOfExamples = ((List<String>) schema.properties().get("leaf-list-with-min-elements").example());
+        assertNotNull(listOfExamples);
+        assertEquals(jsonRef, xmlRef);
+        assertEquals(listOfExamples.size(), minItems.intValue());
+    }
+
     private static void verifyRequestRef(final Operation operation, final String expectedRef, final String nodeType) {
         final Map<String, MediaTypeObject> postContent;
         if (operation.requestBody() != null) {
index 1d68a9bc6405603c1a8f8a980f1357468bae6e19..6235e722e1054d3d75a539ad6c95301e6b09bcfd 100644 (file)
@@ -390,4 +390,36 @@ public final class MountPointOpenApiTest {
         assertNotNull(namespace);
         assertEquals("urn:ietf:params:xml:ns:yang:test:action:types", namespace);
     }
+
+    /**
+     * Test that number of elements in payload is correct.
+     */
+    @SuppressWarnings("unchecked")
+    @Test
+    public void testLeafListWithMinElementsPayloadOnMountPoint() throws Exception {
+        final var mockInfo = DocGenTestHelper.createMockUriInfo(HTTP_URL);
+        openApi.onMountPointCreated(INSTANCE_ID);
+        final var mountPointApi = openApi.getMountPointApi(mockInfo, 1L, null);
+        assertNotNull(mountPointApi);
+        final var paths = mountPointApi.paths();
+        final var path =
+            paths.get("/rests/data/nodes/node=123/yang-ext:mount/mandatory-test:root-container/mandatory-container");
+        assertNotNull(path);
+        final var requestBody = path.put().requestBody().content();
+        assertNotNull(requestBody);
+        final var jsonRef = requestBody.get("application/json").schema().properties()
+            .get("mandatory-test:mandatory-container").ref();
+        assertNotNull(jsonRef);
+        final var xmlRef = requestBody.get("application/xml").schema().ref();
+        assertNotNull(xmlRef);
+        final var schema =
+            mountPointApi.components().schemas().get("mandatory-test_root-container_mandatory-container");
+        assertNotNull(schema);
+        final var minItems = schema.properties().get("leaf-list-with-min-elements").minItems();
+        assertNotNull(minItems);
+        final var listOfExamples = ((List<String>) schema.properties().get("leaf-list-with-min-elements").example());
+        assertNotNull(listOfExamples);
+        assertEquals(jsonRef, xmlRef);
+        assertEquals(listOfExamples.size(), minItems.intValue());
+    }
 }