jaxb
  1. jaxb
  2. JAXB-797

Unmarshalling issue when using abstract element and substitution group

    Details

    • Type: Bug Bug
    • Status: Open
    • Priority: Minor Minor
    • Resolution: Unresolved
    • Affects Version/s: 2.1.10
    • Fix Version/s: None
    • Component/s: None
    • Labels:
      None
    • Environment:

      JDK 1.6.0_23, Windows XP.

      Description

      Given the following schema:

      <?xml version="1.0" encoding="UTF-8"?>
      <schema xmlns="http://www.w3.org/2001/XMLSchema" targetNamespace="http://org.mitre.jaxbtest"
      xmlns:tns="http://org.mitre.jaxbtest">
      <element abstract="true" name="fruit" type="tns:Fruit"/>
      <element name="apple" substitutionGroup="tns:fruit" type="tns:Apple"/>
      <element name="banana" substitutionGroup="tns:fruit" type="tns:Banana"/>
      <element name="fruitBowl" type="tns:FruitBowl"/>
      <element name="fruitSalad" type="tns:FruitSalad"/>
      <complexType abstract="true" name="Fruit">
      <attribute name="name" type="string"/>
      </complexType>
      <complexType name="Apple">
      <complexContent>
      <extension base="tns:Fruit"/>
      </complexContent>
      </complexType>
      <complexType name="Banana">
      <complexContent>
      <extension base="tns:Fruit"/>
      </complexContent>
      </complexType>
      <complexType name="FruitBowl">
      <sequence>
      <element ref="tns:fruit"/>
      <element ref="tns:apple"/>
      </sequence>
      </complexType>
      </schema>

      And the following XML instance document:

      <?xml version="1.0" encoding="UTF-8"?>
      <fruitBowl xmlns="http://org.mitre.jaxbtest">
      <banana name="banana" />
      <apple name="apple"/>
      </fruitBowl>

      After running xjc on the schema, the following unit test fails. Given the FruitBowl instance returned by unmarshal(), the getFruit().getValue() call should return a Fruit object of type Banana, and the getApple() call should return an Apple instance. The actual result is that the getFruit().getValue() call returns an instance of Apple, and the getApple() call returns null. It appears that JAXB incorrectly assigns the <apple> element to the <fruit> substitution group specified by the schema, and ignores the <banana> entirely, whereas the schema clearly specifies that the first element in the sequence is a member of the <fruit> substitution group, and the second element is an <apple>.

      Unit Test:

      @Test
      public void testFruitBowl() throws Exception

      { Unmarshaller unmarshaller = JAXBContext.newInstance("org.mitre.jaxbtest").createUnmarshaller(); FruitBowl bowl = (FruitBowl) ((JAXBElement)unmarshaller.unmarshal(ClassLoader.getSystemClassLoader().getResource("org/mitre/jaxbtest/FruitBowl.xml"))).getValue(); Apple apple = bowl.getApple(); assertNotNull(apple); // FAILS assertEquals("apple", apple.getName()); Fruit fruit = bowl.getFruit().getValue(); assertEquals("banana", fruit.getName()); }

        Activity

        Hide
        jkranes added a comment -

        Attaching Maven project with source files and unit tests to demonstrate this bug.

        Show
        jkranes added a comment - Attaching Maven project with source files and unit tests to demonstrate this bug.
        Hide
        Yoel_B added a comment -

        I've the same problem using JAXB 2.2.4.

        Show
        Yoel_B added a comment - I've the same problem using JAXB 2.2.4.

          People

          • Assignee:
            Iaroslav Savytskyi
            Reporter:
            jkranes
          • Votes:
            3 Vote for this issue
            Watchers:
            3 Start watching this issue

            Dates

            • Created:
              Updated: