Bug 5461: Augmenting a choice without a case from another module causes NPE 09/38309/3
authorFilip Gregor <fgregor@cisco.com>
Fri, 11 Mar 2016 12:41:57 +0000 (13:41 +0100)
committerFilip Gregor <fgregor@cisco.com>
Tue, 17 May 2016 12:03:27 +0000 (14:03 +0200)
fixed case creation, added tests

Change-Id: Ida25d45893058f320c0373355d41cc8c4b981f84
Signed-off-by: Filip Gregor <fgregor@cisco.com>
(cherry picked from commit 69779f07e0841a3e89204dad5d5a6bb966c4727c)

binding/mdsal-binding-generator-impl/src/main/java/org/opendaylight/yangtools/sal/binding/generator/impl/BindingGeneratorImpl.java
binding/mdsal-binding-java-api-generator/src/test/java/org/opendaylight/yangtools/sal/java/api/generator/stmt/parser/retest/CompilationTest.java
binding/mdsal-binding-java-api-generator/src/test/resources/compilation/bug5461/bar.yang [new file with mode: 0644]
binding/mdsal-binding-java-api-generator/src/test/resources/compilation/bug5461/foo.yang [new file with mode: 0644]

index b028ed78cda51f5bd54c9c9549298edbfb103383..5e719b49feb923995bf6cbc161de0ccf9eaea518 100644 (file)
@@ -837,7 +837,7 @@ public class BindingGeneratorImpl implements BindingGenerator {
 
         } else {
             generateTypesFromAugmentedChoiceCases(module, augmentPackageName, targetTypeBuilder.toInstance(),
-                    (ChoiceSchemaNode) targetSchemaNode, augSchema.getChildNodes());
+                    (ChoiceSchemaNode) targetSchemaNode, augSchema.getChildNodes(), null);
         }
     }
 
@@ -872,7 +872,7 @@ public class BindingGeneratorImpl implements BindingGenerator {
                     augSchema);
         } else {
             generateTypesFromAugmentedChoiceCases(module, augmentPackageName, targetTypeBuilder.toInstance(),
-                    (ChoiceSchemaNode) targetSchemaNode, augSchema.getChildNodes());
+                    (ChoiceSchemaNode) targetSchemaNode, augSchema.getChildNodes(), usesNodeParent);
         }
     }
 
@@ -1295,7 +1295,8 @@ public class BindingGeneratorImpl implements BindingGenerator {
      *             </ul>
      */
     private void generateTypesFromAugmentedChoiceCases(final Module module, final String basePackageName,
-            final Type targetType, final ChoiceSchemaNode targetNode, final Iterable<DataSchemaNode> augmentedNodes) {
+            final Type targetType, final ChoiceSchemaNode targetNode, final Iterable<DataSchemaNode> augmentedNodes,
+            final DataNodeContainer usesNodeParent) {
         checkArgument(basePackageName != null, "Base Package Name cannot be NULL.");
         checkArgument(targetType != null, "Referenced Choice Type cannot be NULL.");
         checkArgument(augmentedNodes != null, "Set of Choice Case Nodes cannot be NULL.");
@@ -1326,10 +1327,20 @@ public class BindingGeneratorImpl implements BindingGenerator {
                 }
 
                 ChoiceCaseNode node = null;
+                final String caseLocalName = caseNode.getQName().getLocalName();
                 if (caseNode instanceof ChoiceCaseNode) {
                     node = (ChoiceCaseNode) caseNode;
+                } else if (targetNode.getCaseNodeByName(caseLocalName) == null) {
+                    final String targetNodeLocalName = targetNode.getQName().getLocalName();
+                    for (DataSchemaNode dataSchemaNode : usesNodeParent.getChildNodes()) {
+                        if (dataSchemaNode instanceof ChoiceSchemaNode && targetNodeLocalName.equals(dataSchemaNode.getQName
+                                ().getLocalName())) {
+                            node = ((ChoiceSchemaNode) dataSchemaNode).getCaseNodeByName(caseLocalName);
+                            break;
+                        }
+                    }
                 } else {
-                    node = targetNode.getCaseNodeByName(caseNode.getQName().getLocalName());
+                    node = targetNode.getCaseNodeByName(caseLocalName);
                 }
                 final Iterable<DataSchemaNode> childNodes = node.getChildNodes();
                 if (childNodes != null) {
@@ -2266,4 +2277,4 @@ public class BindingGeneratorImpl implements BindingGenerator {
             builder.addAnnotation("", "Deprecated");
         }
     }
-}
+}
\ No newline at end of file
index 67cd6b0e28e0eb08f3b030208bc3fa141587fcad..821b28dd583a1b0961318c928054e9f58e89d1de 100644 (file)
@@ -589,6 +589,21 @@ public class CompilationTest extends BaseCompilationTest {
         cleanUp(sourcesOutputDir, compiledOutputDir);
     }
 
+    @Test
+    public void testBug5461() throws Exception {
+        final File sourcesOutputDir = new File(GENERATOR_OUTPUT_PATH + FS + "bug5461");
+        assertTrue("Failed to create test file '" + sourcesOutputDir + "'", sourcesOutputDir.mkdir());
+        final File compiledOutputDir = new File(COMPILER_OUTPUT_PATH + FS + "bug5461");
+        assertTrue("Failed to create test file '" + compiledOutputDir + "'", compiledOutputDir.mkdir());
+
+        generateTestSources("/compilation/bug5461", sourcesOutputDir);
+
+        // Test if sources are compilable
+        testCompilation(sourcesOutputDir, compiledOutputDir);
+
+        cleanUp(sourcesOutputDir, compiledOutputDir);
+    }
+
     /**
      * Test if class generated for node from grouping implements ChildOf.
      *
diff --git a/binding/mdsal-binding-java-api-generator/src/test/resources/compilation/bug5461/bar.yang b/binding/mdsal-binding-java-api-generator/src/test/resources/compilation/bug5461/bar.yang
new file mode 100644 (file)
index 0000000..6d9babe
--- /dev/null
@@ -0,0 +1,15 @@
+module bar {
+    namespace "urn:test:bar";
+    prefix bar;
+
+    revision 2016-01-01 {
+    }
+
+    grouping bar-grouping {
+        choice bar-choice {
+            leaf bar-leaf {
+              type string;
+            }
+        }
+    }
+}
\ No newline at end of file
diff --git a/binding/mdsal-binding-java-api-generator/src/test/resources/compilation/bug5461/foo.yang b/binding/mdsal-binding-java-api-generator/src/test/resources/compilation/bug5461/foo.yang
new file mode 100644 (file)
index 0000000..1f4441e
--- /dev/null
@@ -0,0 +1,22 @@
+module foo {
+    namespace "urn:test:foo";
+    prefix foo;
+
+    revision 2016-01-01 {
+    }
+
+    import bar {
+        prefix bar;
+        revision-date 2016-01-01;
+    }
+
+    grouping foo-grouping {
+        uses bar:bar-grouping {
+            augment "bar-choice" {
+                leaf foo-leaf {
+                  type string;
+                }
+            }
+        }
+    }
+}
\ No newline at end of file