mtyson
mtyson

Reputation: 8570

Jackson JsonSchemaGenerator - How to get Schema as String

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

Upvotes: 7

Views: 7571

Answers (2)

Thiago Burgos
Thiago Burgos

Reputation: 1017

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

Upvotes: 11

mtyson
mtyson

Reputation: 8570

Easily done:

m.writeValueAsString(jsonSchema);

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

Upvotes: 1

Related Questions