Search in sources :

Example 1 with JsonFilter

use of com.fasterxml.jackson.annotation.JsonFilter in project candlepin by candlepin.

the class CandlepinSwaggerModelConverter method parseProperty.

private void parseProperty(ModelConverterContext context, boolean isNested, final BeanDescription beanDesc, Set<String> propertiesToIgnore, List<Property> props, BeanPropertyDefinition propDef) {
    Property property = null;
    String propName = propDef.getName();
    Annotation[] annotations = null;
    propName = getPropName(propDef, propName);
    PropertyMetadata md = propDef.getMetadata();
    boolean hasSetter = false, hasGetter = false;
    if (propDef.getSetter() == null) {
        hasSetter = false;
    } else {
        hasSetter = true;
    }
    if (propDef.getGetter() != null) {
        JsonProperty pd = propDef.getGetter().getAnnotation(JsonProperty.class);
        if (pd != null) {
            hasGetter = true;
        }
    }
    Boolean isReadOnly = null;
    if (!hasSetter & hasGetter) {
        isReadOnly = Boolean.TRUE;
    } else {
        isReadOnly = Boolean.FALSE;
    }
    final AnnotatedMember member = propDef.getPrimaryMember();
    if (member != null && !propertiesToIgnore.contains(propName) && /**
     * If the owning type is nested than we should include only those
     * fields that have the Hateoas annotation.
     */
    !(isNested && !member.hasAnnotation(HateoasInclude.class))) {
        List<Annotation> annotationList = new ArrayList<>();
        for (Annotation a : member.annotations()) {
            annotationList.add(a);
        }
        annotations = annotationList.toArray(new Annotation[annotationList.size()]);
        ApiModelProperty mp = member.getAnnotation(ApiModelProperty.class);
        if (mp != null && mp.readOnly()) {
            isReadOnly = mp.readOnly();
        }
        Type nested = null;
        JavaType propType = member.getType(beanDesc.bindingsForBeanType());
        JsonFilter jsonFilter = propType.getRawClass().getAnnotation(JsonFilter.class);
        /**
         * At this point the propType is a type of some nested field of the
         * type that is being processed. The condition checks if this
         * particular type should have Hateoas serialization enabled. In
         * other words, if we should create a new Nested* model.
         */
        if (jsonFilter != null && (jsonFilter.value().equals("ConsumerFilter") || jsonFilter.value().equals("EntitlementFilter") || jsonFilter.value().equals("OwnerFilter") || jsonFilter.value().equals("GuestFilter"))) {
            if (!nestedJavaTypes.containsKey(propType)) {
                nestedJavaTypes.put(propType, new NestedComplexType(propType));
            }
            nested = nestedJavaTypes.get(propType);
        } else {
            nested = propType;
        }
        // allow override of name from annotation
        if (mp != null && !mp.name().isEmpty()) {
            propName = mp.name();
        }
        if (mp != null && !mp.dataType().isEmpty()) {
            property = resolveApiAnnotated(context, property, annotations, mp, propType);
        }
        // no property from override, construct from propType
        if (property == null) {
            if (mp != null && StringUtils.isNotEmpty(mp.reference())) {
                property = new RefProperty(mp.reference());
            } else if (member.getAnnotation(JsonIdentityInfo.class) != null) {
                property = GeneratorWrapper.processJsonIdentity(propType, context, pMapper, member.getAnnotation(JsonIdentityInfo.class), member.getAnnotation(JsonIdentityReference.class));
            }
            if (property == null) {
                property = context.resolveProperty(nested, annotations);
            }
        }
        if (property != null) {
            addMetadataToProperty(property, propName, md, isReadOnly, member, mp);
            applyBeanValidatorAnnotations(property, annotations);
            props.add(property);
        }
    }
}
Also used : ApiModelProperty(io.swagger.annotations.ApiModelProperty) JsonProperty(com.fasterxml.jackson.annotation.JsonProperty) JsonIdentityReference(com.fasterxml.jackson.annotation.JsonIdentityReference) AnnotatedMember(com.fasterxml.jackson.databind.introspect.AnnotatedMember) ArrayList(java.util.ArrayList) Annotation(java.lang.annotation.Annotation) RefProperty(io.swagger.models.properties.RefProperty) NamedType(com.fasterxml.jackson.databind.jsontype.NamedType) Type(java.lang.reflect.Type) JavaType(com.fasterxml.jackson.databind.JavaType) PrimitiveType(io.swagger.util.PrimitiveType) JavaType(com.fasterxml.jackson.databind.JavaType) JsonIdentityInfo(com.fasterxml.jackson.annotation.JsonIdentityInfo) JsonFilter(com.fasterxml.jackson.annotation.JsonFilter) PropertyMetadata(com.fasterxml.jackson.databind.PropertyMetadata) JsonProperty(com.fasterxml.jackson.annotation.JsonProperty) StringProperty(io.swagger.models.properties.StringProperty) ArrayProperty(io.swagger.models.properties.ArrayProperty) Property(io.swagger.models.properties.Property) MapProperty(io.swagger.models.properties.MapProperty) UUIDProperty(io.swagger.models.properties.UUIDProperty) ApiModelProperty(io.swagger.annotations.ApiModelProperty) AbstractNumericProperty(io.swagger.models.properties.AbstractNumericProperty) RefProperty(io.swagger.models.properties.RefProperty) IntegerProperty(io.swagger.models.properties.IntegerProperty) HateoasInclude(org.candlepin.common.jackson.HateoasInclude)

Aggregations

JsonFilter (com.fasterxml.jackson.annotation.JsonFilter)1 JsonIdentityInfo (com.fasterxml.jackson.annotation.JsonIdentityInfo)1 JsonIdentityReference (com.fasterxml.jackson.annotation.JsonIdentityReference)1 JsonProperty (com.fasterxml.jackson.annotation.JsonProperty)1 JavaType (com.fasterxml.jackson.databind.JavaType)1 PropertyMetadata (com.fasterxml.jackson.databind.PropertyMetadata)1 AnnotatedMember (com.fasterxml.jackson.databind.introspect.AnnotatedMember)1 NamedType (com.fasterxml.jackson.databind.jsontype.NamedType)1 ApiModelProperty (io.swagger.annotations.ApiModelProperty)1 AbstractNumericProperty (io.swagger.models.properties.AbstractNumericProperty)1 ArrayProperty (io.swagger.models.properties.ArrayProperty)1 IntegerProperty (io.swagger.models.properties.IntegerProperty)1 MapProperty (io.swagger.models.properties.MapProperty)1 Property (io.swagger.models.properties.Property)1 RefProperty (io.swagger.models.properties.RefProperty)1 StringProperty (io.swagger.models.properties.StringProperty)1 UUIDProperty (io.swagger.models.properties.UUIDProperty)1 PrimitiveType (io.swagger.util.PrimitiveType)1 Annotation (java.lang.annotation.Annotation)1 Type (java.lang.reflect.Type)1