Can jsonschema2pojo support $ref value of "resource:/path/to/common.json"?

2,592 views
Skip to first unread message

Gary Shih

unread,
Jan 18, 2013, 3:26:26 AM1/18/13
to jsonschema...@googlegroups.com
I have a set fairly complex json schema define in which is referencing several common schema via $ref with value of "resource:/common/user.json".

I am able to use fge json-schema-validator to run through json schema validation that contains $ref withe "resource:" scheme.

Now I'm trying to run jsonschema2pojo against json schema to generate the pojo.

However, I'm getting errors where the jsonschema2pojo isn't able to find the common schema references via "resource:".

My understanding is that "resource:" will instruct the processor to try to locate the references schema from classpath. 

Does jsonschema2pojo support this?


Joe Littlejohn

unread,
Jan 18, 2013, 9:20:51 AM1/18/13
to jsonschema...@googlegroups.com
Hi Gary,

I'm afraid this isn't possible, $ref values are expected to be filesystem links at the moment.

Feel free to add an enhancement request for Classpath URL support and I'll investigate adding it for 0.3.5.

Cheers

Joe

mars...@gmail.com

unread,
Feb 4, 2013, 11:33:52 AM2/4/13
to jsonschema...@googlegroups.com
+1 on this enhancement request. 

Is that to say there is currently NO WAY of reusing schemas (and generated DTOs) from one java library to another?

Say I have a library A, which contains a json schema (A.schema) and generated classes/sources. Say I have another project/lib B, itself containing another schema. That B.schema $ref A.schema. Do I absolutely _have to_ copy A.schema from A.jar into my lib B.jar? If so, will this also regenerate A.java and A.class from A.schema in B project? Even if B.jar has a dependency to A.jar?

If that really is the case, this would be deal breaker for all the use cases I have right now. Please confirm the available options with 0.3.4 (and anticipated 0.3.5).

Joe Littlejohn

unread,
Feb 4, 2013, 3:00:36 PM2/4/13
to jsonschema...@googlegroups.com
Philippe, as I mentioned I hope to include this for 0.3.5. I know you're interested in http:// too and it may be quite simple to add both at the same time. Clearly we need to put some abstraction into the process of resolving the content of a schema document, so that it can be resolved by different providers (chosen based on the URI protocol).

Yes you're correct, there's no way to bundle up schemas into jars and read them from the classpath, as this would require 'resource:' URIs to be supported and they currently are not. If you want to re-use schemas across many projects, I presume your concern is having to duplicate or copy schemas and keep parallel version in sync. I guess you could instead:

- create a central repository of schemas, create a local mount point to this

- download/clone a set of schemas during your build so that they can be accessed during type generation

Just to be clear Philippe, there is currently no way to read a schema from the classpath (whether using $ref or not).

Now on to your question about regeneration, because I think this is critical. Clearly, you have no need or desire to repeatedly regenerate types from project A. So when jsonschema2pojo is processing schemas, it *does not* attempt to generate types if the type is already present on the classpath. Of course, A.schema must at least be accessible, otherwise there would be no way for the tool to know that the types do not need to be generated.

I don't know if this answers all of your questions or simply creates more. Hopefully the above is clear but feel free to ask more questions if you have any.

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-...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

mars...@gmail.com

unread,
Feb 5, 2013, 2:03:30 PM2/5/13
to jsonschema...@googlegroups.com
Glad to read you look forward adding this to 0.3.5. 

If I understand correctly, with 0.3.5 neither the schema file nor the DTO classes will be generated (as long as A.jar is available in B classpath). 
In the meantime I also understand that in 0.3.4, as long B as A in its classpath, all I need is to get a copy of A.schema into B project. That copy could be automated or not (e.g. I could have a build process that extracts configured json-schemas from A.jar and copy it into B/schemas folder dynamically...)

Now how does the maven generate goal of the plugin "know" the current classpath of the project (introspecting B/pom.xml dependencies?)
Thanks!

Joe Littlejohn

unread,
Feb 5, 2013, 2:28:59 PM2/5/13
to jsonschema...@googlegroups.com
Maven plugins can ask the container to build the classpath of the current project for a given scope. These links should make the dependency resolution clearer:


