All Downloads are FREE. Search and download functionalities are using the official Maven repository.

cn.msuno.swagger.spring.boot.autoconfigure.plugins.SwaggerDefaultModelProvider Maven / Gradle / Ivy

There is a newer version: 2.5.2
Show newest version
package cn.msuno.swagger.spring.boot.autoconfigure.plugins;

import java.lang.reflect.Field;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Component;

import com.fasterxml.classmate.TypeResolver;
import com.google.common.base.Optional;

import cn.msuno.javadoc.docs.ClassJavadoc;
import cn.msuno.javadoc.docs.FieldJavadoc;
import springfox.documentation.schema.DefaultModelProvider;
import springfox.documentation.schema.Model;
import springfox.documentation.schema.ModelDependencyProvider;
import springfox.documentation.schema.ModelProperty;
import springfox.documentation.schema.TypeNameExtractor;
import springfox.documentation.schema.plugins.SchemaPluginsManager;
import springfox.documentation.schema.property.ModelPropertiesProvider;
import springfox.documentation.spi.schema.EnumTypeDeterminer;
import springfox.documentation.spi.schema.contexts.ModelContext;

@Component
@Primary
public class SwaggerDefaultModelProvider extends DefaultModelProvider {
    
    private Logger logger = LoggerFactory.getLogger(getClass());
    
    @Autowired
    public SwaggerDefaultModelProvider(TypeResolver resolver,
            @Qualifier("cachedModelProperties") ModelPropertiesProvider propertiesProvider,
            @Qualifier("cachedModelDependencies") ModelDependencyProvider dependencyProvider,
            SchemaPluginsManager schemaPluginsManager,
            TypeNameExtractor typeNameExtractor, EnumTypeDeterminer enumTypeDeterminer) {
        super(resolver, propertiesProvider, dependencyProvider, schemaPluginsManager, typeNameExtractor,
                enumTypeDeterminer);
    }
    
    
    @Override
    public Optional modelFor(ModelContext modelContext) {
        Optional modelOptional = super.modelFor(modelContext);
        ClassJavadoc javadoc = null;
        Model model = null;
        if (modelOptional.isPresent()) {
            model = modelOptional.get();
            try {
                Class aClass = Class.forName(model.getQualifiedType());
                javadoc = SwaggerBuilderPlugin.getOrCreate(aClass);
            } catch (ClassNotFoundException e) {
                logger.info("get class error {}", e.getMessage());
            }
        }
        if (null != javadoc) {
            updateField(model, "description", javadoc.getComment().toString());
            Map properties = model.getProperties();
            for (Map.Entry kv : properties.entrySet()) {
                for (FieldJavadoc fieldJavadoc : javadoc.getFields()) {
                    if (fieldJavadoc.getName().equals(kv.getKey())) {
                        updateField(kv.getValue(), "description", fieldJavadoc.getComment().toString());
                    }
                }
            }
        }
        return modelOptional;
    }
    
    @Override
    public Map dependencies(ModelContext modelContext) {
        return super.dependencies(modelContext);
    }
    
    private void updateField(Object obj, String key, String value) {
        Class clz = obj.getClass();
        Field[] fields = clz.getDeclaredFields();
        for (Field field : fields) {
            field.setAccessible(true);
            if (key.equals(field.getName())) {
                try {
                    field.set(obj, value);
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                }
            }
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy