You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I am trying to build a Decoder for a XML Schema that uses "inheritance" via the <xs:extension base="BaseClass"> schema element. Creating DTO model to match has me creating a base class and then deriving subclasses from it. Our schema is much more involved, but this sample illustrates the problem I'm having:
The XML, attributes and elements, required and optional, are a combination of the base class and derived class in the XML schema. The attributes and elements defined in the base object are prefixed with "Base"; items defined in the subclass are prefixed with "Sub". To decode this XML, I created the following base class and subclass:
The base class works as expected with the CodingKeys and DynamicNodeDecoding protocol. Attributes are decoded as attributes, elements as elements, and optionals handled with optional data types (?).
In the subclass, I understand that I cannot use DynamicNodeDecoding and, instead have to implement a init(from decoder: ...) constructor and manually decode the items. However, because I can't supply my own nodeDecoding(...) to specify the key type, .attribute vs .element, the attributes are being decodes as elements and failing:
Error decoding XML file: error=keyNotFound(CodingKeys(stringValue: "SubAttr1", intValue: nil), Swift.DecodingError.Context(codingPath: [CodingKeys(stringValue: "SubAttr1", intValue: nil)], debugDescription: "No element found for key CodingKeys(stringValue: \"SubAttr1\", intValue: nil) (\"SubAttr1\").", underlyingError: nil))
I can see in XMLKeyedDecodingContainer.decodeConcrete(...) there is a switch to decode as attribute or element, but I do not know how I can override the strategy (attribute, element, or both) used for a particular item when I'm calling the container.decodeXXX(...) functions.
Can anyone suggest an approach that would allow me to decode attributes, required and optional, in a subclass? Thanks!
Note: This is a vast simplification of the XML schema I'm working with. Many subclasses use the base class and for each subclass, there is actually a hierarchy of 5-10 subclasses before you get to the class being decoded. In the end, if this is not doable with the current framework, I may need to flatten the schema into the DTOs (i.e.: no subclasses), but that will be no easy task either and would love to be able to represent the schema as-is in the DTOs.
Thanks!
The text was updated successfully, but these errors were encountered:
Hi,
I am trying to build a Decoder for a XML Schema that uses "inheritance" via the
<xs:extension base="BaseClass">
schema element. Creating DTO model to match has me creating a base class and then deriving subclasses from it. Our schema is much more involved, but this sample illustrates the problem I'm having:The XML, attributes and elements, required and optional, are a combination of the base class and derived class in the XML schema. The attributes and elements defined in the base object are prefixed with "Base"; items defined in the subclass are prefixed with "Sub". To decode this XML, I created the following base class and subclass:
Base Class:
SubClass:
The base class works as expected with the
CodingKeys
andDynamicNodeDecoding
protocol. Attributes are decoded as attributes, elements as elements, and optionals handled with optional data types (?
).In the subclass, I understand that I cannot use
DynamicNodeDecoding
and, instead have to implement ainit(from decoder: ...)
constructor and manually decode the items. However, because I can't supply my ownnodeDecoding(...)
to specify the key type,.attribute
vs.element
, the attributes are being decodes as elements and failing:I can see in
XMLKeyedDecodingContainer.decodeConcrete(...)
there is a switch to decode as attribute or element, but I do not know how I can override the strategy (attribute, element, or both) used for a particular item when I'm calling thecontainer.decodeXXX(...)
functions.Can anyone suggest an approach that would allow me to decode attributes, required and optional, in a subclass? Thanks!
Note: This is a vast simplification of the XML schema I'm working with. Many subclasses use the base class and for each subclass, there is actually a hierarchy of 5-10 subclasses before you get to the class being decoded. In the end, if this is not doable with the current framework, I may need to flatten the schema into the DTOs (i.e.: no subclasses), but that will be no easy task either and would love to be able to represent the schema as-is in the DTOs.
Thanks!
The text was updated successfully, but these errors were encountered: