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

com.paas.swagger.plugin.ApiBeanPropertyPropertyBuilder Maven / Gradle / Ivy

The newest version!
package com.paas.swagger.plugin;

import com.alibaba.fastjson.annotation.JSONField;
import com.google.common.base.Optional;

import com.paas.swagger.annotations.ApiBeanProperty;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spi.schema.ModelPropertyBuilderPlugin;
import springfox.documentation.spi.schema.contexts.ModelPropertyContext;
import springfox.documentation.swagger.common.SwaggerPluginSupport;

import static com.paas.swagger.schema.ApiBeanPropertyProperties.*;
import static springfox.documentation.schema.Annotations.findPropertyAnnotation;

/**
 * @ClassName ApiBeanPropertyPropertyBuilder
 * @Date 2020/8/7 11:32
 * @Auther wangyongyong
 * @Version 1.0
 * @Description bean 对象解析
 */
public class ApiBeanPropertyPropertyBuilder implements ModelPropertyBuilderPlugin {

    @Override
    public void apply(ModelPropertyContext context) {
        Optional annotation = Optional.absent();
        Optional jsonFieldAnnotation = Optional.absent();
        if (context.getBeanPropertyDefinition().isPresent()) {
            annotation = annotation.or(findPropertyAnnotation(context.getBeanPropertyDefinition().get(), ApiBeanProperty.class));
            jsonFieldAnnotation = jsonFieldAnnotation.or(findPropertyAnnotation(context.getBeanPropertyDefinition().get(), JSONField.class));
        }
        if (jsonFieldAnnotation.isPresent()) {
            JSONField jsonField = jsonFieldAnnotation.get();
            context.getBuilder().name(jsonField.name()).isHidden(!jsonField.serialize()).isHidden(!jsonField.deserialize()).defaultValue(jsonField.defaultValue());
        }
        if (annotation.isPresent()) {
            ApiBeanProperty property = annotation.get();
            context.getBuilder()
                    .allowableValues(annotation.transform(toAllowableValues()).orNull())
                    .required(annotation.transform(toIsRequired()).or(false))
                    .description(property.value())
                    .isHidden(annotation.transform(toHidden()).or(false))
                    .type(annotation.transform(toType(context.getResolver())).orNull())
                    .position(annotation.transform(toPosition()).or(0))
                    .example(property.example());
        }
    }

    @Override
    public boolean supports(DocumentationType documentationType) {
        return SwaggerPluginSupport.pluginDoesApply(documentationType);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy