[jackson-module-jsonSchema] How to add Custom Property for Json Schema

1,051 views
Skip to first unread message

Sathish Kumar Thiyagarajan

unread,
Jun 10, 2014, 3:43:23 AM6/10/14
to jackso...@googlegroups.com
How do we add custom property to JSON Schema ? For Eg, Consider below use case

Every Entity should have a property which will be used as label while displaying in the UI. ( E.g For Employee schema, 'name' is used as LABEL. for Movie schema, 'title' is used as LABEL

If we have a field like 

private String title;

with Normal case, This will be converted as 

"title":{"type":"string"}

Now if we mark the title field with a custom annotation like 

@Label
private String title;

Expected result is to generate a property schema as

"title":{"type":"string","label":true}

I tried doing the same as given below

     /**
     * adds custom property to JSON Schema like "label":true
     *
     * @param type
     * @param propertySchema
     */
    private void addCustomProperty(String name, Object value, JsonSchema propertySchema) {
        Map<String, Object> objectAsMap = objectMapper.convertValue(propertySchema, Map.class);
        objectAsMap.put(name, value);
        propertySchema = objectMapper.convertValue(objectAsMap, JsonSchema.class);
    }
 

    /**
     * navigate Java Class and add Custom Annotations to JSON Schema
     *
     * @param type
     * @param jsonSchema
     */
    private void addLabelFields(Class type, JsonSchema jsonSchema) {
        JsonSchema propertySchema;
        for (Field field : type.getDeclaredFields()) {
            if (field.isAnnotationPresent(Label.class)) {
                propertySchema = jsonSchema.asObjectSchema().getProperties().get(field.getName());
                addCustomProperty("label", true, propertySchema);
            }
        }
    }

But, i am getting an exception as given below

Exception in thread "main" java.lang.IllegalArgumentException: Unrecognized field "label" (class com.fasterxml.jackson.module.jsonSchema.types.StringSchema), not marked as ignorable (14 known properties: "default", "enum", "$schema", "maxLength", "$ref", "pattern", "format", "title", "minLength", "extends", "disallow", "id", "description", "required"])
 at [Source: N/A; line: -1, column: -1] (through reference chain: com.fasterxml.jackson.module.jsonSchema.types.StringSchema["label"])
at com.fasterxml.jackson.databind.ObjectMapper._convert(ObjectMapper.java:2761)
at com.fasterxml.jackson.databind.ObjectMapper.convertValue(ObjectMapper.java:2687)
at org.jsonschema.practice.CustomProperty.addCustomProperty(CustomProperty.java:80)
at org.jsonschema.practice.CustomProperty.addLabelFields(CustomProperty.java:67)
at org.jsonschema.practice.CustomProperty.main(CustomProperty.java:95)

Ref: Attached is the Sample Maven Project for the same.
jsonschema-addprop.zip

Tatu Saloranta

unread,
Jun 11, 2014, 12:04:52 AM6/11/14
to jackso...@googlegroups.com
I don't think there is functionality for doing this currently, but one way such support could be added would be to add a meta-annotation; marker that could be added to other annotations to indicate that their values were to be added. There would obviously need to be other controls to indicate kind of inclusion to use; but the basic idea would be to use such markers to support custom annotation types.

-+ Tatu +-



--
You received this message because you are subscribed to the Google Groups "jackson-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email to jackson-user...@googlegroups.com.
To post to this group, send email to jackso...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Jon

unread,
Nov 17, 2015, 10:59:12 AM11/17/15
to jackson-user
A year on...

Is there any way of doing this with the current JSON schema generator?

We notice that Map<Float, Integer> types will always be output as follows:

"exampleMap" : {
      "type" : "object",
      "title" : "MapType",
      "additionalProperties" : {
        "type" : "integer"
      }
    }

Ideally we'd like to preserve the type of the key.  Is that possible?

Many thanks,
Jon.

Tatu Saloranta

unread,
Nov 17, 2015, 11:06:52 AM11/17/15
to jackson-user
Jon, is this related to the original question?

-+ Tatu +-

Jon

unread,
Nov 18, 2015, 8:30:51 AM11/18/15
to jackson-user
I'll post a new one for the map problem, but the general way of adding custom properties for documentation still remains, yes.
Reply all
Reply to author
Forward
0 new messages