From ded2b6279af65315840a43a2d507cd715bdad851 Mon Sep 17 00:00:00 2001 From: Robert Varga Date: Thu, 4 Jun 2020 13:06:06 +0200 Subject: [PATCH] Check unions for identityref types If an identityref type is encountered within a union, we end up going to TypeDefinitionAwareCodec, which is not equiped to handle the complexities of XML-encoding identityrefs. Make sure we do perform a check to side-step this problem. JIRA: YANGTOOLS-1108 Change-Id: I8dcf70385aceabc5f3e33a508ddd00f926047774 Signed-off-by: Robert Varga --- .../yang/data/codec/xml/XMLStreamWriterUtils.java | 15 +++++++++++++++ 1 file changed, 15 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 62766f4e4f..ac3119ff40 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 @@ -21,6 +21,7 @@ import org.opendaylight.yangtools.yang.model.api.TypeDefinition; import org.opendaylight.yangtools.yang.model.api.type.IdentityrefTypeDefinition; import org.opendaylight.yangtools.yang.model.api.type.InstanceIdentifierTypeDefinition; import org.opendaylight.yangtools.yang.model.api.type.LeafrefTypeDefinition; +import org.opendaylight.yangtools.yang.model.api.type.UnionTypeDefinition; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -72,11 +73,25 @@ abstract class XMLStreamWriterUtils { return encode(writer, (IdentityrefTypeDefinition) type, value, parent); } else if (type instanceof InstanceIdentifierTypeDefinition) { return encode(writer, (InstanceIdentifierTypeDefinition) type, value); + } else if (value instanceof QName && isIdentityrefUnion(type)) { + // Ugly special-case form unions with identityrefs + return encode(writer, (QName) value, parent); } else { return serialize(type, value); } } + private static boolean isIdentityrefUnion(final TypeDefinition type) { + if (type instanceof UnionTypeDefinition) { + for (TypeDefinition subtype : ((UnionTypeDefinition) type).getTypes()) { + if (subtype instanceof IdentityrefTypeDefinition || isIdentityrefUnion(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