jaxb
  1. jaxb
  2. JAXB-357

Inconsistency in code generation for IDREFS caused by collectionType option in globalBindings

    Details

    • Type: Bug Bug
    • Status: Open
    • Priority: Major Major
    • Resolution: Unresolved
    • Affects Version/s: 2.1.3
    • Fix Version/s: not determined
    • Component/s: spec
    • Labels:
      None
    • Environment:

      Operating System: All
      Platform: All

    • Issuezilla Id:
      357

      Description

      Inconsistency in code generation for IDREFS caused by collectionType option in
      globalBindings:

      If collectionType option is included in xjb

      <jxb:globalBindings collectionType="java.util.LinkedList"

      then generated code will look like:

      //snip...
      @XmlAttribute(namespace = "http://niem.gov/niem/structures/1.0")
      @XmlIDREF
      @XmlSchemaType(name = "IDREFS")
      protected List<Object> linkMetadata = new LinkedList<Object>();
      //snip...
      public List<Object> getMetadata() {
      if (metadata == null)

      { metadata = new LinkedList<Object>(); }

      return this.metadata;
      }

      If collectionType is not specified then code will default to ArrayList and will
      look like this:

      //snip...
      @XmlAttribute(namespace = "http://niem.gov/niem/structures/1.0")
      @XmlIDREF
      @XmlSchemaType(name = "IDREFS")
      protected List<Object> linkMetadata;
      //snip...
      public List<Object> getLinkMetadata() {
      if (linkMetadata == null)

      { linkMetadata = new ArrayList<Object>(); }

      return this.linkMetadata;
      }
      //snip...

      The problem is in the following code:
      protected List<Object> linkMetadata = new LinkedList<Object>();
      While this looks correct from the first glance in reality it causes the problem
      for IDREFS:
      if in the original xml element/attribute does not exist it is initialized
      anyway and upon marshalling will result in empty value string - "" which is
      invalid according to XML schema:

      <!-- snip -->
      <attribute name="linkMetadata" type="IDREFS"/>
      <complexType name="ComplexObjectType">
      <attribute ref="s:linkMetadata"/>
      </complexType>
      <element name="foo" type="s:ComplexObjectType"/>
      <!-- snip -->

      Original xml:
      <foo />

      Output xml:
      <foo s:linkMetadata="" />

      s:linkMetadata="" where s:linkMetadata is of IDREFS is invalid according to XML
      schema.

        Activity

        Hide
        kohsuke added a comment -

        I guess this is a spec bug, in the sense that these two features interact in an
        unexpected way.

        I don't see how we can make this work, so most likely the resolution would be to
        prevent collectionType from taking effect on IDREFS, or something along that line.

        Show
        kohsuke added a comment - I guess this is a spec bug, in the sense that these two features interact in an unexpected way. I don't see how we can make this work, so most likely the resolution would be to prevent collectionType from taking effect on IDREFS, or something along that line.
        Hide
        basdebakker14 added a comment -

        The same issue exists with attributes of type NMTOKENS.

        Show
        basdebakker14 added a comment - The same issue exists with attributes of type NMTOKENS.
        Hide
        gb96 added a comment -

        This problem actually has nothing to do with the collectionType option!

        Invalid empty attribute string "" is generated regardless of collectionType.

        You can fix the problem by detecting the empty collections and replacing them with nulls prior to or during marshalling, however you need to use either Reflection or a Collection-Setter-Injector XJC plugin to be able to set a collection field value to null.

        Show
        gb96 added a comment - This problem actually has nothing to do with the collectionType option! Invalid empty attribute string "" is generated regardless of collectionType. You can fix the problem by detecting the empty collections and replacing them with nulls prior to or during marshalling, however you need to use either Reflection or a Collection-Setter-Injector XJC plugin to be able to set a collection field value to null.

          People

          • Assignee:
            Martin Grebac
            Reporter:
            bshrom
          • Votes:
            2 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

            • Created:
              Updated: