2

I'm attempting to use Jackson to generate JSON schemas from POJOs. Jackson has two ways to do this. This is the first:

ObjectMapper mapper = new ObjectMapper();
JsonSchema schema = mapper.generateJsonSchema(Entity.class);
String schemaString = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(schema);

This is fine - it generates exactly what you'd expect.

{
  "type" : "object",
    "properties" : {
    "pedigreeIds" : {
      "type" : "array",
      "items" : {
        "type" : "string"
      }
    },
    ...
  }
}

However, this approach is deprecated in favor of the second, which uses jackson-module-jsonschema:

ObjectMapper mapper = new ObjectMapper();
SchemaFactoryWrapper visitor = new SchemaFactoryWrapper();
mapper.acceptJsonFormatVisitor(mapper.constructType(Entity.class), visitor);
JsonSchema schema = visitor.finalSchema();
String schemaString = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(schema);

Using this code spits out the following for the same entity class:

{
  "$ref" : null,
  "$schema" : null,
  "disallow" : null,
  "id" : null,
  "required" : null,
  "description" : null,
  "title" : null,
  "enums" : [ ],
  "additionalProperties" : null,
  "dependencies" : [ ],
  "patternProperties" : { },
  "properties" : {
    "observationTime" : {
      "$ref" : null,
      "$schema" : null,
      "disallow" : null,
      ...
    }
  }
}

Given that the first method is deprecated, I'd prefer to use the second if possible. I can't find any differences between what I'm doing and what various code snippets on the internet are doing.

Is there a configuration switch I'm missing, or something I'm simply doing wrong in the second example that causes Jackson to not generate the expected schema?

jruizaranguren
  • 12,679
  • 7
  • 55
  • 73
Dave
  • 211
  • 3
  • 5
  • what version of jackson-mapper-asl are you using ?? – Ashish Jan 09 '14 at 15:48
  • All Jackson components are version 2.2.3. – Dave Jan 09 '14 at 16:03
  • That's strange. It works for me in this version. Could you also try with this line `mapper.acceptJsonFormatVisitor(Entity.class, visitor)`? See also this question: http://stackoverflow.com/questions/17783909/create-json-schema-from-java-class/17786708 – Michał Ziober Jan 09 '14 at 22:27
  • Yep, I'd tried that as well - that was actually one of the references I found while trying to see if anyone else had run into this issue. It doesn't change the returned data. – Dave Jan 10 '14 at 16:25
  • Clarify please how you got your output in the second example? It seems you have printed something other than `schemaString` content. – Eugene Evdokimov Jan 14 '14 at 07:00
  • Both results are generated the same way, using `System.out.println(schemaString)` on the server side - that line of code did not change. The returned data was verified by hitting a Jersey endpoint which contains the code in question, then examining the returned JSON string on the calling client. – Dave Jan 14 '14 at 21:14

0 Answers0