Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ObjectFactory class is being generated wrong #51

Open
thiaguten opened this issue Mar 3, 2017 · 1 comment
Open

ObjectFactory class is being generated wrong #51

thiaguten opened this issue Mar 3, 2017 · 1 comment

Comments

@thiaguten
Copy link

thiaguten commented Mar 3, 2017

Hi,

Please, I'm having trouble using the plugin (1.3.6) with Gradle 3.3 and I need help.
I had two XSD's schemas (routes.xsd and security.xsd) and after running ./gradlew xjc the ObjectFactory class that was being generated, has only some methods of only one XSD file.

routes.xsd:

<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
           targetNamespace="http://www.restnext.org/routes"
           xmlns="http://www.restnext.org/routes"
           elementFormDefault="qualified">

    <xs:element name="routes">
        <xs:annotation>
            <xs:appinfo>This schema defines a RestNEXT Route Metadata.</xs:appinfo>
            <xs:documentation source="description">
                This is the root element of the descriptor.
            </xs:documentation>
        </xs:annotation>
        <xs:complexType>
            <xs:sequence>
                <xs:element name="route" maxOccurs="unbounded">
                    <xs:annotation>
                        <xs:documentation source="description">
                            This element represents the route metadata.
                        </xs:documentation>
                    </xs:annotation>
                    <xs:complexType>
                        <xs:all>
                            <!-- comment this path element with regex validation
                            because this entry can be:
                            a path (/test),
                            a path param (/test/{name}) or
                            a path regex (/test/regex/\\d+).
                            And this regex only match as valid the entries: (path and path param).
                            <xs:element name="path">
                                <xs:annotation>
                                    <xs:documentation source="description">
                                        This element defines the route path.
                                    </xs:documentation>
                                </xs:annotation>
                                <xs:simpleType>
                                    <xs:restriction base="xs:string">
                                        <xs:pattern value="([/])(([/\w])+(/\{[\w]+\})*)*([?])?"/>
                                    </xs:restriction>
                                </xs:simpleType>
                            </xs:element>
                            -->
                            <xs:element name="path" type="xs:string">
                                <xs:annotation>
                                    <xs:documentation source="description">
                                        This element defines the route path.
                                    </xs:documentation>
                                </xs:annotation>
                            </xs:element>

                            <xs:element name="provider">
                                <xs:annotation>
                                    <xs:documentation source="description">
                                        This element defines the lambda string method reference route provider.
                                    </xs:documentation>
                                </xs:annotation>
                                <xs:simpleType>
                                    <xs:restriction base="xs:string">
                                        <xs:pattern value="([\w.])*([:]{2})(\w)+"/>
                                    </xs:restriction>
                                </xs:simpleType>
                            </xs:element>

                            <xs:element name="enable" type="xs:boolean" minOccurs="0" default="true">
                                <xs:annotation>
                                    <xs:documentation source="description">
                                        This element defines if this route path is enable or not.
                                    </xs:documentation>
                                </xs:annotation>
                            </xs:element>

                            <xs:element name="methods" minOccurs="0">
                                <xs:annotation>
                                    <xs:documentation source="description">
                                        This element defines the route allowed http methods.
                                    </xs:documentation>
                                </xs:annotation>
                                <xs:complexType>
                                    <xs:sequence>
                                        <xs:element name="method" maxOccurs="unbounded">
                                            <xs:annotation>
                                                <xs:documentation source="description">
                                                    This element defines a http method.
                                                </xs:documentation>
                                            </xs:annotation>
                                            <xs:simpleType>
                                                <xs:restriction base="xs:string">
                                                    <xs:enumeration value="GET"/>
                                                    <xs:enumeration value="POST"/>
                                                    <xs:enumeration value="PUT"/>
                                                    <xs:enumeration value="PATCH"/>
                                                    <xs:enumeration value="DELETE"/>
                                                </xs:restriction>
                                            </xs:simpleType>
                                        </xs:element>
                                    </xs:sequence>
                                </xs:complexType>
                            </xs:element>

                            <xs:element name="medias" minOccurs="0">
                                <xs:annotation>
                                    <xs:documentation source="description">
                                        This element defines the route allowed media types.
                                    </xs:documentation>
                                </xs:annotation>
                                <xs:complexType>
                                    <xs:sequence>
                                        <xs:element name="media" type="xs:string" maxOccurs="unbounded">
                                            <xs:annotation>
                                                <xs:documentation source="description">
                                                    This element defines a media type.
                                                </xs:documentation>
                                            </xs:annotation>
                                        </xs:element>
                                    </xs:sequence>
                                </xs:complexType>
                            </xs:element>

                        </xs:all>
                    </xs:complexType>
                </xs:element>
            </xs:sequence>
        </xs:complexType>
    </xs:element>

