JAXB problem with complexType restrictions
843834Nov 8 2002 — edited Apr 17 2003Hello,
It looks like there's a bug in JAXB validation of complex types that restrict other complex types. From looking at the code, it looks as if the the class associated with a complexType automatically inherits all of the field definitions from the complexType that it is restricting. Instead the class for the complextype should only contain field descriptors detailed in its own <xs:restriction> block and not those of the base complexType. XMLSpy seems to validate this type of document correctly, meaning that it complains if the restricted complexType has any elements that are not declared in its own <xs:restriction> block. JAXB on the other hand, will accept such a document as valid. Is this a bug or am I doing something wrong?
Here's a sample schema and xml doc to illustrate the behaviour I'm seeing:
Schema:
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" elementFormDefault="qualified" attributeFormDefault="unqualified">
<!-- The super set of addons that contains 3 elements A,B,C -->
<xs:complexType name="Super_AddOns">
<xs:sequence>
<xs:element name="A" type="xs:string"/>
<xs:element name="B" type="xs:string"/>
<xs:element name="C" type="xs:string"/>
</xs:sequence>
</xs:complexType>
<!-- The subset of addons that contains only 2 elements A, B -->
<xs:complexType name="Sub_AddOns">
<xs:complexContent>
<xs:restriction base="Super_AddOns">
<xs:sequence>
<xs:element name="A" type="xs:string"/>
<xs:element name="B" type="xs:string"/>
</xs:sequence>
</xs:restriction>
</xs:complexContent>
</xs:complexType>
<!-- product one declares that it has an element of the subtype Sub_AddOns -->
<xs:element name="product_1">
<xs:complexType>
<xs:sequence>
<xs:element name="AddOns" type="Sub_AddOns"/>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
With this schema, the following document shouldn't validate, but with the JAXB classes, it unmarshals and validates without throwing an exception:
<!-- product_1 is set to have addOns of type Sub_AddOns which only allows elements A and B, thus the C element should cause this document to be invalid -->
<product_1 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="C:\xml\jaxb-1.0-beta\Untitled1.xsd">
<AddOns>
<A>safljaslfas</A>
<B>asdlfjasf</B>
<C>safasdf</C>
</AddOns>
</product_1>
Any ideas?
Thanks in advance for your help,
-mirza