com.paas.swagger.schema.ApiBeanPropertyProperties 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.schema;
import com.fasterxml.classmate.ResolvedType;
import com.fasterxml.classmate.TypeResolver;
import com.google.common.base.Function;
import com.google.common.base.Splitter;
import com.google.common.collect.Lists;
import com.paas.swagger.annotations.ApiBeanProperty;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import springfox.documentation.service.AllowableListValues;
import springfox.documentation.service.AllowableRangeValues;
import springfox.documentation.service.AllowableValues;
import java.util.Collections;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import static com.google.common.collect.Lists.newArrayList;
import static org.springframework.util.StringUtils.hasText;
/**
* @ClassName ApiBeanPropertyProperties
* @Date 2020/8/7 11:51
* @Auther wangyongyong
* @Version 1.0
* @Description TODO
*/
public class ApiBeanPropertyProperties
{
private static final Logger LOGGER = LoggerFactory.getLogger(ApiBeanPropertyProperties.class);
private static final Pattern RANGE_PATTERN = Pattern.compile("range([\\[(])(.*),(.*)([])])$");
private ApiBeanPropertyProperties()
{
throw new UnsupportedOperationException();
}
public static Function toAllowableValues()
{
return annotation -> allowableValueFromString(annotation.allowableValues());
}
public static AllowableValues allowableValueFromString(String allowableValueString)
{
AllowableValues allowableValues = new AllowableListValues(Lists.newArrayList(), "LIST");
String trimmed = allowableValueString.trim();
Matcher matcher = RANGE_PATTERN.matcher(trimmed.replaceAll(" ", ""));
if (matcher.matches())
{
if (matcher.groupCount() != 4)
{
LOGGER.warn("Unable to parse range specified {} correctly", trimmed);
}
else
{
allowableValues = new AllowableRangeValues(
matcher.group(2).contains("infinity") ? null : matcher.group(2),
matcher.group(1).equals("("),
matcher.group(3).contains("infinity") ? null : matcher.group(3),
matcher.group(4).equals(")"));
}
}
else if (trimmed.contains(","))
{
Iterable split = Splitter.on(',').trimResults().omitEmptyStrings().split(trimmed);
allowableValues = new AllowableListValues(newArrayList(split), "LIST");
}
else if (hasText(trimmed))
{
List singleVal = Collections.singletonList(trimmed);
allowableValues = new AllowableListValues(singleVal, "LIST");
}
return allowableValues;
}
public static Function toIsRequired()
{
return annotation -> annotation.required();
}
public static Function toPosition()
{
return annotation -> annotation.position();
}
public static Function toType(final TypeResolver resolver)
{
return annotation ->
{
try
{
return resolver.resolve(Class.forName(annotation.dataType()));
}
catch (ClassNotFoundException e)
{
return resolver.resolve(Object.class);
}
};
}
public static Function toHidden()
{
return annotation -> annotation.hidden();
}
}