</xs:schema>
<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
           targetNamespace="http://www.restnext.org/securities"
           xmlns="http://www.restnext.org/securities"
           elementFormDefault="qualified">

    <xs:element name="securities">
        <xs:annotation>
            <xs:appinfo>This schema defines a RestNEXT Security Metadata.</xs:appinfo>
            <xs:documentation source="description">
                This is the root element of the descriptor.
            </xs:documentation>
        </xs:annotation>
        <xs:complexType>
            <xs:sequence>
                <xs:element name="security" maxOccurs="unbounded">
                    <xs:annotation>
                        <xs:documentation source="description">
                            This element represents the security metadata.
                        </xs:documentation>
                    </xs:annotation>
                    <xs:complexType>
                        <xs:all>
                            <!-- comment this path element with regex validation
                            because this entry can be:
                            a path (/test),
                            a path param (/test/{name}) or
                            a path regex (/test/regex/\\d+).
                            And this regex only match as valid the entries: (path and path param).
                            <xs:element name="path">
                                <xs:annotation>
                                    <xs:documentation source="description">
                                        This element defines the security path.
                                    </xs:documentation>
                                </xs:annotation>
                                <xs:simpleType>
                                    <xs:restriction base="xs:string">
                                        <xs:pattern value="([/])(([/\w])+(/\{[\w]+\})*)*([?])?"/>
                                    </xs:restriction>
                                </xs:simpleType>
                            </xs:element>
                            -->
                            <xs:element name="path" type="xs:string">
                                <xs:annotation>
                                    <xs:documentation source="description">
                                        This element defines the security path.
                                    </xs:documentation>
                                </xs:annotation>
                            </xs:element>

                            <xs:element name="provider">
                                <xs:annotation>
                                    <xs:documentation source="description">
                                        This element defines the lambda string method reference security provider.
                                    </xs:documentation>
                                </xs:annotation>
                                <xs:simpleType>
                                    <xs:restriction base="xs:string">
                                        <xs:pattern value="([\w.])*([:]{2})(\w)+"/>
                                    </xs:restriction>
                                </xs:simpleType>
                            </xs:element>

                            <xs:element name="enable" type="xs:boolean" minOccurs="0" default="true">
                                <xs:annotation>
                                    <xs:documentation source="description">
                                        This element defines if this security path is enable or not.
                                    </xs:documentation>
                                </xs:annotation>
                            </xs:element>

                        </xs:all>
                    </xs:complexType>
                </xs:element>
            </xs:sequence>
        </xs:complexType>
    </xs:element>

</xs:schema>

build.gradle:

description = 'restnext-core'

buildscript {
  repositories {
    jcenter()
    mavenCentral()
  }

  dependencies {
    classpath 'com.github.jacobono:gradle-jaxb-plugin:1.3.6'
  }
}

apply plugin: 'com.github.jacobono.jaxb'

dependencies {
  compile project(':restnext-util')
  compile 'io.netty:netty-all'
  compile 'org.slf4j:slf4j-api'
  testCompile 'junit:junit'
  compile 'org.javassist:javassist'
  compile 'ch.qos.logback:logback-classic'
  jaxb 'com.sun.xml.bind:jaxb-core:2.2.11'
  jaxb 'com.sun.xml.bind:jaxb-xjc:2.2.11'
  jaxb 'com.sun.xml.bind:jaxb-impl:2.2.11'
  jaxb 'javax.xml.bind:jaxb-api:2.2.12'
}

