Example usage for com.fasterxml.jackson.module.jsonSchema.types SimpleTypeSchema setRequired

List of usage examples for com.fasterxml.jackson.module.jsonSchema.types SimpleTypeSchema setRequired

Introduction

In this page you can find the example usage for com.fasterxml.jackson.module.jsonSchema.types SimpleTypeSchema setRequired.

Prototype

public void setRequired(Boolean required) 

Source Link

Usage

From source file:org.brutusin.json.impl.JacksonSchemaFactory.java

void enrich(SimpleTypeSchema schema, BeanProperty beanProperty) {
    JsonProperty jsonAnnot = beanProperty.getAnnotation(JsonProperty.class);
    IndexableProperty indexAnnot = beanProperty.getAnnotation(IndexableProperty.class);
    DependentProperty dependsAnnot = beanProperty.getAnnotation(DependentProperty.class);

    if (schema instanceof StringSchema) {
        StringSchema sschema = (StringSchema) schema;
        try {//from w  w w . j a va  2 s . c  o m
            Set<String> enums = sschema.getEnums();
            if (enums != null) {
                Method method = schema.getClass().getMethod("setValues", List.class);
                method.invoke(schema, new ArrayList(enums));
            }
        } catch (Exception parseException) {
            throw new Error("Error setting enum value from enumeration for " + beanProperty.getFullName(),
                    parseException);
        }
    }

    if (jsonAnnot == null) {
        schema.setTitle(beanProperty.getName());
    } else {
        if (jsonAnnot.title() != null) {
            schema.setTitle(jsonAnnot.title());
        } else {
            schema.setTitle(beanProperty.getName());
        }
        schema.setDescription(jsonAnnot.description());
        schema.setRequired(jsonAnnot.required());
        String def = jsonAnnot.defaultJsonExp();
        if (def != null) {
            try {
                Object defaultValue = JsonCodec.getInstance().parse(def, beanProperty.getType().getRawClass());
                Method method = schema.getClass().getMethod("setDef", Object.class);
                method.invoke(schema, defaultValue);
            } catch (Exception parseException) {
                throw new Error("Error setting default value for " + beanProperty.getFullName(),
                        parseException);
            }
        }
        String valuesMethodName = jsonAnnot.valuesMethod();
        if (valuesMethodName != null && !valuesMethodName.isEmpty()) {
            try {
                Method valuesMethod = beanProperty.getMember().getDeclaringClass().getMethod(valuesMethodName,
                        null);
                valuesMethod.setAccessible(true);
                Object valuesValue = valuesMethod.invoke(null, null);
                Method method = schema.getClass().getMethod("setValues", List.class);
                method.invoke(schema, valuesValue);
            } catch (Exception ex) {
                throw new Error("Error setting enum value from @JsonProperty.valuesMethod() for "
                        + beanProperty.getFullName(), ex);
            }
        } else {
            String values = jsonAnnot.values();
            if (values != null && !values.isEmpty()) {
                try {
                    Object valuesValue = JsonCodec.getInstance().parse(values, List.class);
                    Method method = schema.getClass().getMethod("setValues", List.class);
                    method.invoke(schema, valuesValue);
                } catch (Exception parseException) {
                    throw new Error("Error setting enum value from @JsonProperty.values() for "
                            + beanProperty.getFullName(), parseException);
                }
            }
        }
    }
    if (indexAnnot != null) {
        try {
            Method method = schema.getClass().getMethod("setIndex", IndexableProperty.IndexMode.class);
            method.invoke(schema, indexAnnot.mode());
        } catch (Exception parseException) {
            throw new Error("Error setting index value for " + beanProperty.getFullName(), parseException);
        }
    }
    if (dependsAnnot != null) {
        try {
            Method method = schema.getClass().getMethod("setDependsOn", String[].class);
            method.invoke(schema, (Object) dependsAnnot.dependsOn());
        } catch (Exception parseException) {
            throw new Error("Error setting dependsOn value for " + beanProperty.getFullName(), parseException);
        }
    }
}