Closed pdvrieze closed 6 months ago
@jpd236 This particular example was intended to work as the new version does, but it highlights a general issue of limited checking of root tag names. I've pushed an update that fixes this testing. What will now happen is that if the policy sets a tag qname (this requires XmlSerialName
) it will use and enforce that (or if the tag name is explicitly given), but if the name is not explicit it will use the name in the reader.
Note that serialization distinguishes type and usage name, and the SerialName annotation just changes that from property / type names. In XML it is generally practice to reuse tag definitions, thus the policy needs to support that. The default policy does this based on whether explicit naming can be detected (SerialName annotations are invisible). In such case it is considered that the type also represents a reusable tag name unless again explicitly overridden at use space.
Thanks. I used @SerialName
to try and simplify the toy example here as much as possible, but my actual code is indeed using @XmlSerialName
so your fix should impact it and restore the prior behavior. I'm parsing XML that can use one of two potential root elements without knowing which one up front, and so I try one parse, and if it fails, try the other. This only works if the parser fails when the root tag mismatches.
@jpd236 For your use case you may want to consider that the xml format does support polymorphic root tags, so you could deal with your use case that way (there is a test on polymorphic root tags) - you can use (sealed) interfaces or just use any as base type with a serializersModule for the subtypes.
Just released.
Discussed in https://github.com/pdvrieze/xmlutil/discussions/186