krasa / krasa-jaxb-tools

XJC / JAXB plugin for generation of Bean Validation Annotations (JSR-303) and replacing primitive types
Apache License 2.0
60 stars 49 forks source link

Attributes don't seem to be annotated unless they have a minOccurs > 0 #62

Closed younis-alrawi closed 7 years ago

younis-alrawi commented 7 years ago

I have come across what I believe to be a bug, where when I have the following XSD

<xs:element minOccurs="0" maxOccurs="1" name="StateOrTerritoryC" nillable="true">
    <xs:simpleType>
        <xs:restriction base="xs:string">
            <xs:enumeration value="AAT" />
            <xs:enumeration value="ACT" />
            <xs:enumeration value="NSW" />
            <xs:enumeration value="NT" />
            <xs:enumeration value="QLD" />
            <xs:enumeration value="SA" />
            <xs:enumeration value="TAS" />
            <xs:enumeration value="VIC" />
            <xs:enumeration value="WA" />
        </xs:restriction>
    </xs:simpleType>
</xs:element>

I will end up with the following

@XmlElementRef(name = "StateOrTerritoryC", namespace = "http://xyz", type = JAXBElement.class)
    protected JAXBElement<String> stateOrTerritoryC;

The moment I change the minOccurs from 0 to 1

<xs:element minOccurs="1" maxOccurs="1" name="StateOrTerritoryC" nillable="true">
    <xs:simpleType>
        <xs:restriction base="xs:string">
            <xs:enumeration value="AAT" />
            <xs:enumeration value="ACT" />
            <xs:enumeration value="NSW" />
            <xs:enumeration value="NT" />
            <xs:enumeration value="QLD" />
            <xs:enumeration value="SA" />
            <xs:enumeration value="TAS" />
            <xs:enumeration value="VIC" />
            <xs:enumeration value="WA" />
        </xs:restriction>
    </xs:simpleType>
</xs:element>

I end up with the following

@XmlElement(name = "StateOrTerritoryC", required = true, nillable = true)
@Pattern(regexp = "(AAT)|(ACT)|(NSW)|(NT)|(QLD)|(SA)|(TAS)|(VIC)|(WA)")
protected String stateOrTerritoryC;

Has anybody noticed this ?

younis-alrawi commented 7 years ago

Please note that this is not an issue with the tool. Instead, this is an issues with JAXB. See the following link for details:

https://stackoverflow.com/questions/19665550/jaxb-element-that-is-both-optional-and-nillable