Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Namespace repairing generates xmlns definitions for xml: prefix (which is implicit) #395

Closed
mvysny opened this issue Dec 12, 2019 · 4 comments
Milestone

Comments

@mvysny
Copy link

mvysny commented Dec 12, 2019

Saving this class:

data class StdTitle(@field:JacksonXmlProperty(isAttribute = true, namespace = "http://www.w3.org/XML/1998/namespace", localName = "lang") var lang: String = "")

will produce the following XML when namespace repairing is true:

<StdTitle xmlns:wstxns1="http://www.w3.org/XML/1998/namespace" wstxns1:lang="en"/>

However, the xml: prefix and the http://www.w3.org/XML/1998/namespace namespace is implicitly defined on all XMLs and there is no need to generate xmlns for that.

@mvysny
Copy link
Author

mvysny commented Dec 12, 2019

Workaround is to disable namespace repairing and explicitly set xml prefix. Sample code:

        val mapper: XmlMapper = XmlMapper().apply {
            enable(SerializationFeature.INDENT_OUTPUT)
            enable(ToXmlGenerator.Feature.WRITE_XML_DECLARATION)
            setSerializationInclusion(JsonInclude.Include.NON_NULL)
            factory.xmlOutputFactory.setProperty(WstxOutputProperties.P_USE_DOUBLE_QUOTES_IN_XML_DECL, true)
            factory.xmlOutputFactory.setProperty(XMLOutputFactory.IS_REPAIRING_NAMESPACES, false)
            factory.xmlOutputFactory.setProperty(WstxInputProperties.P_RETURN_NULL_FOR_DEFAULT_NAMESPACE, true)
        }
        file.outputStream().buffered().use { fout ->
            val writer: XMLStreamWriter = mapper.factory.xmlOutputFactory.createXMLStreamWriter(fout)
            writer.setPrefix("xml", "http://www.w3.org/XML/1998/namespace")
            mapper.writeValue(writer, this)
            writer.close()
        }

@cowtowncoder
Copy link
Member

If this is for actual Woodstox bug, test should only use Woodstox API, and not rely on Jackson XML format module.
Would it be simple enough to change test in that way?

Alternatively I can transfer this to jackson-dataformat-xml if this is more likely to be due to the way XML module uses stax(2) API.

@cowtowncoder cowtowncoder transferred this issue from FasterXML/woodstox Apr 22, 2020
@cowtowncoder
Copy link
Member

Interesting. I can reproduce this -- I am surprised Woodstox does try to output this as it shouldn't (as per XML specification). Will need to see what gives.

@cowtowncoder cowtowncoder removed the 2.12 label May 14, 2020
@cowtowncoder cowtowncoder added this to the 2.10.5 milestone May 14, 2020
@cowtowncoder cowtowncoder changed the title namespace repairing generates xmlns definitions for xml: prefix (which is implicit) Namespace repairing generates xmlns definitions for xml: prefix (which is implicit) May 14, 2020
@cowtowncoder
Copy link
Member

Fix will be included in 2.10.5 (which will take a while to release) and 2.11.1 (probably released earlier, in 1-2 months)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants