OpenAPI: netopeer2 NPE because stack is null 63/110263/4
authorlubos-cicut <lubos.cicut@pantheon.tech>
Mon, 19 Feb 2024 10:33:44 +0000 (11:33 +0100)
committerRobert Varga <nite@hq.sk>
Wed, 6 Mar 2024 13:32:23 +0000 (13:32 +0000)
When we load netopeer2 modules, in karaf.log we get NullPointException.
It is because stack was transported to PropertyEntity#processTypeDef
as null.

JIRA: NETCONF-1254
Change-Id: I319313919568f8b9756ed63a3a92d670bf349b72
Signed-off-by: lubos-cicut <lubos.cicut@pantheon.tech>
restconf/restconf-openapi/src/main/java/org/opendaylight/restconf/openapi/model/PropertyEntity.java

index f973b1f9d73a97be197e5a20596bfb9a775e0743..cedf558898ea6252e7625300bf08b73a4305e863 100644 (file)
@@ -153,7 +153,7 @@ public class PropertyEntity {
         final var name = schemaNode.getQName().getLocalName();
         final var shouldBeAddedAsChild = !isParentConfig || schemaNode.isConfiguration();
         if (schemaNode instanceof ListSchemaNode || schemaNode instanceof ContainerSchemaNode) {
-            processDataNodeContainer((DataNodeContainer) schemaNode);
+            processDataNodeContainer((DataNodeContainer) schemaNode, stack);
             if (shouldBeAddedAsChild && isSchemaNodeMandatory(schemaNode)) {
                 required.add(name);
             }
@@ -174,7 +174,8 @@ public class PropertyEntity {
         stack.exit();
     }
 
-    private void processDataNodeContainer(final DataNodeContainer dataNode) throws IOException {
+    private void processDataNodeContainer(final DataNodeContainer dataNode, final SchemaInferenceStack stack)
+            throws IOException {
         final var schemaNode = (SchemaNode) dataNode;
         final var localName = schemaNode.getQName().getLocalName();
         final var nodeName = parentName + "_" + localName;
@@ -188,10 +189,11 @@ public class PropertyEntity {
             discriminator = definitionNames.getDiscriminator(schemaNode);
         }
 
-        processRef(nodeName, schemaNode, discriminator);
+        processRef(nodeName, schemaNode, discriminator, stack);
     }
 
-    private void processRef(final String name, final SchemaNode schemaNode, String discriminator) throws IOException {
+    private void processRef(final String name, final SchemaNode schemaNode, String discriminator,
+            final SchemaInferenceStack stack) throws IOException {
         final var ref = COMPONENTS_PREFIX + name + discriminator;
         if (schemaNode instanceof ListSchemaNode listNode) {
             generator.writeStringField(TYPE, ARRAY_TYPE);
@@ -204,7 +206,7 @@ public class PropertyEntity {
                 final var minElements = listNode.getElementCountConstraint().orElseThrow().getMinElements();
                 final var maxElements = listNode.getElementCountConstraint().orElseThrow().getMaxElements();
                 if (minElements != null) {
-                    createExamples(listNode, minElements);
+                    createExamples(listNode, minElements, stack);
                     generator.writeNumberField("minItems", minElements);
                 }
                 if (maxElements != null) {
@@ -221,8 +223,8 @@ public class PropertyEntity {
     }
 
     private void createExamples(final ListSchemaNode schemaNode,
-        @NonNull final Integer minElements) throws IOException {
-        final var firstExampleMap = prepareFirstListExample(schemaNode);
+            @NonNull final Integer minElements, final SchemaInferenceStack stack) throws IOException {
+        final var firstExampleMap = prepareFirstListExample(schemaNode, stack);
         final var examples = new ArrayList<Map<String, Object>>();
         examples.add(firstExampleMap);
 
@@ -258,14 +260,15 @@ public class PropertyEntity {
         generator.writeEndArray();
     }
 
-    private HashMap<String, Object> prepareFirstListExample(final ListSchemaNode schemaNode) {
+    private HashMap<String, Object> prepareFirstListExample(final ListSchemaNode schemaNode,
+            final SchemaInferenceStack stack) {
         final var childNodes = schemaNode.getChildNodes();
         final var firstExampleMap = new HashMap<String, Object>();
         // Cycle for each child node
         for (final var childNode : childNodes) {
             if (childNode instanceof TypedDataSchemaNode leafSchemaNode) {
                 final var def = new TypeDef();
-                processTypeDef(leafSchemaNode.getType(), leafSchemaNode, null, def);
+                processTypeDef(leafSchemaNode.getType(), leafSchemaNode, stack, def);
                 if (def.hasExample()) {
                     firstExampleMap.put(leafSchemaNode.getQName().getLocalName(), def.getExample());
                 }