-
Notifications
You must be signed in to change notification settings - Fork 99
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
xsd:elements defined under a xsd:choice should be correctly represented in the java class #558
Comments
I understand quite well what you're talking about but we can't produce this from XJC. <xsd:element name="array" type="SequenceOfData" />
<xsd:element name="structure" type="SequenceOfData" /> That would lead to creating : public Data(SequenceOfData array) {
this.array = array;
}
public Data(SequenceOfData structure) {
this.structure = structure;
} Which is invalid java code (same code signature of constructor). Maybe a new plugin that would insert some code in setters which will assert that each fields of the class is null before setting the actual data ? |
Hi @laurentschoelens , I opened this issue because we are given an standard xsd, we want to create java classes as part of our internal library. Multiple projects will use this library and we don't want the users to have any knowledge on the xsd itself. I want the generated Java classes to be self-sufficient. As of now, the generated classes have no restriction on the number of fields the user can initialize. Ideally, the choice element says that only one of the field should be present inside the object. If I'm not being clear, there is an article on the issue . The only other way we have is to take the created object and validate it against the schema. But it would be great to restrict the users to not let them initialize other fields at all. Okay, from your above comment, I agree that this scenario would be a problem
What if the plugin could generate Builder class with methods like
Or as you said, the setters should have the restriction |
A couple thoughts to consider:
choice is an odd paradigm. Not sure the benefit of using it outweighs all the language pain points. Using anyType or extension of types are more readily rationalized into programming languages. A helper model class (possibly using a JAXB Adapter) may be another option to consider. Create a new class that enforces the desired behavior. |
Hi @mattrpav , Having a
Generates:
Having something like this in the xsd
Generates:
I think the plugin can follow similar approach? Perhaps a builder pattern which will only initialize the Object with one property (Builder can reset the other properties to null and only retain the last property provided to it). |
I've taken back your example (with less elements inside <xsd:complexType name="Key-Info">
<xsd:sequence>
<xsd:choice>
<xsd:element name="identified-key" type="xsd:int" />
<xsd:element name="wrapped-key" type="xsd:string" />
</xsd:choice>
<xsd:choice>
<xsd:element name="another-identified-key" type="xsd:int" />
<xsd:element name="another-wrapped-key" type="xsd:string" />
</xsd:choice>
<xsd:element name="mandatory-key" type="xsd:string" />
</xsd:sequence>
</xsd:complexType> This generates the following : public class KeyInfo {
@XmlElement(name = "identified-key")
protected Integer identifiedKey;
@XmlElement(name = "wrapped-key")
protected String wrappedKey;
@XmlElement(name = "another-identified-key")
protected Integer anotherIdentifiedKey;
@XmlElement(name = "another-wrapped-key")
protected String anotherWrappedKey;
@XmlElement(name = "mandatory-key", required = true)
protected String mandatoryKey;
// ...
} Having such thing make it more complex to create a builder class (since you will need the For the moment, I don't see a simple solution to handle each use case that would be implemented in a new XJC plugin. |
Hi @laurentschoelens, yeah, I could see this would be very complex. |
Given an xsd sample:
This generates a class with setters for each property. That means, we can initialize multiple fields in once object instance of Data. This should be restricted. Ideally, it should have maybe create multiple constructors which accepts only one field.
Like:
These constructors created for each field would have the correct representation and restricts user to only initialize the Data object with one field.
Data.txt
The text was updated successfully, but these errors were encountered: