2
votes

I'm trying to use EclipseLink MOXy 2.1.2 and Java 1.6 to generate a schema based on my domain classes, following this example: http://wiki.eclipse.org/EclipseLink/Examples/MOXy/JAXB/GenerateSchema

However there is an issue with the default minOccurs value. If the annotation @XmlElement does not specify the required attribute, it should default to false, which means when generating the schema minOccurs should be 0. This works as expected with Sun's JAXB implementation. However when using Moxy minOccurs is not being generated and the default value of 1 is implied.

For example,

public Integer getDuration() {
   return duration;
}

Should have generated

<xs:element name="duration" type="xsd:int" minOccurs="0"/>

Instead if using Moxy it generates

<xsd:element name="duration" type="xsd:int"/>

which means minOccurs="1" since that is the default. Is this a bug? Is there any workaround for this?

Thanks in advance!

2

2 Answers

1
votes

Is there any chance you are on a pre-release version of EclipseLink 2.1.2? The released version of EclipseLink 2.1.2 can be obtained here:

When I run the following example:

package minoccurs;

import java.io.IOException;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.SchemaOutputResolver;
import javax.xml.transform.Result;
import javax.xml.transform.stream.StreamResult;

import org.eclipse.persistence.Version;

public class Demo {

    public static class Root {

        private Integer duration; 

        public Integer getDuration() {
            return duration;
        }

        public void setDuration(Integer duration) {
            this.duration = duration;
        }

    }

    public static class MySchemaOutputResolver extends SchemaOutputResolver {

        @Override
        public Result createOutput(String namespaceUri, String suggestedFileName) throws IOException {
            StreamResult result = new StreamResult(System.out);
            result.setSystemId(suggestedFileName);
            return result;
        }

    }

    public static void main(String[] args) throws Exception {
        JAXBContext jc = JAXBContext.newInstance(Root.class);
        System.out.println(jc);
        System.out.println(Version.getVersionString());
        jc.generateSchema(new MySchemaOutputResolver());
    }
}

I get:

org.eclipse.persistence.jaxb.JAXBContext@16cd7d5
2.1.2.v20101206-r8635
<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
   <xsd:complexType name="root">
      <xsd:sequence>
         <xsd:element name="duration" type="xsd:int" minOccurs="0"/>
      </xsd:sequence>
   </xsd:complexType>
</xsd:schema>

UPDATE #1

Based on your update, this is an issue. I have opened the following EclipseLink MOXy bug:

UPDATE #2

We have addressed this bug in the EclipseLink 2.2 stream. The fix can be obtained from the nightly downloads starting December 21st:

This fix will also eventually be included in the 2.1.3 patch.

0
votes

Thanks for your response, Blaise. The problem seems to be due to the fact that I'm trying to generate xsd:all by specifying propOrder = {}. With that annotation MOXy doesn't generate the minOccurs="0". I've modified your demo class to demonstrate the issue:

package moxy.test;

import java.io.IOException;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.SchemaOutputResolver;
import javax.xml.bind.annotation.XmlType;
import javax.xml.transform.Result;
import javax.xml.transform.stream.StreamResult;

import org.eclipse.persistence.Version;

public class Test {

    @XmlType(propOrder = {})
    public static class Root {

        private Integer duration;
        private Integer period;

        public Integer getPeriod() {
            return this.period;
        }

        public void setPeriod(Integer period) {
            this.period = period;
        }

        public Integer getDuration() {
            return duration;
        }

        public void setDuration(Integer duration) {
            this.duration = duration;
        }

    }

    public static class MySchemaOutputResolver extends SchemaOutputResolver {

        @Override
        public Result createOutput(String namespaceUri, String suggestedFileName) throws IOException {
            StreamResult result = new StreamResult(System.out);
            result.setSystemId(suggestedFileName);
            return result;
        }

    }

    public static void main(String[] args) throws Exception {
        JAXBContext jc = JAXBContext.newInstance(Root.class);
        System.out.println(jc);
        System.out.println(Version.getVersionString());
        jc.generateSchema(new MySchemaOutputResolver());
    }
}

The following is produced with MOXy. Notice there is no minOccurs attribute:

2.1.2.v20101206-r8635
<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
   <xsd:complexType name="root">
      <xsd:all>
         <xsd:element name="duration" type="xsd:int"/>
         <xsd:element name="period" type="xsd:int"/>
      </xsd:all>
   </xsd:complexType>
</xsd:schema>

If you remove the @XmlType(propOrder = {}) annotation the generated schema has the minOccurs present but with the elements as a sequence:

org.eclipse.persistence.jaxb.JAXBContext@cdedfd
2.1.2.v20101206-r8635
<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
   <xsd:complexType name="root">
      <xsd:sequence>
         <xsd:element name="duration" type="xsd:int" minOccurs="0"/>
         <xsd:element name="period" type="xsd:int" minOccurs="0"/>
      </xsd:sequence>
   </xsd:complexType>
</xsd:schema>

And just for reference, here is what is generated by Sun's JAXB reference implementation:

jar:file:/C:/Program%20Files/Java/jdk1.6.0_21/jre/lib/rt.jar!/com/sun/xml/internal/bind/v2/runtime/JAXBContextImpl.class Build-Id: 1.6.0_21
...

2.1.2.v20101206-r8635
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<xs:schema version="1.0" xmlns:xs="http://www.w3.org/2001/XMLSchema">

  <xs:complexType name="root">
    <xs:all>
      <xs:element name="duration" type="xs:int" minOccurs="0"/>
      <xs:element name="period" type="xs:int" minOccurs="0"/>
    </xs:all>
  </xs:complexType>
</xs:schema>

Thanks in advance!