Search in sources :

Example 26 with NamedType

use of com.fasterxml.jackson.databind.jsontype.NamedType in project swagger-core by swagger-api.

the class ModelResolver method removeSuperSubTypes.

private void removeSuperSubTypes(List<NamedType> resultTypes, Class<?> superClass) {
    JavaType superType = _mapper.constructType(superClass);
    BeanDescription superBean = _mapper.getSerializationConfig().introspect(superType);
    final List<NamedType> superTypes = _intr.findSubtypes(superBean.getClassInfo());
    if (superTypes != null) {
        resultTypes.removeAll(superTypes);
    }
}
Also used : JavaType(com.fasterxml.jackson.databind.JavaType) NamedType(com.fasterxml.jackson.databind.jsontype.NamedType) BeanDescription(com.fasterxml.jackson.databind.BeanDescription)

Example 27 with NamedType

use of com.fasterxml.jackson.databind.jsontype.NamedType in project swagger-core by swagger-api.

the class ModelResolver method resolveSubtypes.

private boolean resolveSubtypes(ModelImpl model, BeanDescription bean, ModelConverterContext context) {
    final List<NamedType> types = _intr.findSubtypes(bean.getClassInfo());
    if (types == null) {
        return false;
    }
    /**
         * As the introspector will find @JsonSubTypes for a child class that are present on its super classes, the
         * code segment below will also run the introspector on the parent class, and then remove any sub-types that are
         * found for the parent from the sub-types found for the child. The same logic all applies to implemented
         * interfaces, and is accounted for below.
         */
    removeSuperClassAndInterfaceSubTypes(types, bean);
    int count = 0;
    final Class<?> beanClass = bean.getClassInfo().getAnnotated();
    for (NamedType subtype : types) {
        final Class<?> subtypeType = subtype.getType();
        if (!beanClass.isAssignableFrom(subtypeType)) {
            continue;
        }
        final Model subtypeModel = context.resolve(subtypeType);
        if (subtypeModel instanceof ModelImpl) {
            final ModelImpl impl = (ModelImpl) subtypeModel;
            // check if model name was inherited
            if (impl.getName().equals(model.getName())) {
                impl.setName(_typeNameResolver.nameForType(_mapper.constructType(subtypeType), TypeNameResolver.Options.SKIP_API_MODEL));
            }
            // remove shared properties defined in the parent
            final Map<String, Property> baseProps = model.getProperties();
            final Map<String, Property> subtypeProps = impl.getProperties();
            if (baseProps != null && subtypeProps != null) {
                for (Map.Entry<String, Property> entry : baseProps.entrySet()) {
                    if (entry.getValue().equals(subtypeProps.get(entry.getKey()))) {
                        subtypeProps.remove(entry.getKey());
                    }
                }
            }
            impl.setDiscriminator(null);
            ComposedModel child = new ComposedModel().parent(new RefModel(model.getName())).child(impl);
            context.defineModel(impl.getName(), child, subtypeType, null);
            ++count;
        }
    }
    return count != 0;
}
Also used : RefModel(io.swagger.models.RefModel) NamedType(com.fasterxml.jackson.databind.jsontype.NamedType) ComposedModel(io.swagger.models.ComposedModel) Model(io.swagger.models.Model) RefModel(io.swagger.models.RefModel) ComposedModel(io.swagger.models.ComposedModel) ApiModel(io.swagger.annotations.ApiModel) 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) Map(java.util.Map) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap)

Aggregations

NamedType (com.fasterxml.jackson.databind.jsontype.NamedType)27 AnnotationIntrospector (com.fasterxml.jackson.databind.AnnotationIntrospector)4 JavaType (com.fasterxml.jackson.databind.JavaType)3 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)3 ApiModel (io.swagger.annotations.ApiModel)3 HashMap (java.util.HashMap)3 Before (org.junit.Before)3 JsonProperty (com.fasterxml.jackson.annotation.JsonProperty)2 BeanDescription (com.fasterxml.jackson.databind.BeanDescription)2 TypeSerializer (com.fasterxml.jackson.databind.jsontype.TypeSerializer)2 DataSchema (io.druid.segment.indexing.DataSchema)2 UniformGranularitySpec (io.druid.segment.indexing.granularity.UniformGranularitySpec)2 HashBasedNumberedShardSpec (io.druid.timeline.partition.HashBasedNumberedShardSpec)2 ApiModelProperty (io.swagger.annotations.ApiModelProperty)2 ComposedModel (io.swagger.models.ComposedModel)2 Model (io.swagger.models.Model)2 ModelImpl (io.swagger.models.ModelImpl)2 RefModel (io.swagger.models.RefModel)2 AbstractNumericProperty (io.swagger.models.properties.AbstractNumericProperty)2 ArrayProperty (io.swagger.models.properties.ArrayProperty)2