Replies: 4 comments 4 replies
-
The code does generate prefixes if it doesn't have a an existing one, but they start with ns1 and increment until an unused prefix is found. However, by default serialization uses the "native" serializer (as in the stax parser/serializer provided by the jvm (and this can be adjusted using service loaders - at jvm level). I would suspect that your system loads such an xml library (which gets picked up automatically - I only test against the default jvm library and woodstox), and that this library generates crappy prefixes. This doesn't mean however that there isn't a bug. I'll have a check with the default implementation. |
Beta Was this translation helpful? Give feedback.
-
I will try to isolate a reproducer, AFAIK some |
Beta Was this translation helpful? Give feedback.
-
Your reproducer has some gaps (null attributes will actually mean empty attributes, but I have managed to reproduce the output with the following reproducer. /**
* Tests for but #196 with strange namespaces.
*/
class StrangeNamespace196 {
@Test
fun testSerialize() {
val xml = XML {
recommended_0_87_0 {
}
}
// val data = Container(Code("ABC", "null", "null", "null", LanguageTagCode("null")))
val data = Container(Code("ABC", "null", "null", "null", LanguageTagCode("en") ))
val serialized = xml.encodeToString(Container.serializer(), data)
assertXmlEquals("<Container></Container>", serialized)
}
@Serializable
class Container(
@XmlElement
@XmlSerialName(
value = "ReceivingSystem",
namespace = "http://myrealns.example.com/foo",
)
val receivingSystem: Code? = null)
@Serializable
@XmlSerialName(
value = "Code",
namespace = "http://myrealns.example.com/bar",
)
data class Code(
@XmlValue
val `value`: String,
val listID: String? = null,
val listVersionID: String? = null,
val name: String? = null,
val languageID: LanguageTagCode? = null,
)
@Serializable
@JvmInline
value class LanguageTagCode(val lang: String)
} |
Beta Was this translation helpful? Give feedback.
-
Ok. I've figured out what is going on. This is a problem with the default xml writer provided by the JVM (11 all the way to 21) when it is in namespace repairing mode (the default - one of the config flags). "Fix": val xml = XML { repairNamespaces=false } The serialization module is designed to write namespace attributes when needed (and doesn't rely on the "automatic repair mode" ) The short of it: The default JVM serializer is somewhat "broken" (in this case in namespace repairing mode) The long, what happens is:
|
Beta Was this translation helpful? Give feedback.
-
When encoding my classes to a String, sometimes I get this output:
This happens with different classes, small as well big ones but I am unable to create a small reproducer...
While this is valid xml, it makes testing harder. Do you have any idea how this behavior is triggered? I didn't find the
zdef
prefix in the code of xmlutil and I am lost.Kotlin 1.9.22, serialization 1.6.2, xmutil 0.86.3
Beta Was this translation helpful? Give feedback.
All reactions