Check unions for instance-identifier types 52/90252/2
authorRobert Varga <robert.varga@pantheon.tech>
Thu, 4 Jun 2020 12:11:05 +0000 (14:11 +0200)
committerRobert Varga <robert.varga@pantheon.tech>
Thu, 4 Jun 2020 12:21:38 +0000 (14:21 +0200)
If an instance-identifier type is encountered within a union, we
end up going to TypeDefinitionAwareCodec, which is not equiped to
handle the complexities of XML-encoding instance identifiers. Make
sure we do perform a check to side-step this problem.

Change-Id: Ib5bc79a26808f4101ce182e092418a29e5b7bdc8
JIRA: YANGTOOLS-1108
Signed-off-by: Robert Varga <robert.varga@pantheon.tech>
yang/yang-data-codec-xml/src/main/java/org/opendaylight/yangtools/yang/data/codec/xml/XMLStreamWriterUtils.java

index ac3119ff40f89c3544d8816f6ecc153b6b94866a..9ac5106b520e602c31fbee0b3df0185160e8510e 100644 (file)
@@ -76,6 +76,8 @@ abstract class XMLStreamWriterUtils {
         } else if (value instanceof QName && isIdentityrefUnion(type)) {
             // Ugly special-case form unions with identityrefs
             return encode(writer, (QName) value, parent);
+        } else if (value instanceof YangInstanceIdentifier && isInstanceIdentifierUnion(type)) {
+            return encodeInstanceIdentifier(writer, (YangInstanceIdentifier) value);
         } else {
             return serialize(type, value);
         }
@@ -92,6 +94,17 @@ abstract class XMLStreamWriterUtils {
         return false;
     }
 
+    private boolean isInstanceIdentifierUnion(final TypeDefinition<?> type) {
+        if (type instanceof UnionTypeDefinition) {
+            for (TypeDefinition<?> subtype : ((UnionTypeDefinition) type).getTypes()) {
+                if (subtype instanceof InstanceIdentifierTypeDefinition || isInstanceIdentifierUnion(subtype)) {
+                    return true;
+                }
+            }
+        }
+        return false;
+    }
+
     private static String serialize(final @NonNull TypeDefinition<?> type, final @NonNull Object value) {
         final TypeDefinitionAwareCodec<Object, ?> codec = TypeDefinitionAwareCodec.from(type);
         if (codec == null) {