Search code examples
javajsonjsonschemajackson-modules

Jackson: json schema references other schemas, can this be disabled?


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!


Solution

  • 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);