Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » EclipseLink » MOXy Schema generation of xsd:restictions(Java -> Schema xsd:restriction generation)
MOXy Schema generation of xsd:restictions [message #1707431] Fri, 04 September 2015 16:44 Go to next message
J F is currently offline J FFriend
Messages: 256
Registered: July 2009
Senior Member
MOXy BeanValidator gives me the ability to add validation to my JAXB classes.

Using Moxy's XJC "Bean Validation Plugin" I can generate Bean Validation in my generated JAXB classes based on Schema restrictions or facets.

However is there any way of generating a schema with restrictions based on a java class annotated with Bean Validation annotations?

XJC has a handy plugin architecture, but so far as I can see there is no 'Java first' equivalent which I can plugin into if I wish to enhance the generated XSD with additional restrictions or facets, or even to add XML comments. Where would I start if I wanted to add this functionality to MOXy? MOXy is extremely flexible with meet in the middle mappings, can this be used during schema generation?

The jaxb-facets code seems to do what I want but evidently this is aimed at the JAXB-RI and work in this area at least seems stalled according to this java.net JIRA. So in terms of tracking a spec I guess the simplest thing would be to support Java Bean Validation Annotations -> XSD facets as a mirror image of what Moxy already does XSD facets -> Java Bean Validation Annotations. Would the Eclipselink team be open to taking in an implementation of this for MOXy?

[Updated on: Mon, 07 September 2015 16:59]

Report message to a moderator

Re: MOXy Schema generation of xsd:restictions [message #1707572 is a reply to message #1707431] Mon, 07 September 2015 12:50 Go to previous messageGo to next message
Dmitry Kornilov is currently offline Dmitry KornilovFriend
Messages: 9
Registered: June 2014
Junior Member
I think it is there. MOXy uses its own SchemaGen implementation for the process of generating XSD files from Java classes. SchemaGen was extended to automatically generate XSD Restrictions and Facets based on BV annotations found on Java classes. Since the schema generation process takes place upon creation of JAXBContext, the BV enhancement feature can be turned on/off by setting the following property (found in JAXBContextProperties) on JAXBContext:

/**
 * Property for disabling/enabling generation of XML Facets during schemagen.
 * The mapped value must be of type Boolean.
 * If it's true, then facets will be generated, based on the BV annotations.
 * If false, the BV annotations processing will be skipped during schemagen
 * and no facets will be generated.
 *
 * @since 2.6
 */
public static final String GENERATE_FACETS = "eclipselink.generate.facets";


SchemaGen recognizes annotations provided by the BV API, including @Pattern.List. If SchemaGen encounters a field annotated with both @NotNull and @XmlElement(nillable = true), it will raise the BeanValidationException.notNullAndNillable().

Sample:

Map props = new HashMap( );
props.put("eclipselink.beanvalidation.facets", true);
JAXBContext jc = JAXBContext.newInstance(classes, properties);
SchemaOutputResolver sor = new MSOR();
jc.generateSchema(sor);
Re: MOXy Schema generation of xsd:restictions [message #1707588 is a reply to message #1707572] Mon, 07 September 2015 16:41 Go to previous messageGo to next message
J F is currently offline J FFriend
Messages: 256
Registered: July 2009
Senior Member
Hi Dmitry,

Thanks for your response.

I have tried this with the attached example and cannot get it to work. From a brief look at the MOXy source for 2.6 it would appear that it is a simple case of a call to set a flag which is not being done. In order to emit facets org.eclipse.persistence.jaxb.compiler.AnnotationsProcessor would have to have facets set to true. This is a value which is derived from org.eclipse.persistence.jaxb.javamodel.Helper facets. This in turn could only be changed to true using Helper.setFacets. The only place I can see this is in the constructor for org.eclipse.persistence.jaxb.compiler.Generator(JavaModelInput) where the value is derived from the JavaModelInput param. The implementation of the JavaModelInput, JavaModelInputImpl has this set by JAXBContext
private static void JAXBContext.enableFacetsIfPropertySetTrue(JavaModelInputImpl inputImpl, Map properties) {
        Object propertyValue = properties.get(JAXBContextProperties.BEAN_VALIDATION_FACETS);
        if (propertyValue != null) inputImpl.setFacets((Boolean) propertyValue);
    }

so that ties up with what you were saying.

But org.eclipse.persistence.jaxb.compiler.Generator has several constructors which do not set up facets on the Helper, and JAXBContext seems to call these rather than Generator(JavaModelInput) .

I'll try to set up a call to Generator(JavaModelInput) directly. I tried 2.6.1-RC1 but that did not work either.
Re: MOXy Schema generation of xsd:restictions [message #1707592 is a reply to message #1707588] Mon, 07 September 2015 17:47 Go to previous messageGo to next message
J F is currently offline J FFriend
Messages: 256
Registered: July 2009
Senior Member
I downloaded and built the source altering the Generator constructors so they all called;

if (jModelInput instanceof JavaModelInputImpl) helper.setFacets(((JavaModelInputImpl) jModelInput).isFacets());


I also had to annotate the Java fields with @XmlElement;

@XmlRootElement(name="ContactInfo5")
public class ContactInfo5 {

	@Max(1000)
	@XmlElement(name="code")
	public int code;
	
	@XmlElement(name="phoneNumberit")
    @Pattern(regexp="\\(\\d{3}\\)\\d{3}-\\d{4}")
    public String phoneNumberit;
	
}



... and I am now getting facets generated for @Max. So far no luck with @Pattern but I am working on that. Simply adding the @XmlElement and running the non modified Moxy code did not emit the facets.
Re: MOXy Schema generation of xsd:restictions [message #1707647 is a reply to message #1707592] Tue, 08 September 2015 10:59 Go to previous messageGo to next message
J F is currently offline J FFriend
Messages: 256
Registered: July 2009
Senior Member
@Pattern seems to be ignored during the ultimate marshalling, albeit the schema model is set up correctly prior to calling the org.eclipse.persistence.internal.oxm.XMLMarshaller, and the descriptor for Pattern is set up in org.eclipse.persistence.internal.oxm.schema.SchemaModelProject.buildRestrictionDescriptor. org.eclipse.persistence.internal.oxm.schema.model.Restriction.getMaxLength gets called as expected in my example but not getPattern().


Is this something that is only available in 2.7?
Re: MOXy Schema generation of xsd:restictions [message #1707653 is a reply to message #1707647] Tue, 08 September 2015 11:46 Go to previous messageGo to next message
J F is currently offline J FFriend
Messages: 256
Registered: July 2009
Senior Member
Look like this was fixed in master about 5 months ago.
Running the same example with 2.7.0-SNAPSHOT from https://oss.sonatype.org/content/repositories/snapshots/ gives the following output;

<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
   <xsd:complexType name="contactInfo5">
      <xsd:sequence>
         <xsd:element name="code">
            <xsd:simpleType>
               <xsd:restriction base="xsd:int">
                  <xsd:maxInclusive value="1000"/>
               </xsd:restriction>
            </xsd:simpleType>
         </xsd:element>
         <xsd:element name="phoneNumber" minOccurs="0">
            <xsd:simpleType>
               <xsd:restriction base="xsd:string"/>
            </xsd:simpleType>
         </xsd:element>
      </xsd:sequence>
   </xsd:complexType>
   <xsd:element name="ContactInfo5" type="contactInfo5"/>
</xsd:schema>


Also I had to annotate the java fields with @XmlElement before the facets were kicked into life. @XmlAttribute, @XmlAccessorType(XmlAccessType.FIELD) made no difference.

[Updated on: Tue, 08 September 2015 12:25]

Report message to a moderator

Re: MOXy Schema generation of xsd:restictions [message #1707667 is a reply to message #1707653] Tue, 08 September 2015 12:32 Go to previous messageGo to next message
J F is currently offline J FFriend
Messages: 256
Registered: July 2009
Senior Member
As you mentioned @Pattern.List does work, so now I have a work around.

package uk.co.his.test;

import javax.validation.constraints.Max;
import javax.validation.constraints.Pattern;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement(name="ContactInfo5")
@XmlAccessorType(XmlAccessType.FIELD)
public class ContactInfo5 {

	@Max(1000)
	@XmlElement
	public int code;

	@XmlElement
    @Pattern(regexp="[0-9]*")
    public String phoneNumber;
	
	@XmlElement
    @Pattern.List(value = { @Pattern(regexp="[0-9]*") })
    public String phoneNumber2;
	
}


gives

<?xml version="1.0" encoding="UTF-8"?>
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
   <xsd:complexType name="contactInfo5">
      <xsd:sequence>
         <xsd:element name="code">
            <xsd:simpleType>
               <xsd:restriction base="xsd:int">
                  <xsd:maxInclusive value="1000"/>
               </xsd:restriction>
            </xsd:simpleType>
         </xsd:element>
         <xsd:element name="phoneNumber" minOccurs="0">
            <xsd:simpleType>
               <xsd:restriction base="xsd:string"/>
            </xsd:simpleType>
         </xsd:element>
         <xsd:element name="phoneNumber2" minOccurs="0">
            <xsd:simpleType>
               <xsd:restriction base="xsd:string">
                  <xsd:pattern value="[0-9]*"/>
               </xsd:restriction>
            </xsd:simpleType>
         </xsd:element>
      </xsd:sequence>
   </xsd:complexType>
   <xsd:element name="ContactInfo5" type="contactInfo5"/>
</xsd:schema>
Re: MOXy Schema generation of xsd:restictions [message #1707845 is a reply to message #1707667] Wed, 09 September 2015 16:34 Go to previous messageGo to next message
Dmitry Kornilov is currently offline Dmitry KornilovFriend
Messages: 9
Registered: June 2014
Junior Member
I see in the code that facets generation is invoked only on elements annotated with @XmlElement. Currently @XmlAttribute and @XmlAccessorType(XmlAccessType.FIELD) are not supported.
There is a handler for @Pattern annotation in the code. It's surprising that it doesn't work. I'll take a look at your code possibly this week and find out why it doesn't work.

[Updated on: Wed, 09 September 2015 16:37]

Report message to a moderator

Re: MOXy Schema generation of xsd:restictions [message #1708028 is a reply to message #1707845] Fri, 11 September 2015 14:25 Go to previous messageGo to next message
Dmitry Kornilov is currently offline Dmitry KornilovFriend
Messages: 9
Registered: June 2014
Junior Member
I confirm a bug with @Pattern annotation processing.
A new bugzilla issue is created: https://bugs.eclipse.org/bugs/show_bug.cgi?id=477203

[Updated on: Fri, 11 September 2015 14:26]

Report message to a moderator

Re: MOXy Schema generation of xsd:restictions [message #1709575 is a reply to message #1707431] Tue, 29 September 2015 10:59 Go to previous messageGo to next message
Marcel Valovy is currently offline Marcel ValovyFriend
Messages: 1
Registered: September 2015
Junior Member
Hi J F,

If a field is not annotated with @XmlElement or @XmlAttribute, then Schemagen cannot recognize such fields (not BV in JAXB bug).

About Xml Attributes... EclipseLink 2.6 does not currently support that feature. The reason is that our Schemagen inlines any found Xml Attributes into their parent element's definition. It is not possible to define XML Restrictions/Facets for inlined attributes.

For a future release, it could be supported, and I filed a request for enhancement:
https://bugs.eclipse.org/bugs/show_bug.cgi?id=478616

Thanks for the input.

Marcel
Re: MOXy Schema generation of xsd:restictions [message #1711157 is a reply to message #1709575] Tue, 13 October 2015 15:03 Go to previous messageGo to next message
J F is currently offline J FFriend
Messages: 256
Registered: July 2009
Senior Member
Thank you, not forgetting Dimitry!

[Updated on: Tue, 13 October 2015 15:04]

Report message to a moderator

Re: MOXy Schema generation of xsd:restictions [message #1712290 is a reply to message #1707667] Thu, 22 October 2015 16:41 Go to previous messageGo to next message
Sanjeev Singh is currently offline Sanjeev SinghFriend
Messages: 1
Registered: October 2015
Junior Member
I tried to generate the xsd with restrictions but it didn't go through.

Do we have a sample code to generate the same ?
Re: MOXy Schema generation of xsd:restictions [message #1712349 is a reply to message #1712290] Fri, 23 October 2015 09:04 Go to previous message
J F is currently offline J FFriend
Messages: 256
Registered: July 2009
Senior Member
This Maven example should give you a good idea in conjunction with the above discussion.
Previous Topic:Nested Object initialization
Next Topic:count queries with specification
Goto Forum:
  


Current Time: Thu Mar 28 14:53:27 GMT 2024

Powered by FUDForum. Page generated in 0.03604 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top