com.paas.swagger.plugin.ApiOperationBuilder Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of paas-swagger-starter Show documentation
Show all versions of paas-swagger-starter Show documentation
Custom Swagger Spring Boot Starter
The newest version!
package com.paas.swagger.plugin;
import com.google.common.base.Optional;
import com.google.common.base.Splitter;
import com.google.common.collect.Lists;
import com.paas.swagger.annotations.ApiOperation;
import org.springframework.util.StringUtils;
import springfox.documentation.service.StringVendorExtension;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spi.service.OperationBuilderPlugin;
import springfox.documentation.spi.service.contexts.OperationContext;
import springfox.documentation.swagger.common.SwaggerPluginSupport;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;
import static com.google.common.base.Strings.nullToEmpty;
import static com.google.common.collect.Sets.newHashSet;
/**
* @ClassName ApiOperationBuilder
* @Date 2020/8/7 13:55
* @Auther wangyongyong
* @Version 1.0
* @Description TODO
*/
public class ApiOperationBuilder implements OperationBuilderPlugin
{
@Override
public void apply(OperationContext context)
{
Optional apiOperationOptional = context.findAnnotation(ApiOperation.class);
if (apiOperationOptional.isPresent())
{
ApiOperation apiOperation = apiOperationOptional.get();
int position;
int posit = apiOperation.position();
if (posit != 0)
{
position = posit;
}
else
{
position = Integer.MAX_VALUE;
}
context.operationBuilder()
.hidden(apiOperation.hidden())
.summary(apiOperation.value())
.notes(apiOperation.notes())
.extensions(Lists.newArrayList(new StringVendorExtension("x-order", String.valueOf(position))));
context.operationBuilder().consumes(asSet(nullToEmpty(apiOperation.consumes())));
context.operationBuilder().produces(asSet(nullToEmpty(apiOperation.produces())));
if (apiOperation.tags().length > 0 && !StringUtils.isEmpty(apiOperation.tags()[0]))
{
context.operationBuilder().tags(new HashSet<>(Arrays.asList(apiOperation.tags())));
}
}
else
{
context.operationBuilder()
.extensions(Lists.newArrayList(new StringVendorExtension("x-order", String.valueOf(Integer.MAX_VALUE))));
}
}
@Override
public boolean supports(DocumentationType delimiter)
{
return SwaggerPluginSupport.pluginDoesApply(delimiter);
}
private Set asSet(String mediaTypes)
{
return newHashSet(Splitter.on(',')
.trimResults()
.omitEmptyStrings()
.splitToList(mediaTypes));
}
}