One final thing that you should be aware of is that where $refs are concerned, the class naming can depend on the context of the $ref. For more details see:


This could easily cause types new types to be generated for schema A, which you don't want. My recommendation would be to include the javaType extension property in your schemas:


By doing this you'll find that the output and naming is consistent no matter how or where your schemas are used, and no matter how the plugin is invoked (e.g. even if a different 'package' argument is supplied).



mars...@gmail.com

unread,
Feb 6, 2013, 7:16:01 AM2/6/13
to jsonschema...@googlegroups.com
Tested and works (classpath handling). Looking forward the .json handling in 0.3.5

Thanks for the detailed answer.

dcha...@mirthcorp.com

unread,
Nov 5, 2014, 7:32:02 PM11/5/14
to jsonschema...@googlegroups.com, mars...@gmail.com
I am now looking into this as well but unfortunately I am not making as much progress on this. I have attempted the javaType solution, but I think i am simply missing something basic. I created two maven projects A and B. My A.json is very simple
{
 
"title" : "A",
 
"description" : "A",
 
"$schema": "http://json-schema.org/draft-03/schema",
 
"id": "com.example.common.A",
 
"type": "object",  
 
"properties": {
   
"Name": {
     
"type": "string",
     
"required": true
   
}
 
}
}

In maven i run >mvn install which generates the .jar and puts it in the maven repo. I verified that the proper pojo is created for A.json so i know the jsonschema2java maven plugin is working ok.

Next i have project B with this B.json defined.

{
 
"$schema": "http:\/\/json-schema.org\/draft-04\/schema#",
 
"title": "B",
 
"description": "B",
 
"type": "object",
 
"properties": {
   
"A": {
   
"javaType" : "com.example.common.A",
 
"$ref" : "../../../../../A/src/main/resources/schema/A.json"
   
}
 
}
}

I feel like i am doing something odd there with the $ref and there has to be a better way, but that relative path does lead to the A.json.

I included the javaType there to see if that would work, bur really i get the same thing with or without it.

when i run >mvn package on my B project it ends up creating A.java and B.java. 

What i really want is simply B.java which has a reference to A which is already present in the .jar created when I installed project A.

I also will include my B project pom.xml so you can see what is in there incase i am missing something with the dependency.



<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
 
<modelVersion>4.0.0</modelVersion>
 
<groupId>B</groupId>
 
<artifactId>B</artifactId>
 
<version>0.0.1-SNAPSHOT</version>
 
<name>B</name>
 
<description>B</description>
 
<build>
 
<plugins>
 
<plugin>
           
<groupId>org.apache.maven.plugins</groupId>
           
<artifactId>maven-compiler-plugin</artifactId>
           
<version>3.1</version>
           
<configuration>
               
<source>1.7</source>
               
<target>1.7</target>
           
</configuration>
       
</plugin>
 
<plugin>
 
<groupId>org.jsonschema2pojo</groupId>
 
<artifactId>jsonschema2pojo-maven-plugin</artifactId>
 
<version>0.4.5</version>
 
<configuration>
 
<sourceDirectory>${basedir}/src/main/resources/schema</sourceDirectory>
 
<targetPackage>com.example.specific</targetPackage>
 
</configuration>
 
<executions>
 
<execution>
 
<goals>
 
<goal>generate</goal>
 
</goals>
 
</execution>
 
</executions>
 
</plugin>
 
</plugins>
 
</build>
 
<dependencies>
 
<dependency>
 
<groupId>A</groupId>
 
<artifactId>A</artifactId>
 
<version>0.0.1-SNAPSHOT</version>
 
<scope>compile</scope>
 
</dependency>
     
<dependency>
           
<groupId>commons-lang</groupId>
           
<artifactId>commons-lang</artifactId>
           
<version>2.4</version>
   
</dependency>
 
<dependency>
 
<groupId>com.fasterxml.jackson.core</groupId>
 
<artifactId>jackson-databind</artifactId>
 
<version>2.3.2</version>
 
</dependency>
 
</dependencies>
</project>

