Skip to main content

@XmlElementRef(required=false) Doesn't Generate minOccurs="0"

1 reply [Last post]
jenglert
Offline
Joined: 2010-06-09
Points: 0

I already logged an issue (https://jaxb.dev.java.net/issues/show_bug.cgi?id=759) but haven't had a response yet so thought I'd post here to to see if anyone has found a work around.

The @XmlElementRef.required() attribute was added in bug 389. I cannot get
@XmlElementRef(required=false) to generate minOccurs="0" for and element with
schemagen. Here is a sample JUnit test that demonstrates the issue:

import java.io.*;

import javax.xml.bind.*;
import javax.xml.bind.annotation.*;

import org.junit.*;

public class SchemagenXmlElementRefRequiredBug {

@XmlRootElement
private static final class ReferencedClass {

private ReferencedClass() {}

@XmlElement
public int mInt;
}

@XmlRootElement
private static final class ClassWithReference {

private ClassWithReference() {}

@XmlElementRef(required=false)
public ReferencedClass mNotRequiredRef;

@XmlElementRef(required=true)
public ReferencedClass mRequiredRef;
}

@Test
public void SchemagenToConsole() throws Exception {
writeXSDToConsole(JAXBContext.newInstance(ClassWithReference.class));
}

private static void writeXSDToConsole(final JAXBContext context)
throws IOException {
final ByteArrayOutputStream out = new ByteArrayOutputStream();

SchemaGenerator.generateSchema(context, out);

System.out.println(out.toString());
}
}

Where SchemaGenerator.generateSchema(context, out) uses
JAXBContext.generateSchema(SchemaOutputResolver) to write the XSD to an
OutputStream. I omitted that code for brevity. The above generates the following
XSD:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>

------- Additional comments from jenglert Mon Jun 7 22:20:11 +0000 2010 -------

Environment:

Java 1.6.0u18
JAXB 2.2.1 added with "-Xbootclasspath/p:lib/jaxb-util.jar"

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
jenglert
Offline
Joined: 2010-06-09
Points: 0

Just following up with the solution, also noted here:

https://jaxb.dev.java.net/issues/show_bug.cgi?id=759

Basically, I forgot to add jaxb-impl.jar to the classpath. So, while the API was correct by adding jaxb-api.jar to the bootclasspath the underlying implementation found in jaxb-impl.jar was missing. Adding jaxb-impl.jar to the classpath was missing from the docs but I suppose could be considered common sense.