Search in sources :

Example 56 with JavaType

use of org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.JavaType in project candlepin by candlepin.

the class CandlepinSwaggerModelConverter method resolve.

public Model resolve(Type rawType, ModelConverterContext context, Iterator<ModelConverter> next) {
    if (this.shouldIgnoreClass(rawType)) {
        return null;
    }
    /**
     * See java doc of NestedComplexType. This unwrapping makes sure that a
     * real type of field used throughout the method. At the same time flag
     * 'isNested' helps to indicate later in the method that this type may
     * be introspected as Hateoas enabled field
     */
    boolean isNested = false;
    if (rawType instanceof NestedComplexType) {
        isNested = true;
        NestedComplexType nested = (NestedComplexType) rawType;
        rawType = nested.getOriginalType();
    }
    JavaType type = pMapper.constructType(rawType);
    if (type.isEnumType() || PrimitiveType.fromType(type) != null) {
        // We don't build models for primitive types
        return null;
    }
    final BeanDescription beanDesc = pMapper.getSerializationConfig().introspect(type);
    // Couple of possibilities for defining
    String name = isNested ? "Nested" : "";
    name += pTypeName(type, beanDesc);
    if ("Object".equals(name)) {
        return new ModelImpl();
    }
    final ModelImpl model = new ModelImpl().type(ModelImpl.OBJECT).name(name).description(pDescription(beanDesc.getClassInfo()));
    if (!type.isContainerType()) {
        // define the model here to support self/cyclic referencing of
        // models
        context.defineModel(name, model, type, null);
    }
    if (type.isContainerType()) {
        // We treat collections as primitive types, just need to add models
        // for values (if any)
        context.resolve(type.getContentType());
        return null;
    }
    // if XmlRootElement annotation, construct an Xml object and attach it
    // to the model
    XmlRootElement rootAnnotation = beanDesc.getClassAnnotations().get(XmlRootElement.class);
    if (rootAnnotation != null && !"".equals(rootAnnotation.name()) && !"##default".equals(rootAnnotation.name())) {
        log.debug(rootAnnotation.toString());
        Xml xml = new Xml().name(rootAnnotation.name());
        if (rootAnnotation.namespace() != null && !"".equals(rootAnnotation.namespace()) && !"##default".equals(rootAnnotation.namespace())) {
            xml.namespace(rootAnnotation.namespace());
        }
        model.xml(xml);
    }
    // see if @JsonIgnoreProperties exist
    Set<String> propertiesToIgnore = new HashSet<>();
    JsonIgnoreProperties ignoreProperties = beanDesc.getClassAnnotations().get(JsonIgnoreProperties.class);
    if (ignoreProperties != null) {
        propertiesToIgnore.addAll(Arrays.asList(ignoreProperties.value()));
    }
    final ApiModel apiModel = beanDesc.getClassAnnotations().get(ApiModel.class);
    String disc = (apiModel == null) ? "" : apiModel.discriminator();
    if (apiModel != null && StringUtils.isNotEmpty(apiModel.reference())) {
        model.setReference(apiModel.reference());
    }
    if (disc.isEmpty()) {
        // longer method would involve
        // AnnotationIntrospector.findTypeResolver(...) but:
        JsonTypeInfo typeInfo = beanDesc.getClassAnnotations().get(JsonTypeInfo.class);
        if (typeInfo != null) {
            disc = typeInfo.property();
        }
    }
    if (!disc.isEmpty()) {
        model.setDiscriminator(disc);
    }
    List<Property> props = new ArrayList<>();
    for (BeanPropertyDefinition propDef : beanDesc.findProperties()) {
        parseProperty(context, isNested, beanDesc, propertiesToIgnore, props, propDef);
    }
    Collections.sort(props, getPropertyComparator());
    Map<String, Property> modelProps = new LinkedHashMap<>();
    for (Property prop : props) {
        modelProps.put(prop.getName(), prop);
    }
    model.setProperties(modelProps);
    /**
     * This must be done after model.setProperties so that the model's set
     * of properties is available to filter from any subtypes
     */
    if (!resolveSubtypes(model, beanDesc, context)) {
        model.setDiscriminator(null);
    }
    return model;
}
Also used : XmlRootElement(javax.xml.bind.annotation.XmlRootElement) BeanDescription(com.fasterxml.jackson.databind.BeanDescription) ArrayList(java.util.ArrayList) ApiModel(io.swagger.annotations.ApiModel) JsonTypeInfo(com.fasterxml.jackson.annotation.JsonTypeInfo) JsonIgnoreProperties(com.fasterxml.jackson.annotation.JsonIgnoreProperties) LinkedHashMap(java.util.LinkedHashMap) JavaType(com.fasterxml.jackson.databind.JavaType) Xml(io.swagger.models.Xml) BeanPropertyDefinition(com.fasterxml.jackson.databind.introspect.BeanPropertyDefinition) ModelImpl(io.swagger.models.ModelImpl) 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) HashSet(java.util.HashSet)

Example 57 with JavaType

use of org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.JavaType in project candlepin by candlepin.

the class CandlepinSwaggerModelConverter method resolveProperty.

