Require union field to be set 76/103176/2
authorRobert Varga <robert.varga@pantheon.tech>
Wed, 9 Nov 2022 18:05:46 +0000 (19:05 +0100)
committerRobert Varga <robert.varga@pantheon.tech>
Wed, 9 Nov 2022 18:14:02 +0000 (19:14 +0100)
Do not allow an union type object to be costructed without supplying one
of its alternatives.

JIRA: MDSAL-792
Change-Id: I69a9dc3f0094cc9ff03d0846d5f9dcc746417628
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
binding/mdsal-binding-java-api-generator/src/main/java/org/opendaylight/mdsal/binding/java/api/generator/UnionTemplate.xtend
binding/mdsal-binding-test-model/src/test/java/org/opendaylight/mdsal/binding/test/model/Mdsal792Test.java [new file with mode: 0644]

index d982873700f7b8fb2a6bce215dc879d52692fdcc..a76d86b55a73fa5a1fdbd044db4a487447b79eaa 100644 (file)
@@ -66,12 +66,13 @@ class UnionTemplate extends ClassTemplate {
                 «generateCheckers(property, restrictions, actualType)»
             «ENDIF»
             «val propertyAndTopParentProperties = parentProperties + #[property]»
+            «val propFieldName = property.fieldName»
             public «type.name»(«propertyAndTopParentProperties.asArgumentsDeclaration») {
                 super(«parentProperties.asArguments»);
                 «IF restrictions !== null»
-                    «checkArgument(property, restrictions, actualType, property.fieldName)»
+                    «checkArgument(property, restrictions, actualType, propFieldName)»
                 «ENDIF»
-                this.«property.fieldName» = «property.fieldName»;
+                this.«propFieldName» = «JU_OBJECTS.importedName».requireNonNull(«propFieldName»);
                 «FOR other : finalProperties»
                     «IF !property.equals(other)»
                          this.«other.fieldName» = null;
diff --git a/binding/mdsal-binding-test-model/src/test/java/org/opendaylight/mdsal/binding/test/model/Mdsal792Test.java b/binding/mdsal-binding-test-model/src/test/java/org/opendaylight/mdsal/binding/test/model/Mdsal792Test.java
new file mode 100644 (file)
index 0000000..e253b0a
--- /dev/null
@@ -0,0 +1,21 @@
+/*
+ * Copyright (c) 2022 PANTHEON.tech, s.r.o. and others.  All rights reserved.
+ *
+ * This program and the accompanying materials are made available under the
+ * terms of the Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at http://www.eclipse.org/legal/epl-v10.html
+ */
+package org.opendaylight.mdsal.binding.test.model;
+
+import static org.junit.jupiter.api.Assertions.assertThrows;
+
+import org.junit.jupiter.api.Test;
+import org.opendaylight.yang.gen.v1.urn.test.rev170101.Cont.VlanId;
+import org.opendaylight.yang.gen.v1.urn.test.rev170101.Cont.VlanId.Enumeration;
+
+public class Mdsal792Test {
+    @Test
+    public void testRejectNulls() {
+        assertThrows(NullPointerException.class, () -> new VlanId((Enumeration) null));
+    }
+}