-
-
Notifications
You must be signed in to change notification settings - Fork 222
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
Support for providing namespace to use for fields of a Class (unless overridden by field) #18
Comments
Jackson does not support per-package annotations (except for specific case of JAXB annotations), so that'd be sort of new feature altogether (and this time, for jackson-databind :) ), but let's start with per-class one. Class annotations are inherited, so it can work as nicely if you have shared base class or interface(s) |
I am having a major problem with this limitation. I didn't realize that @XmlSchema was not supported with the XML annotation support. Once I added a namespace to my root element with @XmlRootElement or @JacksonXmlRootElement, the namespace shows up in the root properly, but every child element has xmlns="" added to it. Below is sample output based on code from a bug that was fixed in 2012. When the bug was first fixed the output looked fine but the xmlns="" started showing up in 2.0.3. The output is with version 2.4.3 and with woodstox-core-asl version 4.4.1 <person xmlns="http://example.org/person">
<name xmlns="">Name</name>
<age xmlns="">30</age>
<notes xmlns="">
<note>This is note #1</note>
<note>This is note #2</note>
</notes>
</person> Is there another way to get the expected results without having to annotate every field? |
@Poorman65 Support for (parts of?) Also: I think there may be another RFE for adding a way to pre-bind namespace prefixes outside of annotations. This would probably make sense as another way to tackle this problem. For what it is worth, I think addition of |
In my situation I have four classes that can be written as separate files or included inline with a parent (which would be one of the four). Each of these four is in its own package and uses its own namespace. What I need to do is to have all the children use the parents namespace unless it is one of those four, in which case its branch of the object graph would use that namespace. If I wanted to override the implemented behavior for my scenario, what class would I need to override? Perhaps the writer? As for the default namespace fix that is implemented, I don't think I've ever seen this anywhere. It seems that it is implemented as no namespace instead of default namespace. In my example above, the default namespace is xmlns="http://example.org/person" not xmlns="". |
Ok; so the way to customize handling of namespace to use is easiest done by sub-classing Now: as to fix, empy namespace; there are two meanings to default namespace:
What I meant was, specifically, that in order for a given element to have empty namespace, it is necessary to add And so the goal is not to try to suppress namespace binding code (which is actually working exactly like it should), but to make sure target namespace is correctly specified. |
Rats. I see But then again, perhaps supporting package-defaulting for this particular case would be ok. |
@Poorman65 Looking at Do you have examples that suggest that it would actually be applicable instance documents? |
Here is the code for an @XmlSchema example: package-info.java in schemasample package
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<person xmlns="my.xmlschema.person">
<name>Name</name>
<age>30</age>
<notes>
<note>This is note #1</note>
<note>This is note #2</note>
</notes>
</person> |
Also, I am still trying to work out the implementation for overriding the namespace behavior. I tried just overriding findNamespace in JaxbAnnotationIntrospector but couldn't figure out what to return. The parent doesn't appear to be accessible from that class. I am trying now to override the changeProperties method in XmlBeanSerializerModifier to be able to add the parent bean descriptor to my CustomJaxbAnnotationIntrospector as the content is processed. Not sure if there is something I am missing that would make this easier. Thanks for the help. |
Hmmh. Ok, so JAXB does seem to directly use the namespace. That is something that javadocs didn't quite show -- I would actually have expected it to lead to separate annotations in generated classes. I am fine adding such functionality, but since JAXB is such a complicated spec, want to make sure I follow its intent here. As I hope you don't have to use |
Ok, as usual, Blaise's blog is more useful than JAXB spec or javadoc. :) http://blog.bdoughan.com/2010/08/jaxb-namespaces.html So you are right in usage, and I should add support for it. And also see if |
+1 |
Can you post the link to the enhancement that you filed on this? I was curious about the status, as I would like to have this feature as well. |
@dsharp1 Not sure what the question is? No work has been done for this issue. |
It is possible to use the XmlMapper with a JacksonXmlAnnotationIntrospector to get the same effect as jaxb and XmlSchema with package level namespaces:
|
@msillence I tried this and it seems to not have the desired effect. I am simply trying to add xmlns="http://some.schema.com" to the root element. When I added the code above it started complaining about multiple explicit names on an attribute inside the class that would have the namespace declaration. |
same problem, JacsonNamespaceInstrospector doesnt work for me, my temporary solution:
|
@cowtowncoder , @msillence
By using JacksonXmlAnnotationIntrospector I am able to get
Prefix isn't there in xmlns:prefix=".........." |
@cowtowncoder in the beginning you mentioned
I tried creating an interface with |
@msugakov-sh a full reproduction (class declaration, json content, code called) would be useful; I can have a look. |
Sure, here you can find the repo: https://github.com/msugakov/jackson-schema-test <House number="30">
<kitchen numberOfWindows="2"/>
<bathroom hasHotWater="false"/>
<wstxns1:livingRoom xmlns:wstxns1="http://this-namespace-will-be-present/">
<atmosphere>Cosy</atmosphere>
</wstxns1:livingRoom>
</House> I was expecting Thanks, |
@msugakov thank you for adding more detail. Use of Lombok makes it bit trickier to follow in theory, but I think I can explain why But I am not sure why I hope to have time to look into this more in future, but right now I have bit of a backlog so that may take a while. |
@msugakov I found the issue wrt |
I don't know if this would be hard to implement, but from a user perspective, a straightforward and backwards-compatible API for such a feature would be to add an optional "defaultNamespace" to @JacksonXmlRootElement and @JacksonXmlProperty. It should apply to all descendant fields unless overridden (individually, or by another default further down the tree). Off-topic: If this happens, maybe throw in namespaceAlias as well? It should be inheritable in a similar fashion |
The root problem here is that the core abstractions in java and xml are fundamentally different. In xml the namespaces is a property of the element type, which in turn combines field name and type in the java mapping. If every element type mapped to its own class, we could do all namespacing on the class level, but we want to use String etc. for several element types, so we also need to control namespace from the owner-side. Jackson-xml seems to try to make-do with only owner-side namespacing (except for root elements), but this is a poor match for how xml works. |
Yeah Jackson does not really have concept of "inheritable" attributes/properties for nested Object properties. For what it is worth, Jackson does not really take namespace information into account on deserialization at all: it does try to match things as expected on serialization side (for interoperability), but not so for deserialization. |
It occurred to me that maybe mixing up use of the JaxbAnnotationModule and the JacksonAnnotationIntrospector might be bad idea - so I tried, again, this time just customizing the JaxbAnnotationModule to support my use case. The code I am using now does mostly what I need - except it generates a namespace for attributes, which I don't want - and it looks like this:
|
Currently in the xml-module you have to qualify each field with the wanted namespace. This in many ways are similar to JAXB, but JAXB provides @XmlSchema for adding namespaces at the package level.
It would be nice if one could provide a default namespace for a class, so that theres not need to qualify every single field.
Even better would be to provide this at the package level, but class level should be more than ok for now.
The text was updated successfully, but these errors were encountered: