jaxb
  1. jaxb
  2. JAXB-515

Default and fixed fields not generated correctly when using restriction to create new types.

    Details

    • Issuezilla Id:
      515

      Description

      If restriction is used to derive a new complex type from an existing type, and
      attributes are restricted by having a default or fixed value specified, where
      none was previously specified on the base type, then the xjc compiler fails to
      create the default or fixed value. It does manage to do it for default or fixed
      values on the base type itself.

      I've cut this down to a simple example to illustrate. Here is my schema (test.xsd):

      <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
      xmlns:test="http://thebadgerset.co.uk/test-0.1"
      targetNamespace="http://thebadgerset.co.uk/test-0.1"
      elementFormDefault="qualified">

      <xs:element name="test" type="test:B"/>

      <xs:complexType name="A">
      <xs:attribute name="fixedInRoot" type="xs:string" fixed="fixedValue"/>
      <xs:attribute name="fixedByOverride" type="xs:string"/>
      <xs:attribute name="defaultInRoot" type="xs:string" default="defaultValue"/>
      <xs:attribute name="defaultOverride" type="xs:string"/>
      <xs:attribute name="defaultOverrideByFixed" type="xs:string"
      default="defaultValue"/>
      </xs:complexType>

      <xs:complexType name="B">
      <xs:complexContent>
      <xs:restriction base="test:A">
      <xs:attribute name="fixedByOverride" type="xs:string" fixed="fixedValue"/>
      <xs:attribute name="defaultOverride" type="xs:string" default="newDefaultValue"/>
      <xs:attribute name="defaultOverrideByFixed" type="xs:string" fixed="fixedValue"/>
      </xs:restriction>
      </xs:complexContent>
      </xs:complexType>

      </xs:schema>

      Here is a sample data file to test this with (test.xml):

      <test xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://thebadgerset.co.uk/test-0.1 test.xsd"
      xmlns="http://thebadgerset.co.uk/test-0.1"/>

      Here is my bindings file (test.xjb):

      <?xml version="1.0" encoding="UTF-8"?>
      <bindings xmlns="http://java.sun.com/xml/ns/jaxb"
      xmlns:xsd="http://www.w3.org/2001/XMLSchema"
      xmlns:xjc="http://java.sun.com/xml/ns/jaxb/xjc"
      version="2.0">

      <bindings schemaLocation="test.xsd" node="/xsd:schema">

      <globalBindings>
      <xjc:serializable/>
      </globalBindings>

      <schemaBindings>
      <package name="uk.co.thebadgerset.xsdrestrictiontest"/>
      </schemaBindings>

      </bindings>

      </bindings>

      Here is my main method:

      public static void main(String[] args)
      {
      try
      {
      // Create an unmarshaller.
      JAXBContext jc = JAXBContext.newInstance("uk.co.thebadgerset.xsdrestrictiontest");
      Unmarshaller u = jc.createUnmarshaller();

      // Set the schema on the unmarshaller.
      SchemaFactory schemaFactory =
      SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
      Schema sedaSchema =
      schemaFactory.newSchema(Main.class.getClassLoader().getResource("test.xsd"));
      u.setSchema(sedaSchema);

      // Unmarshall the test file.
      Reader configReader = new
      InputStreamReader(Main.class.getClassLoader().getResourceAsStream("test.xml"));
      JAXBElement<? extends B> element = (JAXBElement<? extends B>)
      u.unmarshal(configReader);
      B b = element.getValue();

      // Check what values have been set for fixed and default fields.
      System.out.println("b.getFixedByOverride() = " + b.getFixedByOverride());
      System.out.println("b.getFixedInRoot() = " + b.getFixedInRoot());
      System.out.println("b.getDefaultInRoot() = " + b.getDefaultInRoot());
      System.out.println("b.getDefaultOverride() = " + b.getDefaultOverride());
      System.out.println("b.getDefaultOverrideByFixed() = " +
      b.getDefaultOverrideByFixed());
      }
      catch (Exception e)
      {
      e.printStackTrace();
      }
      }

      The schema is compiled into classes 'A' and 'B' by xjc, I'm using latest
      released version 2.1.7.

      Here is the output:

      b.getFixedByOverride() = null
      b.getFixedInRoot() = fixedValue
      b.getDefaultInRoot() = defaultValue
      b.getDefaultOverride() = null
      b.getDefaultOverrideByFixed() = defaultValue

      What you can see from this, is that the fixed and defaulted attributes in the
      base type 'A' are correctly fixed and defaulted. When I override them by
      restriction to create a new type 'B' the new fixed or defaulted values are not
      provided by the class B.

      Seems to me that the 'get*' methods in A that B alters, could override the
      methods in 'A' to provide the fixed or defaulted values that I want.

      I have attached an archive with an example for this bug to the forum topic:

      http://forums.java.net/jive/thread.jspa?threadID=42439&tstart=0

      You should be able to build and run it with:

      > mvn clean assembly:assembly
      > java -cp target/xsdrestrictiontest-0.1-SNAPSHOT-all-test-deps.jar
      uk.co.thebadgerset.xsdrestrictiontest.Main

        Activity

        Hide
        rupertlssmith added a comment -

        Created an attachment (id=256)
        The example described in the bug report.

        Show
        rupertlssmith added a comment - Created an attachment (id=256) The example described in the bug report.
        Hide
        Pavel Bucek added a comment -

        not in current scope of work, changing to ENHANCEMENT

        Show
        Pavel Bucek added a comment - not in current scope of work, changing to ENHANCEMENT
        Hide
        Thomas De Smedt added a comment -

        see https://jaxb.dev.java.net/issues/show_bug.cgi?id=683 for a workaround through
        validation

        Show
        Thomas De Smedt added a comment - see https://jaxb.dev.java.net/issues/show_bug.cgi?id=683 for a workaround through validation

          People

          • Assignee:
            jaxb-issues
            Reporter:
            rupertlssmith
          • Votes:
            3 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

            • Created:
              Updated: