[YANGTOOLS-155] Union Codec serialize throws Class cast when the union object is passed Created: 07/May/14  Updated: 10/Apr/22  Resolved: 28/May/14

Status: Resolved
Project: yangtools
Component/s: None
Affects Version/s: None
Fix Version/s: None

Type: Bug
Reporter: Kamal Rameshan Assignee: Kamal Rameshan
Resolution: Done Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified
Environment:

Operating System: Mac OS
Platform: PC


Issue Links:
Blocks
blocks OPNFLWPLUG-121 Reserve ports should be logical ports Resolved
External issue ID: 973
Priority: Normal

 Description   

I have a union type in my yang schema

leaf port-number {
type union

{ type uint32; type string; }

}

through which it creates a PortNumber object.

But the Union codec UnionCodecStringImpl.serialize accepts a String and not the union object PortNumber.

The serialize() should be able to look for the types within the UnionTypeDefinition and return back the non-null value of the union.

Following is the error i am getting:
osgi> 2014-05-05 18:25:52.634 PDT [http-bio-8080-exec-8] ERROR o.o.y.y.d.i.c.xml.XmlDocumentUtils - Provided node value PortNumber [_string=LOCAL] did not have type type (urn:ietf:params:xml:ns:yang:1)union (types=[, uint32, string] required by mapping. Using stream instead.
java.lang.ClassCastException: org.opendaylight.yang.gen.v1.urn.opendaylight.flow.types.port.rev130925.CommonPort$PortNumber cannot be cast to java.lang.String
at org.opendaylight.yangtools.yang.data.impl.codec.TypeDefinitionAwareCodec$UnionCodecStringImpl.serialize(TypeDefinitionAwareCodec.java:548) ~[bundlefile:na]
at org.opendaylight.yangtools.yang.data.impl.codec.xml.XmlDocumentUtils.writeValueByType(XmlDocumentUtils.java:257) [bundlefile:na]
at org.opendaylight.yangtools.yang.data.impl.codec.xml.XmlDocumentUtils.writeValueByType(XmlDocumentUtils.java:218) [bundlefile:na]
at org.opendaylight.yangtools.yang.data.impl.codec.xml.XmlDocumentUtils.createXmlRootElement(XmlDocumentUtils.java:168) [bundlefile:na]
at org.opendaylight.yangtools.yang.data.impl.codec.xml.XmlDocumentUtils.createXmlRootElement(XmlDocumentUtils.java:189) [bundlefile:na]
at org.opendaylight.yangtools.yang.data.impl.codec.xml.XmlDocumentUtils.createXmlRootElement(XmlDocumentUtils.java:189) [bundlefile:na]
at org.opendaylight.yangtools.yang.data.impl.codec.xml.XmlDocumentUtils.toDocument(XmlDocumentUtils.java:112) [bundlefile:na]



 Comments   
Comment by Kamal Rameshan [ 07/May/14 ]

https://git.opendaylight.org/gerrit/#/c/6799/ submitted for review

Comment by Tom Pantelis [ 19/May/14 ]

This bug is similar to YANGTOOLS-160. I went down a similar path to modify the EnumCodecStringImpl, which works but the root cause is actually elsewhere in the codec toDomStatic method generated by the TransformerGenerator. The value instance should already be an appropriate String instance when it reaches the EnumCodecStringImpl/UnionCodecStringImpl.

Generated at Wed Feb 07 20:52:25 UTC 2024 using Jira 8.20.10#820010-sha1:ace47f9899e9ee25d7157d59aa17ab06aee30d3d.