Repository navigation
jaxb underscores in schema element names - #713
Conversation
|
(Message from the pull-request-builder): Admins, please verify this patch for it to build in the pull-request-builder. |
|
Xjc also has an arg to decide how to handle dashes/underscores during code generation I believe. Would that be of any use? |
|
@sjaakd, maybe the JAXB method matching strategy should be adapted to take
the "_" pattern in `name()` into account as well?
|
|
failed to mention, I tried setting underscoreBinding="asCharInWord" but for some reason that only made it convert "planet_name" to "Planet_Name" which still does not match what was set in the annotation |
|
ok, the suffix idea was kind of stupid. It falls down as soon as are properties like "planetName" and "currentPlanetName" where one is a suffix of the other. then the code in my branch produces ambiguous matches. so nothing gained :) A different idea I am exploring right now is to inspect the targetType and find the field annotated with XmlElementRef. These annotations also contain the name exactly how it would be spelled over in the XmlElementDecl annotation. |
Yep.. That would make sense.. Would that solve the problem above? E.g. construct a regex that matches both |
Yes, I think it would. So from a camel-cased property name such as "planetName" we'd also find an element named "planet_name" in XML. Though we'd have to think about avoiding ambiguities if the elements "planet_name" and "planetName" are there.
That sounds interesting. How would that look like? Thanks for working on this btw., @grandmasterpixel! |
|
So my idea was something like this: @XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "ShippingAddressType", propOrder = {
"street",
"houseNumber",
"city",
"country",
"planetName"
})
public class ShippingAddressType {
//...
@XmlElementRef(name = "planet_name", namespace = "http://www.mapstruct.org/itest/jaxb/xsd/test2", type = JAXBElement.class)
protected JAXBElement<String> planetName;
//...
}and this @XmlRegistry
public class ObjectFactory {
//...
@XmlElementDecl(namespace = "http://www.mapstruct.org/itest/jaxb/xsd/test2", name = "houseNumber", scope = ShippingAddressType.class)
public JAXBElement<String> createShippingAddressTypeHouseNumber(String value) {
return new JAXBElement<String>(_ShippingAddressTypeHouseNumber_QNAME, String.class, ShippingAddressType.class, value);
}
@XmlElementDecl(namespace = "http://www.mapstruct.org/itest/jaxb/xsd/test2", name = "planet_name", scope = ShippingAddressType.class)
public JAXBElement<String> createShippingAddressTypePlanetName(String value) {
return new JAXBElement<String>(_ShippingAddressTypePlanetName_QNAME, String.class, ShippingAddressType.class, value);
}
}I can see what factory method to choose from the information on the annotations. So when XmlElementDeclSelector gets called to find a factory method for "planetName" it would need to
the last point was already solved by you so is was trying my luck on the other parts. |
|
what I last pushed still needs some code and branch cleanup but it is able to find the right factory for "planet_name" that I added to the jaxb integration test earlier. |
|
My first XmlElementRef lookup loop did not traverse up the inheritance chain. Now it does and a new version of the jaxb integration test tells me that that is the right thing to do :) |
|
I am back at work and my colleagues are already using a mapstruct version built from my branch so at least our problem is solved. Since our main problem is solved I will only be working on this on my own time now. Probably mostly in the evening (Central European Time) or weekends. That is unless we run into XmlElementRefs at work :) |
|
Hey, thanks for reporting back! Back online again, too, and we've planned
to finally take a closer look at this change later this week.
|
|
Today I have been playing around a bit. <complexType name="pea">
<choice maxOccurs="unbounded">
<element name="foo" type="xmlelementrefs:FooType"/>
<element name="bar" type="xmlelementrefs:BarType"/>
</choice>
</complexType>
<complexType name="FooType">
<sequence>
<element name="theFoo" type="string"/>
<element name="otherFoo" type="string"/>
</sequence>
</complexType>
<complexType name="BarType">
<sequence>
<element name="theBar" type="string"/>
<element name="otherBar" type="string"/>
</sequence>
</complexType>
<element name="pea" type="xmlelementrefs:pea"/>I get a Pea class with a property like this @XmlElementRefs({
@XmlElementRef(name = "bar", namespace = "http://www.mapstruct.org/itest/jaxb/xsd/xmlelementrefs", type = JAXBElement.class),
@XmlElementRef(name = "foo", namespace = "http://www.mapstruct.org/itest/jaxb/xsd/xmlelementrefs", type = JAXBElement.class)
})
protected List<JAXBElement<?>> fooOrBar;Which means this xml creates a heterogeneous List of a FooType and a BarType <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<pea xmlns="http://www.mapstruct.org/itest/jaxb/xsd/xmlelementrefs">
<foo>
<theFoo>0</theFoo>
<otherFoo>0</otherFoo>
</foo>
<bar>
<theBar>0</theBar>
<otherBar>0</otherBar>
</bar>
</pea>So to create a mapping method for that mapstruct would need to generate some kind "if instanceof else if" construct, I think. Does mapstruct already do something like that in the nonjaxb case? |
|
Hi @grandmasterpixel , I was just reviewing your code 😄
Did you look at the |
There was a problem hiding this comment.
I reckon getResultType() is more safe.. (you do that a bit later as well)
There was a problem hiding this comment.
uh, thanks, using getExecutable().getReturnType() would have actually been a bug :)
|
@grandmasterpixel . First of all, thanks for your contribution. I ran through your code. Main thing is that I find the selector hard to understand. What I do understand is that you (somehow) try to match up the
I think you're on the right track here. Main issues:
Next: I like the integration test a lot. However, I would like to see some (fixed) unit test as well. See for instance test package: Last but not least, I think you should cover additional problems in a separate PR. In this way we can speed up your solution a bit 👍 |
|
Hey @grandmasterpixel, I am a bit confused by the changes in Wouldn't it be sufficient to alter the name check so it's based on a "normalized" value of E.g. we look for a method matching property name "planetName", and we have two candidate methods: Now if there was another method Update: JAXB itself doesn't handle presence of "planet_name" and "planetName" very well: So we should be on the safe side if doing it as described above. I don't think the route via the target type and WDYT? |
|
If it just had been the case that I just needed to camelcase (planet_name->planetName) XmlElementDecl.name to get a valid match I would have probably gone with that. regarding jaxb collisions: |
|
just pushed something that I hope fixes all of the issues sjaakd mentioned |
|
@grandmasterpixel, cool, looks good to merge for me. Neat stuff with the Could you only change the e-mail of the commits to the one associated with your GitHub account and squash everything into a single commit; That way they will be properly linked to your user. Thanks! Your name (for copyright.txt) is Vincent Alexander Beelte, right? |
|
@grandmasterpixel. Looks good at first glance. I'll have one more thorough look tomorrow. One thing I spotted though is the absence of unit test (only integration test). I do think its quite straightforward to add a unit test as well (there are already some for jaxb selection). In that way, we spot problems immediately and don't have to wait for the outcome of the integration test. |
|
Forgot the unittest. Will be present in the squashed commit. |
|
Ah, could you also create an issue for it and refer it's number from the commit message ("#123 Bla bla"). Thx! |
…ElementDecl to implement #726
|
added unittest, rebased from origin/master fixed compilation, also I opened and referenced ticket #726 |
|
Writing this to trigger the Jenkins pull-request-builder: |
|
Merged; @grandmasterpixel, many thanks for implementing this and following up on all the review remarks. Much appreciated! |
|
or not, as gunnar was faster merging it... 😉 |
Hi,
since I have only been working with jaxb, xml schemas and soap for about 2 weeks there is a lot I still don't know about the problem I "solved" in this PR.
I have a wsdl of a third party soap service. Some of the elements have names using underscores instead of camel case like this:
which is perfectly ok.
When generating code from that wsdl with the "cxf-codegen-plugin" I get Code mit mostly "javaified" names (camel case with no underscores)
In the "ObjectFactory" the factory methods have @XmlElementDecl annotations that use the underscore names to set their "name" properties. Which might look like this:
Still kind of ok until I am trying to map my classes to the generated jaxb classes. For the cases where there is more than one property of the same type and one of them was generated from an underscore name in the schema I get a "Ambiguous mapping methods found for mapping property" compile error.
That seems to be because the XmlElementDeclSelector tries to exactly match the camel case name with the underscore name in the annotation. Failing that is falls back to only matching the scope, which result in too many candidates.
What I changed was add a third match on a method suffix. It seemed to me the generated code in ObjectFactory always followed the pattern of "TypeNamePropertyName" in the method names, so using the property name and match it as a suffix seemed like it might work.