Bug 2561: Parser incorrectly allows usage of union in list 29/14129/6
authorMartin Ciglan <mciglan@cisco.com>
Tue, 13 Jan 2015 15:42:46 +0000 (16:42 +0100)
committerMartin Ciglan <mciglan@cisco.com>
Fri, 23 Jan 2015 09:53:03 +0000 (10:53 +0100)
Proposed solution rejects this and parser throws exception

Change-Id: I3cca06a72cfa33245f05a81e62b6a696966697c4
Signed-off-by: Martin Ciglan <mciglan@cisco.com>
yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/impl/YangParserListenerImpl.java
yang/yang-parser-impl/src/test/java/org/opendaylight/yangtools/yang/parser/impl/TypesResolutionTest.java
yang/yang-parser-impl/src/test/resources/types/union-in-list/unioninlisttest.yang [new file with mode: 0644]

index 39dfbc9222bd7b3a27cc753de40fcc75c32dbeec..77a0a354dfa341e69805bdf0769eaa46358cb3f0 100644 (file)
@@ -816,6 +816,10 @@ public final class YangParserListenerImpl extends YangParserBaseListener {
             } else if (childNode instanceof Key_stmtContext) {
                 final Set<String> key = createListKey((Key_stmtContext) childNode);
                 builder.setKeys(key);
+            } else if (childNode instanceof YangParser.Identifier_stmtContext) {
+                if (childNode.getChild(0).toString().equals("union")) {
+                    throw new YangParseException(moduleName, line, "Union statement is not allowed inside a list statement");
+                }
             }
         }
     }
index 6156093cf89684131339d108868a6c08c924dd7d..65d9d5d35c8b582959b9b32048572185b62e5afe 100644 (file)
@@ -39,6 +39,7 @@ import org.opendaylight.yangtools.yang.model.util.ExtendedType;
 import org.opendaylight.yangtools.yang.model.util.IdentityrefType;
 import org.opendaylight.yangtools.yang.model.util.InstanceIdentifierType;
 import org.opendaylight.yangtools.yang.model.util.UnionType;
+import org.opendaylight.yangtools.yang.parser.util.YangParseException;
 
 public class TypesResolutionTest {
     private Set<Module> testedModules;
@@ -346,4 +347,10 @@ public class TypesResolutionTest {
         parser.parseFiles(Arrays.asList(unionbits));
     }
 
+    @Test(expected = YangParseException.class)
+    public void testUnionInList() throws Exception {
+       File unioninlist = new File(getClass().getResource("/types/union-in-list/unioninlisttest.yang").toURI());
+       YangContextParser parser = new YangParserImpl();
+       parser.parseFiles(Arrays.asList(unioninlist));
+    }
 }
diff --git a/yang/yang-parser-impl/src/test/resources/types/union-in-list/unioninlisttest.yang b/yang/yang-parser-impl/src/test/resources/types/union-in-list/unioninlisttest.yang
new file mode 100644 (file)
index 0000000..1db8726
--- /dev/null
@@ -0,0 +1,32 @@
+module unioninlisttest {
+
+    namespace "urn:uilt";
+    prefix "uilt";
+
+    revision 2015-01-13 {
+    }
+
+    list foo {
+        key "name";
+        unique "ip port";
+
+        leaf name {
+            type string;
+        }
+
+        union {
+            type int32;
+            type enumeration {
+                enum "test";
+            }
+        }
+
+        leaf ip {
+            type string;
+        }
+
+        leaf port {
+            type string;
+        }
+    }
+}
\ No newline at end of file