jaxb {
  xsdDir = rootProject.file('/restnext-core/src/main/resources')
  episodesDir = rootProject.file('/restnext-core/src/main/resources/META-INF')
  xjc {
    header = false
    generatePackage = 'org.restnext.core.jaxb.internal'
    args = [
        //'-no-header',
        '-enableIntrospection',
        '-npa'
    ]
  }
}

ObjectFactory class generated by (jacobono/gradle-jaxb-plugin):

package org.restnext.core.jaxb.internal;

import javax.xml.bind.annotation.XmlRegistry;


/**
 * This object contains factory methods for each 
 * Java content interface and Java element interface 
 * generated in the org.restnext.core.jaxb.internal package. 
 * <p>An ObjectFactory allows you to programatically 
 * construct new instances of the Java representation 
 * for XML content. The Java representation of XML 
 * content can consist of schema derived interfaces 
 * and classes representing the binding of schema 
 * type definitions, element declarations and model 
 * groups.  Factory methods for each of these are 
 * provided in this class.
 * 
 */
@XmlRegistry
public class ObjectFactory {


    /**
     * Create a new ObjectFactory that can be used to create new instances of schema derived classes for package: org.restnext.core.jaxb.internal
     * 
     */
    public ObjectFactory() {
    }

    /**
     * Create an instance of {@link Securities }
     * 
     */
    public Securities createSecurities() {
        return new Securities();
    }

    /**
     * Create an instance of {@link Securities.Security }
     * 
     */
    public Securities.Security createSecuritiesSecurity() {
        return new Securities.Security();
    }

}

ObjectFactory class generated by (highsource/maven-jaxb2-plugin):

package org.restnext.core.jaxb.internal;

import javax.xml.bind.annotation.XmlRegistry;


/**
 * This object contains factory methods for each 
 * Java content interface and Java element interface 
 * generated in the org.restnext.core.jaxb.internal package. 
 * <p>An ObjectFactory allows you to programatically 
 * construct new instances of the Java representation 
 * for XML content. The Java representation of XML 
 * content can consist of schema derived interfaces 
 * and classes representing the binding of schema 
 * type definitions, element declarations and model 
 * groups.  Factory methods for each of these are 
 * provided in this class.
 * 
 */
@XmlRegistry
public class ObjectFactory {


    /**
     * Create a new ObjectFactory that can be used to create new instances of schema derived classes for package: org.restnext.core.jaxb.internal
     * 
     */
    public ObjectFactory() {
    }

    /**
     * Create an instance of {@link Routes }
     * 
     */
    public Routes createRoutes() {
        return new Routes();
    }

    /**
     * Create an instance of {@link Securities }
     * 
     */
    public Securities createSecurities() {
        return new Securities();
    }

    /**
     * Create an instance of {@link Routes.Route }
     * 
     */
    public Routes.Route createRoutesRoute() {
        return new Routes.Route();
    }

    /**
     * Create an instance of {@link Securities.Security }
     * 
     */
    public Securities.Security createSecuritiesSecurity() {
        return new Securities.Security();
    }

    /**
     * Create an instance of {@link Routes.Route.Methods }
     * 
     */
    public Routes.Route.Methods createRoutesRouteMethods() {
        return new Routes.Route.Methods();
    }

    /**
     * Create an instance of {@link Routes.Route.Medias }
     * 
     */
    public Routes.Route.Medias createRoutesRouteMedias() {
        return new Routes.Route.Medias();
    }
}
@janvryck
Copy link

janvryck commented May 11, 2017

I had a similar issue and as far as I understand it, this behaviour is to be expected. You provide a package to the plugin, and it will put all generated classes in that package. Since your XSD's have elements with the same name, the existing class for routes.xsd will be overridden by those present in securities.xsd.

I was able to solve this issue by using jaxb bindings (see examples dir)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants