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,985
  • 1
  • 19
  • 30

3 Answers3

2

Just set id to null. E.g.:

jsonSchema.setId(null);
Sachin Gupta
  • 7,805
  • 4
  • 30
  • 45
  • 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.
  • 6,030
  • 5
  • 32
  • 53
0

To remove all id in complex types you can:

public static void removeId(JsonSchema schema) {
    schema.setId(null);
    if (JsonFormatTypes.OBJECT.equals(schema.getType())) {
        schema.asObjectSchema().getProperties().forEach((key, value) -> removeId(value));
    } else if (JsonFormatTypes.ARRAY.equals(schema.getType())) {
        final ArraySchema.Items items = schema.asArraySchema().getItems();
        if (items.isArrayItems()) {
            Stream.of(items.asArrayItems().getJsonSchemas()).forEach(s -> removeId(s));
        } else {
            removeId(items.asSingleItems().getSchema());
        }
    }
}

PS: Objects can also have additionalProperties. Maybe you will need to remove their id.