4

When I use Jackson Json-Schema-Module, my schemas are generated using references that are pointed to each other.

Consider this schema:

This is a property object inside POJO1

    "myObject" : {
     "type" : "object",
     "id": "urn:jsonschema:package:myObject"
     "properties": {
          "property1" : {
             "type" : "string"
         },
          "property2" : {
             "type" : "string"
         }
    }
 }

I have the same property object inside POJO2, and when both schemas get generated, I get the following in POJO2:

  "myObject" : {
     "type" : "object",
     "$ref": "urn:jsonschema:package:myObject"
  }

But I want this property in POJO2'a schema be the same as in POJO1's schema, I don't want the reference. Can this be disabled? Or is there a workaround?

Here's the code I use:

for (Class clazz : classes) {

    ObjectMapper m = new ObjectMapper();
    SchemaFactoryWrapper visitor = new SchemaFactoryWrapper();
    m.acceptJsonFormatVisitor(m.constructType(clazz), visitor);
    JsonSchema jsonSchema = visitor.finalSchema();
    m.write(new File("json/" + clazz.getSimpleName() + ".json"), jsonSchema);

}

At first I thought that if use different ObjectMapper instances for each schema construction I would get what I want, but not the case, apparently there's some sort of cache, my knowledge of such things is not deep enough to come to a right conclusion.

Thanks!

vlashel
  • 345
  • 2
  • 15
  • I've exactly the same query... – Piyush Jajoo Oct 15 '14 at 18:55
  • I've found an answer. – vlashel Oct 16 '14 at 09:00
  • Tatu replied: Reply by Tatu Saloranta yesterday Thank you for reporting that -- it is an unfortunate bug; while instances of VisitorContext were being created properly, part of the earlier fix had missed the fact that Map was still static. So, 2.4.4 (and 2.5) will have the fix. – vlashel Oct 24 '14 at 08:00

4 Answers4

4

You can override VisitorContext

SchemaFactoryWrapper visitor = new SchemaFactoryWrapper();
visitor.setVisitorContext(new VisitorContext(){
        @Override
        public String addSeenSchemaUri(JavaType aSeenSchema) {
            return javaTypeToUrn(aSeenSchema);
        }
    });
Beno
  • 945
  • 11
  • 22
2

You can simply override hashset and set it using java reflections like this:

Field f = visitorContext.getClass().getDeclaredField("seenSchemas");
      f.setAccessible(true);
      f.set(visitorContext, dummyHashSet);
0

There is a class VisitorContext which stores references, its methods are static, so I resolved this problem by changing the source code of this class.

vlashel
  • 345
  • 2
  • 15
  • It would be great if you could tell, how to use it, I've extended from that class but unable to use the new MyVisitorContext. – Piyush Jajoo Oct 16 '14 at 16:19
  • I had to download source code of the library, and make changes inside this class itself. I didn't find any other way. You can't override static methods. – vlashel Oct 16 '14 at 18:13
-1

I think you can check this line:

m.acceptJsonFormatVisitor(m.constructType(clazz), visitor);

What kind of class is clazz? I am not sure, but instead clazz you can try something like:

myclass.class
Mabs
  • 1
  • 1