public Property resolveProperty(Type type, ModelConverterContext context, Annotation[] annotations, Iterator<ModelConverter> next) {
    JavaType propType = null;
    /**
     * See java doc of NestedComplexType. This unwrapping makes sure that a
     * real type of field is passed to _mapper
     */
    if (type instanceof NestedComplexType) {
        propType = pMapper.constructType(((NestedComplexType) type).getOriginalType());
    } else {
        propType = pMapper.constructType(type);
    }
    log.debug("resolveProperty {}", propType);
    Property property = null;
    if (propType.isContainerType()) {
        JavaType keyType = propType.getKeyType();
        JavaType valueType = propType.getContentType();
        if (keyType != null && valueType != null) {
            property = new MapProperty().additionalProperties(context.resolveProperty(valueType, new Annotation[] {}));
        } else if (valueType != null) {
            ArrayProperty arrayProperty = new ArrayProperty().items(context.resolveProperty(valueType, new Annotation[] {}));
            if (pIsSetType(propType.getRawClass())) {
                arrayProperty.setUniqueItems(true);
            }
            property = arrayProperty;
        }
    } else {
        property = PrimitiveType.createProperty(propType);
    }
    if (property == null) {
        if (propType.isEnumType()) {
            property = new StringProperty();
            pAddEnumProps(propType.getRawClass(), property);
        } else if (pIsOptionalType(propType)) {
            property = context.resolveProperty(propType.containedType(0), null);
        } else {
            // complex type
            Model innerModel = context.resolve(type);
            if (innerModel instanceof ModelImpl) {
                ModelImpl mi = (ModelImpl) innerModel;
                property = new RefProperty(StringUtils.isNotEmpty(mi.getReference()) ? mi.getReference() : mi.getName());
            }
        }
    }
    return property;
}
Also used : JavaType(com.fasterxml.jackson.databind.JavaType) ArrayProperty(io.swagger.models.properties.ArrayProperty) MapProperty(io.swagger.models.properties.MapProperty) Model(io.swagger.models.Model) RefModel(io.swagger.models.RefModel) ComposedModel(io.swagger.models.ComposedModel) ApiModel(io.swagger.annotations.ApiModel) StringProperty(io.swagger.models.properties.StringProperty) ModelImpl(io.swagger.models.ModelImpl) 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) RefProperty(io.swagger.models.properties.RefProperty)

Example 58 with JavaType

use of org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.JavaType in project candlepin by candlepin.

the class CandlepinSwaggerModelConverter method resolveApiAnnotated.

private Property resolveApiAnnotated(ModelConverterContext context, Property property, Annotation[] annotations, ApiModelProperty mp, JavaType propType) {
    String or = mp.dataType();
    JavaType innerJavaType = null;
    log.debug("overriding datatype from {} to {}", propType, or);
    if (or.toLowerCase().startsWith("list[")) {
        String innerType = or.substring(5, or.length() - 1);
        ArrayProperty p = new ArrayProperty();
        Property primitiveProperty = PrimitiveType.createProperty(innerType);
        if (primitiveProperty != null) {
            p.setItems(primitiveProperty);
        } else {
            innerJavaType = getInnerType(innerType);
            p.setItems(context.resolveProperty(innerJavaType, annotations));
        }
        property = p;
    } else if (or.toLowerCase().startsWith("map[")) {
        int pos = or.indexOf(",");
        if (pos > 0) {
            String innerType = or.substring(pos + 1, or.length() - 1);
            MapProperty p = new MapProperty();
            Property primitiveProperty = PrimitiveType.createProperty(innerType);
            if (primitiveProperty != null) {
                p.setAdditionalProperties(primitiveProperty);
            } else {
                innerJavaType = getInnerType(innerType);
                p.setAdditionalProperties(context.resolveProperty(innerJavaType, annotations));
            }
            property = p;
        }
    } else {
        Property primitiveProperty = PrimitiveType.createProperty(or);
        if (primitiveProperty != null) {
            property = primitiveProperty;
        } else {
            innerJavaType = getInnerType(or);
            property = context.resolveProperty(innerJavaType, annotations);
        }
    }
    if (innerJavaType != null) {
        context.resolve(innerJavaType);
    }
    return property;
}
Also used : JavaType(com.fasterxml.jackson.databind.JavaType) ArrayProperty(io.swagger.models.properties.ArrayProperty) MapProperty(io.swagger.models.properties.MapProperty) 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)

Example 59 with JavaType

use of org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.JavaType 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)

Example 60 with JavaType

use of org.apache.flink.shaded.jackson2.com.fasterxml.jackson.databind.JavaType in project core-ng-project by neowu.

the class JSONReader method of.

public static <T> JSONReader<T> of(Type instanceType) {
    ObjectMapper objectMapper = JSONMapper.OBJECT_MAPPER;
    JavaType type = objectMapper.getTypeFactory().constructType(instanceType);
    return new JSONReader<>(objectMapper.readerFor(type));
}
Also used : JavaType(com.fasterxml.jackson.databind.JavaType) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Aggregations

JavaType (com.fasterxml.jackson.databind.JavaType)322 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)36 Test (org.junit.Test)29 BeanDescription (com.fasterxml.jackson.databind.BeanDescription)25 IOException (java.io.IOException)25 BeanPropertyDefinition (com.fasterxml.jackson.databind.introspect.BeanPropertyDefinition)22 ArrayList (java.util.ArrayList)21 TypeFactory (com.fasterxml.jackson.databind.type.TypeFactory)17 Property (io.swagger.models.properties.Property)16 List (java.util.List)16 Map (java.util.Map)14 ModelImpl (io.swagger.models.ModelImpl)13 StringProperty (io.swagger.models.properties.StringProperty)13 Annotation (java.lang.annotation.Annotation)12 Method (java.lang.reflect.Method)11 Type (java.lang.reflect.Type)11 ObjectWriter (com.fasterxml.jackson.databind.ObjectWriter)10 BodyParameter (io.swagger.models.parameters.BodyParameter)10 MapProperty (io.swagger.models.properties.MapProperty)10 JsonDeserializer (com.fasterxml.jackson.databind.JsonDeserializer)9