I tried to boil this down into the most simple example so maybe someone could show me where i went wrong. Admittedly i am a little new to maven so go easy on me :)


On Wednesday, February 6, 2013 7:16:01 AM UTC-5, mars...@gmail.com wrote:
Tested and works (classpath handling). Looking forward the .json handling in 0.3.5

Thanks for the detailed answer.


This message, and any documents attached hereto, may contain confidential or proprietary information intended only for the use of the addressee(s) named above or may contain information that is legally  privileged. If you are not the intended addressee, or the person responsible for delivering it to the intended addressee, you are hereby notified that reading, disseminating, distributing or copying this message is strictly prohibited. If you have received this message by mistake, please immediately notify us by replying to the message and delete the original message and any copies immediately thereafter.  Thank you for your cooperation.

Joe Littlejohn

unread,
Nov 5, 2014, 8:36:51 PM11/5/14
to jsonschema...@googlegroups.com, Philippe Marsteau

Yes, jsonschema2pojo does support resource: type URIs to read from the classpath.

Regarding refs and javaType, remember that the ref replaces THE ENTIRE content at that node, so none of the properties alongside the ref have any effect. Hopefully this makes the behavior easier for you to understand.

--
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-...@googlegroups.com.

dcha...@mirthcorp.com

unread,
Nov 5, 2014, 9:44:39 PM11/5/14
to jsonschema...@googlegroups.com, mars...@gmail.com
Thanks for the response Joe. Unfortunately I am not able to glean from that how I should modify my example. it sounds like my javaType was the wrong way to go with the ref in this case so i removed that, however after fiddling with a few things for awhile it seems no matter what I do my project B insists on creating A.java locally vs using the one from A.jar. Maybe I am not fully understanding how to configure my pom such that jsonchema2pojo sees A.class is present vs creating a new .java in my B project? Do you see anything I have done wrong specifically in what i included?
To unsubscribe from this group and stop receiving emails from it, send an email to jsonschema2pojo-users+unsub...@googlegroups.com.

dcha...@mirthcorp.com

unread,
Nov 5, 2014, 10:07:28 PM11/5/14
to jsonschema...@googlegroups.com, mars...@gmail.com, dcha...@mirthcorp.com
OK. I did try this which you hinted at with a resource path


 
"title": "B",
 
"description": "B",
 
"type": "object",
 
"properties": {
   
"A": {

 
"$ref" : "resource:/schema/A.json"
   
}
 
}
}

But alas A.java is still created in the java-gen directory in the B project. When i cracked open the .jar for A i saw a 'schema' folder with A.json in there which is how I am referencing it above. For some reason(I am not seeing it) though jsonschema2pojo isnt finding A.class in the classpath so it knows not to generate it for B...thoughts?

dcha...@mirthcorp.com

unread,
Nov 6, 2014, 7:21:39 AM11/6/14
to jsonschema...@googlegroups.com, mars...@gmail.com, dcha...@mirthcorp.com
OK. I think i got it. 

I had to change my A.json to look like 
{
 
"title" : "A",
 
"description" : "A",
 
"$schema": "http://json-schema.org/draft-03/schema",
 
"id": "com.example.common.A",
 
"type": "object",

 
"javaType":"com.example.common.A",
 
"properties": {

   
"Name": {
     
"type": "string",
     
"required": true
   
}
 
}
}


I put the javaType there. 

B.json looks like

{
 
"schema": "http://json-schema.org/draft-03/schema",
 
"title": "B",
 
"description": "B",
 
"type": "object",
 
"properties": {
   
"A": {
 
"$ref" : "resource:/schema/A.json"
   
}
 
}
}

This puts a little too much 'java' into my schemas then i would prefer, but it does seem to work. It appears to be an option. 

Joe Littlejohn

unread,
Nov 6, 2014, 5:03:56 PM11/6/14
to jsonschema...@googlegroups.com
Yes, exactly. If you imagine that the entire contents of the schema is replaced when you use a $ref, you have to put javaType inside A.json if you want it to have any effect.

I agree this makes your schemas a bit 'java', but for your own sanity I think you'll find everything works better when you have explicit javaType values defined. If you want to use $ref, then they are really essential.



To unsubscribe from this group and stop receiving emails from it, send an email to jsonschema2pojo-...@googlegroups.com.

Dan Chaffee

unread,
Nov 6, 2014, 5:16:45 PM11/6/14
to jsonschema...@googlegroups.com
perfect. thanks for the insight. 

You received this message because you are subscribed to a topic in the Google Groups "jsonschema2pojo-users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/jsonschema2pojo-users/b7QOQuIXuGE/unsubscribe.
To unsubscribe from this group and all its topics, send an email to jsonschema2pojo-...@googlegroups.com.



--

Daniel Chaffee
Sr. Software Architect, NG7

NEXTGEN HEALTHCARE
Solutions for: Ambulatory, Inpatient and Community Connectivity
12301-B Riata Trace Pkwy., Suite 200, Austin, TX 78727
(321) 271-9096 [o] 
dcha...@nextgen.com or dcha...@mirthcorp.com



NOTICE: This email and its attachments, if any, may contain confidential or proprietary information and are intended solely for authorized use by the intended recipient(s) only. Any other use of this email is prohibited. If you have received this email in error, you are hereby notified that any retention, disclosure, copying, forwarding, distribution (in whole or in part and whether electronically, written and/or orally) and/or taking of any action in reliance on this email, its contents and/or any attachments thereto is strictly prohibited. If you received this email in error, please notify the sender by replying to this message and permanently delete this email, and any attachments thereto, from your system immediately.

vaib...@gmail.com

unread,
Feb 21, 2019, 2:01:28 AM2/21/19
to jsonschema2pojo-users
$ref: resource: is not working for you.

It says> Couldn't read content from the classpath, file not found: resource:/core/document.json

in resource i have a package with core and document.json as file where definition is defined.
Mine is gradle springboot project



To unsubscribe from this group and stop receiving emails from it, send an email to jsonschema2pojo-users+unsubscri...@googlegroups.com.

This message, and any documents attached hereto, may contain confidential or proprietary information intended only for the use of the addressee(s) named above or may contain information that is legally  privileged. If you are not the intended addressee, or the person responsible for delivering it to the intended addressee, you are hereby notified that reading, disseminating, distributing or copying this message is strictly prohibited. If you have received this message by mistake, please immediately notify us by replying to the message and delete the original message and any copies immediately thereafter.  Thank you for your cooperation.

This message, and any documents attached hereto, may contain confidential or proprietary information intended only for the use of the addressee(s) named above or may contain information that is legally  privileged. If you are not the intended addressee, or the person responsible for delivering it to the intended addressee, you are hereby notified that reading, disseminating, distributing or copying this message is strictly prohibited. If you have received this message by mistake, please immediately notify us by replying to the message and delete the original message and any copies immediately thereafter.  Thank you for your cooperation.

--
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 http://groups.google.com/group/jsonschema2pojo-users.
For more options, visit https://groups.google.com/d/optout.

--
You received this message because you are subscribed to a topic in the Google Groups "jsonschema2pojo-users" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/jsonschema2pojo-users/b7QOQuIXuGE/unsubscribe.
To unsubscribe from this group and all its topics, send an email to jsonschema2pojo-users+unsub...@googlegroups.com.
--

Daniel Chaffee
Sr. Software Architect, NG7

NEXTGEN HEALTHCARE
Solutions for: Ambulatory, Inpatient and Community Connectivity
12301-B Riata Trace Pkwy., Suite 200, Austin, TX 78727
(321) 271-9096 [o] 
dcha...@nextgen.com or dcha...@mirthcorp.com



NOTICE: This email and its attachments, if any, may contain confidential or proprietary information and are intended solely for authorized use by the intended recipient(s) only. Any other use of this email is prohibited. If you have received this email in error, you are hereby notified that any retention, disclosure, copying, forwarding, distribution (in whole or in part and whether electronically, written and/or orally) and/or taking of any action in reliance on this email, its contents and/or any attachments thereto is strictly prohibited. If you received this email in error, please notify the sender by replying to this message and permanently delete this email, and any attachments thereto, from your system immediately.
Reply all
Reply to author
Forward
0 new messages