2

How to remove the id field ("id" : "urn:jsonschema:org:gradle:Person") from JSON schema created using Jackson?

Generated Schema

{
  "type" : "object",
  "id" : "urn:jsonschema:org:gradle:Person",
  "properties" : {
    "name" : {
      "type" : "string"
    }
  }
}

For POJO class (Person.class)

import com.fasterxml.jackson.annotation.JsonProperty;

public class Person {

    @JsonProperty("name")
    private String name;

} 

Using JSON Schema Generator

import java.io.IOException;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.module.jsonSchema.JsonSchema;
import com.fasterxml.jackson.module.jsonSchema.JsonSchemaGenerator;


public final class GetJsonSchema {
    public static String getJsonSchema2(Class clazz) throws IOException {
        ObjectMapper mapper = new ObjectMapper();
        JsonSchemaGenerator jsonSchemaGenerator = new JsonSchemaGenerator(mapper);
        JsonSchema jsonSchema = jsonSchemaGenerator.generateSchema(clazz);
        return mapper.writerWithDefaultPrettyPrinter().writeValueAsString(jsonSchema);
    }
}

Invoked like

System.out.println(JsonSchema.Create(Person.class));
Vic
  • 1,925
  • 1
  • 18
  • 29

2 Answers2

2

Just set id to null. E.g.:

jsonSchema.setId(null);
Sachin Gupta
  • 7,305
  • 4
  • 28
  • 44
  • This works well when the POJO is simples, what if the POJO is complex with inner objects? – v3nM Dec 05 '17 at 19:01
2

As sachin said, jsonSchema.setId(null) is a good way to accomplish your goal. But Venkat is right in that complex types will still have the id's.

One way to remove them is to use a custom SchemaFactoryWrapper, which will instantiate its own visitorContext which will refuse to provide a URN. However, it's important to note this won't work if one type refers to itself (for example, a status object that might have children status objects).

For example:

private static class IgnoreURNSchemaFactoryWrapper extends SchemaFactoryWrapper {
    public IgnoreURNSchemaFactoryWrapper() {
        this(null, new WrapperFactory());
    }

    public IgnoreURNSchemaFactoryWrapper(SerializerProvider p) {
        this(p, new WrapperFactory());
    }

    protected IgnoreURNSchemaFactoryWrapper(WrapperFactory wrapperFactory) {
        this(null, wrapperFactory);
    }

    public IgnoreURNSchemaFactoryWrapper(SerializerProvider p, WrapperFactory wrapperFactory) {
        super(p, wrapperFactory);
        visitorContext = new VisitorContext() {
            public String javaTypeToUrn(JavaType jt) {
                return null;
            }
        };
    }
}

private static final String printSchema(Class c) {
    try {
        ObjectMapper mapper = new ObjectMapper();
        IgnoreURNSchemaFactoryWrapper visitor = new IgnoreURNSchemaFactoryWrapper();
        mapper.acceptJsonFormatVisitor(c, visitor);
        JsonSchema schema = visitor.finalSchema();
        schema.setId(null);
        ObjectWriter writer = mapper.writerWithDefaultPrettyPrinter();
        String asString = writer.writeValueAsString(schema);
        return asString;
    } catch(Exception e) {
        e.printStackTrace();
        return null;
    }
}
Jan B.
  • 5,464
  • 5
  • 27
  • 47