Search in sources :

Example 1 with ReaderListener

use of io.swagger.jaxrs.config.ReaderListener in project swagger-core by swagger-api.

the class Reader method read.

/**
     * Scans a set of classes for both ReaderListeners and Swagger annotations. All found listeners will
     * be instantiated before any of the classes are scanned for Swagger annotations - so they can be invoked
     * accordingly.
     *
     * @param classes a set of classes to scan
     * @return the generated Swagger definition
     */
public Swagger read(Set<Class<?>> classes) {
    Set<Class<?>> sortedClasses = new TreeSet<Class<?>>(new Comparator<Class<?>>() {

        @Override
        public int compare(Class<?> class1, Class<?> class2) {
            if (class1.equals(class2)) {
                return 0;
            } else if (class1.isAssignableFrom(class2)) {
                return -1;
            } else if (class2.isAssignableFrom(class1)) {
                return 1;
            }
            return class1.getName().compareTo(class2.getName());
        }
    });
    sortedClasses.addAll(classes);
    Map<Class<?>, ReaderListener> listeners = new HashMap<Class<?>, ReaderListener>();
    for (Class<?> cls : sortedClasses) {
        if (ReaderListener.class.isAssignableFrom(cls) && !listeners.containsKey(cls)) {
            try {
                listeners.put(cls, (ReaderListener) cls.newInstance());
            } catch (Exception e) {
                LOGGER.error("Failed to create ReaderListener", e);
            }
        }
    }
    for (ReaderListener listener : listeners.values()) {
        try {
            listener.beforeScan(this, swagger);
        } catch (Exception e) {
            LOGGER.error("Unexpected error invoking beforeScan listener [" + listener.getClass().getName() + "]", e);
        }
    }
    // process SwaggerDefinitions first - so we get tags in desired order
    for (Class<?> cls : sortedClasses) {
        SwaggerDefinition swaggerDefinition = cls.getAnnotation(SwaggerDefinition.class);
        if (swaggerDefinition != null) {
            readSwaggerConfig(cls, swaggerDefinition);
        }
    }
    for (Class<?> cls : sortedClasses) {
        read(cls, "", null, false, new String[0], new String[0], new LinkedHashMap<String, Tag>(), new ArrayList<Parameter>(), new HashSet<Class<?>>());
    }
    for (ReaderListener listener : listeners.values()) {
        try {
            listener.afterScan(this, swagger);
        } catch (Exception e) {
            LOGGER.error("Unexpected error invoking afterScan listener [" + listener.getClass().getName() + "]", e);
        }
    }
    return swagger;
}
Also used : HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) TreeSet(java.util.TreeSet) FormParameter(io.swagger.models.parameters.FormParameter) PathParameter(io.swagger.models.parameters.PathParameter) Parameter(io.swagger.models.parameters.Parameter) QueryParameter(io.swagger.models.parameters.QueryParameter) HeaderParameter(io.swagger.models.parameters.HeaderParameter) AnnotatedParameter(com.fasterxml.jackson.databind.introspect.AnnotatedParameter) Tag(io.swagger.models.Tag) ReaderListener(io.swagger.jaxrs.config.ReaderListener) SwaggerDefinition(io.swagger.annotations.SwaggerDefinition)

Aggregations

AnnotatedParameter (com.fasterxml.jackson.databind.introspect.AnnotatedParameter)1 SwaggerDefinition (io.swagger.annotations.SwaggerDefinition)1 ReaderListener (io.swagger.jaxrs.config.ReaderListener)1 Tag (io.swagger.models.Tag)1 FormParameter (io.swagger.models.parameters.FormParameter)1 HeaderParameter (io.swagger.models.parameters.HeaderParameter)1 Parameter (io.swagger.models.parameters.Parameter)1 PathParameter (io.swagger.models.parameters.PathParameter)1 QueryParameter (io.swagger.models.parameters.QueryParameter)1 HashMap (java.util.HashMap)1 LinkedHashMap (java.util.LinkedHashMap)1 TreeSet (java.util.TreeSet)1