Skip to content

Is it possible to completely ignore namespaces during deserialization? #170

Open

Description

@lamba92

I have my data class that has:

@Serializable
@XmlSerialName(
    value = "project",
    namespace = POM_XML_NAMESPACE,
)
data class ProjectObjectModel(...)

But, some very old pom.xml do not provide any namespace at all! For example ch.qos.logback:logback-parent:0.9.7 starts with:

<?xml version="1.0"?><project>
  <parent>
    <artifactId>logback-parent</artifactId>
    <groupId>ch.qos.logback</groupId>
    <version>0.9.7</version>
  </parent>
  ...

Nowadays it is not really needed to add all this metadata since the classes themselves in which one is deserializing are the specifications! It either fits them or not.

Would it be possible to ignore errors like this one?

Activity

  1. pdvrieze commented on Jul 19, 2023

    @pdvrieze
    Owner

    At the moment there is no built-in standard way to handle missing namespaces. You could do it in the policy, but it is probably easier to have a wrapper around XmlReader that replaces the default (empty) namespace with the POM namespace for tags (but not attributes as the attributes are not qualified).

  2. lamba92 commented on Jul 20, 2023

    @lamba92
    Author

    Could you please show an example of both cases? Or at least a snippet to try to understand in which direction to go

  3. lamba92 commented on Aug 8, 2024

    @lamba92
    Author

    Hi, any updates on this? Since v83.3 this workaround:

    public inline fun <reified T : Any> XML.decodeFromString(
        namespace: String,
        string: String,
    ): T {
        return decodeFromReader<T>(
            object : XmlReader by XmlStreaming.newReader(string) {
                override val namespaceURI: String get() = namespace
            },
        )
    }

    is not working anymore. Is there now a way to properly ignore namespaces during deserialization?

  4. pdvrieze commented on Aug 8, 2024

    @pdvrieze
    Owner

    I'm not sure why the workaround doesn't work, what happens if you use a DelegatingXmlReader explicitly? The other way that you can work around it is to use the policy to do recovery (rather than throw an exception) – the filter you have should be more resilient.

  5. lamba92 commented on Aug 20, 2024

    @lamba92
    Author

    The problem seems to be here. When reader.name is invoked, somehow the QName generated does not use the overridden namespaceURI.

    Overriding the name as well works again:

    public inline fun <reified T : Any> XML.decodeFromString(
        namespace: String,
        string: String,
    ): T {
        return decodeFromReader<T>(ManualNamespaceXmlReader(namespace, string))
    }
    
    public class ManualNamespaceXmlReader(
        override val namespaceURI: String,
        xmlString: String,
    ) : XmlReader by xmlStreaming.newReader(xmlString) {
        override fun toString(): String = "ManualNamespaceXmlReader(namespaceURI=\"$namespaceURI\")"
    
        override val name: QName
            get() = QName(namespaceURI, localName, prefix)
    }
    
  6. pdvrieze commented on Aug 21, 2024

    @pdvrieze
    Owner

    @lamba92 The problem is that when using delegation the reader.name will call the namespaceURI in the delegate, rather than the overridden writer. Where if you manually delegate (not using by delegate) the implementation of name would have to either be explicitly overridden, or it will use the default implementation (that gets the components directly).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions