io.swagger.converter.ModelConverters Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of swagger-all Show documentation
Show all versions of swagger-all Show documentation
swagger-all is a rebundled verison of Swagger as one OSGi bundle.
The newest version!
package io.swagger.converter;
import com.fasterxml.jackson.databind.type.TypeFactory;
import io.swagger.jackson.ModelResolver;
import io.swagger.models.Model;
import io.swagger.models.properties.Property;
import io.swagger.util.Json;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.ServiceLoader;
import java.util.Set;
import java.util.concurrent.CopyOnWriteArrayList;
public class ModelConverters {
private static final ModelConverters SINGLETON = new ModelConverters();
static Logger LOGGER = LoggerFactory.getLogger(ModelConverters.class);
private final List converters;
private final Set skippedPackages = new HashSet();
private final Set skippedClasses = new HashSet();
public ModelConverters() {
converters = new CopyOnWriteArrayList();
converters.add(new ModelResolver(Json.mapper()));
}
public static ModelConverters getInstance() {
return SINGLETON;
}
public void addConverter(ModelConverter converter) {
converters.add(0, converter);
}
public void removeConverter(ModelConverter converter) {
converters.remove(converter);
}
public void addPackageToSkip(String pkg) {
this.skippedPackages.add(pkg);
}
public void addClassToSkip(String cls) {
LOGGER.warn("skipping class " + cls);
this.skippedClasses.add(cls);
}
public Property readAsProperty(Type type) {
ModelConverterContextImpl context = new ModelConverterContextImpl(
converters);
return context.resolveProperty(type, null);
}
public Map read(Type type) {
Map modelMap = new HashMap();
if (shouldProcess(type)) {
ModelConverterContextImpl context = new ModelConverterContextImpl(
converters);
Model resolve = context.resolve(type);
for (Entry entry : context.getDefinedModels()
.entrySet()) {
if (entry.getValue().equals(resolve)) {
modelMap.put(entry.getKey(), entry.getValue());
}
}
}
return modelMap;
}
public Map readAll(Type type) {
if (shouldProcess(type)) {
ModelConverterContextImpl context = new ModelConverterContextImpl(
converters);
LOGGER.debug("ModelConverters readAll from " + type);
context.resolve(type);
return context.getDefinedModels();
}
return new HashMap();
}
private boolean shouldProcess(Type type) {
final Class> cls = TypeFactory.defaultInstance().constructType(type).getRawClass();
if (cls.isPrimitive()) {
return false;
}
String className = cls.getName();
for (String packageName : skippedPackages) {
if (className.startsWith(packageName)) {
return false;
}
}
for (String classToSkip : skippedClasses) {
if (className.equals(classToSkip)) {
return false;
}
}
return true;
}
static {
SINGLETON.skippedPackages.add("java.lang");
ServiceLoader loader = ServiceLoader.load(ModelConverter.class);
Iterator itr = loader.iterator();
while (itr.hasNext()) {
ModelConverter ext = itr.next();
if (ext == null) {
LOGGER.error("failed to load extension " + ext);
} else {
SINGLETON.addConverter(ext);
LOGGER.debug("adding ModelConverter: " + ext);
}
}
}
}