From: Martin Ciglan Date: Tue, 13 Jan 2015 15:42:46 +0000 (+0100) Subject: Bug 2561: Parser incorrectly allows usage of union in list X-Git-Tag: release/lithium~321^2 X-Git-Url: https://git.opendaylight.org/gerrit/gitweb?a=commitdiff_plain;h=dc80518c8ab2d253d2cd9686cdd0320841106aab;hp=9bd760d3fffdaca67bfcdf9d3202a6870830006d;p=yangtools.git Bug 2561: Parser incorrectly allows usage of union in list Proposed solution rejects this and parser throws exception Change-Id: I3cca06a72cfa33245f05a81e62b6a696966697c4 Signed-off-by: Martin Ciglan --- diff --git a/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/impl/YangParserListenerImpl.java b/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/impl/YangParserListenerImpl.java index 39dfbc9222..77a0a354df 100644 --- a/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/impl/YangParserListenerImpl.java +++ b/yang/yang-parser-impl/src/main/java/org/opendaylight/yangtools/yang/parser/impl/YangParserListenerImpl.java @@ -816,6 +816,10 @@ public final class YangParserListenerImpl extends YangParserBaseListener { } else if (childNode instanceof Key_stmtContext) { final Set 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"); + } } } } diff --git a/yang/yang-parser-impl/src/test/java/org/opendaylight/yangtools/yang/parser/impl/TypesResolutionTest.java b/yang/yang-parser-impl/src/test/java/org/opendaylight/yangtools/yang/parser/impl/TypesResolutionTest.java index 6156093cf8..65d9d5d35c 100644 --- a/yang/yang-parser-impl/src/test/java/org/opendaylight/yangtools/yang/parser/impl/TypesResolutionTest.java +++ b/yang/yang-parser-impl/src/test/java/org/opendaylight/yangtools/yang/parser/impl/TypesResolutionTest.java @@ -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 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 index 0000000000..1db87265ca --- /dev/null +++ b/yang/yang-parser-impl/src/test/resources/types/union-in-list/unioninlisttest.yang @@ -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