6

I'm sure I'm just being dense here.

I want to take an object schema, and turn it into a string representation.

Like so, but this returns null:

JsonSchemaGenerator generator = new JsonSchemaGenerator(MAPPER);
JsonSchema jsonSchema = generator.generateSchema(Get.class);
System.out.println("jsonSchema: " + jsonSchema.asObjectSchema().asStringSchema());

This is using com.fasterxml.jackson.module.jsonSchema.JsonSchema, found at https://github.com/FasterXML/jackson-module-jsonSchema/wiki

jruizaranguren
  • 12,679
  • 7
  • 55
  • 73
mtyson
  • 8,196
  • 16
  • 66
  • 106

2 Answers2

10

you can achieve that by doing:

ObjectMapper mapper = new ObjectMapper();
SchemaFactoryWrapper visitor = new SchemaFactoryWrapper();
mapper.acceptJsonFormatVisitor(mapper.constructType(YOURCLASS.class), visitor);
JsonSchema jsonSchema = visitor.finalSchema();
String schemaString = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(jsonSchema);
Thiago Burgos
  • 947
  • 2
  • 13
  • 17
1

Easily done:

m.writeValueAsString(jsonSchema);

Essentially, using Jackson to marshal the schema object into JSON.

mtyson
  • 8,196
  • 16
  • 66
  • 106