Extending from another generated POJO (or existing Java class) when Parent is not in same project

2,567 views
Skip to first unread message

Anthony Capozzi

unread,
Sep 19, 2016, 8:19:33 AM9/19/16
to jsonschema2pojo-users
Hi,

Apologies if this is a duplicate, but I've poked around a bit and couldn't find an answer to my question anywhere.

I have two projects that are part of a multiple project build, e.g. ClientProject and ServiceProject.

In the client project, I'm generating a POJO, e.g. ClientPojo.java.

In the service project, I'm generating a POJO, e.g. ServicePojo.java, that I would like to extend ClientPojo.java.

The client project is configured with a dependency on the client project, e.g.:

dependencies{
 compile project
(':ClientProject')
}

My preference is to have the JSON schema for ServicePojo contain a reference to the ClientProject JSON schema, e.g.:

{

    "type": "object",
    "extends": {
        "$ref": "<what do i put here?>/ClientPojo.json"
    },
    "additionalProperties": false,
    "properties": {
        "newproperty1":                { "type": "number" },
        "newproperty2":                    { "type": "string" },
        "newproperty3":                   { "type": "string" },
        "newproperty4":                     { "type": "string" }
    }
}

However, I had a hard time getting the generator to discover the parent schema. At first, I tried setting "$ref" to "classpath:" and "resource:", but didn't have any luck.

I was finally successful in getting the parent schema file discovered by adding the client projects JSON schema resources to the service projects source dirs, e.g.:

sourceSets {
    main {
        resources {
            srcDir "../ClientProject/src/main/resources/jsonmodel"
        }
    }
}

and then modifying the "$ref" in the ClientPojo.json to use relative pathing to find the parent JSON schema file:

{

    "type": "object",
    "extends": {
        "$ref": "../ClientPojo.json"
    },
    "additionalProperties": false,
    "properties": {
        "newproperty1":                { "type": "number" },
        "newproperty2":                    { "type": "string" },
        "newproperty3":                   { "type": "string" },
        "newproperty4":                     { "type": "string" }
    }
}

With this approach, the ServicePojo.java generated successfully. However, another class called ServicePojoParent.java was also generated, and the ServicePojo.java class extends this ServicePojoParent.java class, rather than generated Java class in the Client project.

It may be simpler if I can just specify the specific Java class that I would like the generated ServicePojo.java to extend. However, I wasn't able to find clear documentation on how to do this, so I'm not sure if it's possible.

Does anyone have any thoughts?

Thanks,
Tony

Anthony Capozzi

unread,
Sep 19, 2016, 9:58:19 AM9/19/16
to jsonschema2pojo-users
Hi,

I think I posted too soon! I searched around a bit more and found reference to the "javaType" to specify the Java class for direct extension. I modified the ServicePojo.json schema definition as follows, and the expected extension is added:

{

    "type": "object",
    "extends": {
        "type": "object",
        "javaType": "ClientPojo"
    },
    "additionalProperties": false,
    "properties": {
        "newproperty1":                { "type": "number" },
        "newproperty2":                    { "type": "string" },
        "newproperty3":                   { "type": "string" },
        "newproperty4":                     { "type": "string" }
    }
}

The generator does to include the appropriate "import" statement, though, and the Gradle build fails with a missing symbol error.

I modified the schema definition to specify the fully qualified name of the ClientPojo:

{

    "type": "object",
    "extends": {
        "type": "object",
        "javaType": "test.package.ClientPojo"
    },
    "additionalProperties": false,
    "properties": {
        "newproperty1":                { "type": "number" },
        "newproperty2":                    { "type": "string" },
        "newproperty3":                   { "type": "string" },
        "newproperty4":                     { "type": "string" }
    }
}

After regenerating, a new package was added to ServiceProject - "test.package" - containing a new class named "ClientPojo".

Is it possible to get the generator to insert the appropriate import statement for the extension type, without having the duplicate parent type also generated?

Thanks,
Tony

Joe Littlejohn

unread,
Sep 19, 2016, 10:24:35 AM9/19/16
to jsonschema...@googlegroups.com
Hi Tony

If you point to another class that is on the classpath already, then no new Java class will be generated. So in this case, make sure test.package.ClientPojo is on the classpath of the project when you are generating your server type.

Cheers

--
You received this message because you are subscribed to the Google Groups "jsonschema2pojo-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jsonschema2pojo-users+unsub...@googlegroups.com.
Visit this group at https://groups.google.com/group/jsonschema2pojo-users.
For more options, visit https://groups.google.com/d/optout.

Anthony Capozzi

unread,
Sep 19, 2016, 11:27:36 AM9/19/16
to jsonschema2pojo-users
Thanks for the reply Joe.

Yes, I believe that the generated POJOs from the Client project are on the class path for a couple of reasons.

First, the Client POJOs are being generated by jsonschema2pojo, and I believe that plugin is making sure that the build/generated-sources folder is on the class path, correct?

Second, Java code that I've written that also resides in the Service project references the generated POJOs that are stored within the Client project's build/generated-sources folder. When the Gradle build executes, I'm not seeing any issues reconciling the imports for those POJOs.

Is there any other way to verify that the jsonschema2pojo plugin is definitely not finding the Client POJOs on the class path, when the generator is run against schema files stored in the Service project?

Am I correct in assuming that if Gradle can locate the Client pojos when building the Service project (due to the inclusion of the project(":ClientProject") dependency, that the pojo generator should also find them?

Thanks,
Tony

Joe Littlejohn

unread,
Sep 19, 2016, 12:22:26 PM9/19/16
to jsonschema...@googlegroups.com
Hmm, in Maven world we do this:


This may not be happening in the Gradle plugin - although I have no idea if it's even required :) I'm not a Gradle user.

Please feel free to raise an issue (Classes from project dependencies are not used by Gradle plugin). Hopefully a helpful Gradle user can comment on this.

Cheers



To unsubscribe from this group and stop receiving emails from it, send an email to jsonschema2pojo-users+unsubscri...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages