Skip to content

Instantly share code, notes, and snippets.

@dnault
Last active August 8, 2022 13:26
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save dnault/3b2f3b14be8bba76da81 to your computer and use it in GitHub Desktop.
Save dnault/3b2f3b14be8bba76da81 to your computer and use it in GitHub Desktop.
Jackson 2.5+ sample code for generating JSON schemas that always use schema references instead of inline schemas
package com.github.therapi.apidoc;
import java.util.List;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.module.jsonSchema.JsonSchema;
import com.fasterxml.jackson.module.jsonSchema.factories.SchemaFactoryWrapper;
import com.fasterxml.jackson.module.jsonSchema.factories.VisitorContext;
public class JsonSchemaExample {
/**
* Treat all schemas as "seen" so that model schemas are never inlined.
*/
public static class VisitorContextWithoutSchemaInlining extends VisitorContext {
@Override
public String addSeenSchemaUri(JavaType aSeenSchema) {
return getSeenSchemaUri(aSeenSchema);
}
@Override
public String getSeenSchemaUri(JavaType aSeenSchema) {
return isModel(aSeenSchema) ? javaTypeToUrn(aSeenSchema) : null;
}
protected boolean isModel(JavaType type) {
return type.getRawClass() != String.class
&& !isBoxedPrimitive(type)
&& !type.isPrimitive()
&& !type.isMapLikeType()
&& !type.isCollectionLikeType();
}
protected static boolean isBoxedPrimitive(JavaType type) {
return type.getRawClass() == Boolean.class
|| type.getRawClass() == Byte.class
|| type.getRawClass() == Long.class
|| type.getRawClass() == Integer.class
|| type.getRawClass() == Short.class
|| type.getRawClass() == Float.class
|| type.getRawClass() == Double.class;
}
}
public static class Animal {
public String species;
}
public static class Zoo {
public String name;
public List<Animal> animals;
}
public static void main(String[] args) throws Exception {
ObjectMapper m = new ObjectMapper();
Class generateSchemaFor = Zoo.class;
SchemaFactoryWrapper visitor = new SchemaFactoryWrapper();
visitor.setVisitorContext(new VisitorContextWithoutSchemaInlining());
m.acceptJsonFormatVisitor(m.constructType(generateSchemaFor), visitor);
JsonSchema jsonSchema = visitor.finalSchema();
System.out.println(m.writerWithDefaultPrettyPrinter().writeValueAsString(jsonSchema));
}
}
@tsz662
Copy link

tsz662 commented Apr 6, 2016

Thanks for this gist. It really saved my time!!
As a side note, in my case, I added !type.isArrayType() at the end of the `isModel' method.

protected boolean isModel(JavaType type) {
            return type.getRawClass() != String.class
                    && !isBoxedPrimitive(type)
                    && !type.isPrimitive()
                    && !type.isMapLikeType()
                    && !type.isCollectionLikeType()
                    && !type.isArrayType();
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment