From 8bf10220fc15babe0ad92ae2f439f8fdf925986a Mon Sep 17 00:00:00 2001 From: Robert Varga Date: Thu, 4 Jun 2020 14:11:05 +0200 Subject: [PATCH] Check unions for instance-identifier types 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 --- .../yang/data/codec/xml/XMLStreamWriterUtils.java | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/yang/yang-data-codec-xml/src/main/java/org/opendaylight/yangtools/yang/data/codec/xml/XMLStreamWriterUtils.java b/yang/yang-data-codec-xml/src/main/java/org/opendaylight/yangtools/yang/data/codec/xml/XMLStreamWriterUtils.java index ac3119ff40..9ac5106b52 100644 --- a/yang/yang-data-codec-xml/src/main/java/org/opendaylight/yangtools/yang/data/codec/xml/XMLStreamWriterUtils.java +++ b/yang/yang-data-codec-xml/src/main/java/org/opendaylight/yangtools/yang/data/codec/xml/XMLStreamWriterUtils.java @@ -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 codec = TypeDefinitionAwareCodec.from(type); if (codec == null) { -- 2.36.6