Skip to content

Commit

Permalink
refactor: 优化部分代码
Browse files Browse the repository at this point in the history
  • Loading branch information
Charles7c committed Jul 31, 2024
1 parent bd07f9b commit 5b0f89a
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 91 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@

import java.util.*;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

/**
* API 文档自动配置
Expand Down Expand Up @@ -141,7 +142,7 @@ public GlobalOpenApiCustomizer globalOpenApiCustomizer(SpringDocExtensionPropert
}

/**
* 自定义 openapi 处理器
* 自定义 OpenApi 处理器
*/
@Bean
public OpenAPIService openApiBuilder(Optional<OpenAPI> openAPI,
Expand All @@ -155,54 +156,35 @@ public OpenAPIService openApiBuilder(Optional<OpenAPI> openAPI,
}

/**
* 展示 枚举类型和值
* 自定义参数配置(针对 BaseEnum 展示枚举值和描述)
*
* @return
* @since 2.4.0
*/
@Bean
public ParameterCustomizer customParameterCustomizer() {
return (parameterModel, methodParameter) -> {
// 判断方法参数类型是否为 IBaseEnum 的子类型
if (ClassUtil.isAssignable(BaseEnum.class, methodParameter.getParameterType())) {
String description = parameterModel.getDescription();
// TODO 会重复调用,有什么优雅的判读方式吗?
if (StrUtil.contains(description, "color:red")) {
return parameterModel;
}
Schema schema = parameterModel.getSchema();

// 获取方法参数类型的所有枚举常量
BaseEnum[] enumConstants = (BaseEnum[])methodParameter.getParameterType().getEnumConstants();
List<String> list = new ArrayList<>();
Map<Object, String> descMap = new HashMap<>();

// 遍历枚举常量,获取其值和描述
for (BaseEnum constant : enumConstants) {
list.add(constant.getValue().toString());
descMap.put(constant.getValue(), constant.getDescription());
}

// 枚举值类型
String enumValueType = EnumTypeUtils.getEnumValueTypeAsString(methodParameter.getParameterType());
schema.setType(enumValueType);
switch (enumValueType) {
case "integer" -> schema.setFormat("int32");
case "long" -> schema.setFormat("int64");
case "number" -> schema.setFormat("double");
}

// 设置枚举值列表和描述
schema.setEnum(list);
parameterModel.setDescription(description + "<span style='color:red'>" + descMap + "</span>");
Class<?> parameterType = methodParameter.getParameterType();
// 判断是否为 BaseEnum 的子类型
if (!ClassUtil.isAssignable(BaseEnum.class, parameterType)) {
return parameterModel;
}
String description = parameterModel.getDescription();
if (StrUtil.contains(description, "color:red")) {
return parameterModel;
}
// 封装参数配置
this.configureSchema(parameterModel.getSchema(), parameterType);
// 自定义枚举描述
parameterModel.setDescription(description + "<span style='color:red'>" + this
.getDescMap(parameterType) + "</span>");
return parameterModel;
};
}

/**
* 展示 枚举类型和值
* 自定义参数配置(针对 BaseEnum 展示枚举值和描述)
*
* @return
* @since 2.4.0
*/
@Bean
public PropertyCustomizer customPropertyCustomizer() {
Expand All @@ -216,36 +198,55 @@ public PropertyCustomizer customPropertyCustomizer() {
} else {
rawClass = Object.class;
}

// 检查原始类是否实现了 IBaseEnum 接口
if (ClassUtil.isAssignable(BaseEnum.class, rawClass)) {
BaseEnum[] enumConstants = (BaseEnum[])rawClass.getEnumConstants();
List<String> list = new ArrayList<>();
Map<Object, String> descMap = new HashMap<>();
// 遍历枚举常量,获取其值和描述
for (BaseEnum constant : enumConstants) {
list.add(constant.getValue().toString());
descMap.put(constant.getValue(), constant.getDescription());
}
// 获取泛型类型
String enumValueType = EnumTypeUtils.getEnumValueTypeAsString(rawClass);
schema.setType(enumValueType);
// 根据枚举值类型设置 schema 的格式
switch (enumValueType) {
case "integer" -> schema.setFormat("int32");
case "long" -> schema.setFormat("int64");
case "number" -> schema.setFormat("double");
}

// 设置枚举值列表和描述
schema.setEnum(list);
schema.setDescription(schema.getDescription() + "<span style='color:red'>" + descMap + "</span>");
// 判断是否为 BaseEnum 的子类型
if (!ClassUtil.isAssignable(BaseEnum.class, rawClass)) {
return schema;
}
// 封装参数配置
this.configureSchema(schema, rawClass);
// 自定义参数描述
schema.setDescription(schema.getDescription() + "<span style='color:red'>" + this
.getDescMap(rawClass) + "</span>");
return schema;
};
}

/**
* 封装 Schema 配置
*
* @param schema Schema
* @param enumClass 枚举类型
* @since 2.4.0
*/
private void configureSchema(Schema schema, Class<?> enumClass) {
BaseEnum[] enums = (BaseEnum[])enumClass.getEnumConstants();
// 设置枚举可用值
List<String> valueList = Arrays.stream(enums).map(e -> e.getValue().toString()).toList();
schema.setEnum(valueList);
// 设置枚举值类型和格式
String enumValueType = EnumTypeUtils.getEnumValueTypeAsString(enumClass);
schema.setType(enumValueType);
switch (enumValueType) {
case "integer" -> schema.setFormat("int32");
case "long" -> schema.setFormat("int64");
case "number" -> schema.setFormat("double");
default -> schema.setFormat(enumValueType);
}
}

/**
* 获取枚举描述 Map
*
* @param enumClass 枚举类型
* @return 枚举描述 Map
* @since 2.4.0
*/
private Map<Object, String> getDescMap(Class<?> enumClass) {
BaseEnum[] enums = (BaseEnum[])enumClass.getEnumConstants();
return Arrays.stream(enums)
.collect(Collectors.toMap(BaseEnum::getValue, BaseEnum::getDescription, (a, b) -> a, LinkedHashMap::new));
}

@PostConstruct
public void postConstruct() {
log.debug("[ContiNew Starter] - Auto Configuration 'ApiDoc' completed initialization.");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,10 @@
import java.util.stream.Stream;

/**
* 自定义 openapi 处理器 对源码功能进行修改 增强使用
* 自定义 OpenApi 处理器(对源码功能进行修改,增强使用)
*
* @author echo
* @since 2.4.0
*/
@SuppressWarnings("all")
public class OpenApiHandler extends OpenAPIService {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,19 @@
/**
* 枚举类型工具
*
* @Author echo
* @date 2024/07/31
* @author echo
* @since 2.4.0
*/
public class EnumTypeUtils {

private EnumTypeUtils() {
}

/**
* 获取enum值类型
* 获取枚举值类型
*
* @param enumClass enum class
* @return {@link String }
* @param enumClass 枚举类型
* @return 枚举值类型
*/
public static String getEnumValueTypeAsString(Class<?> enumClass) {
try {
Expand All @@ -44,25 +47,26 @@ public static String getEnumValueTypeAsString(Class<?> enumClass) {
// 检查接口是否为参数化类型
if (type instanceof ParameterizedType parameterizedType) {
// 检查接口的原始类型是否为 BaseEnum
if (parameterizedType.getRawType() == BaseEnum.class) {
Type actualType = parameterizedType.getActualTypeArguments()[0];
// 检查实际类型参数是否为类类型
if (actualType instanceof Class<?> actualClass) {
if (actualClass == Integer.class) {
return "integer";
} else if (actualClass == Long.class) {
return "long";
} else if (actualClass == Double.class) {
return "number";
} else if (actualClass == String.class) {
return "string";
}
if (parameterizedType.getRawType() != BaseEnum.class) {
continue;
}
Type actualType = parameterizedType.getActualTypeArguments()[0];
// 检查实际类型参数是否为类类型
if (actualType instanceof Class<?> actualClass) {
if (actualClass == Integer.class) {
return "integer";
} else if (actualClass == Long.class) {
return "long";
} else if (actualClass == Double.class) {
return "number";
} else if (actualClass == String.class) {
return "string";
}
}
}
}
} catch (Exception e) {
e.printStackTrace();
} catch (Exception ignored) {
// ignored
}
return "string";
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,11 @@ public MybatisPlusPropertiesCustomizer mybatisPlusPropertiesCustomizer() {
public MybatisPlusInterceptor mybatisPlusInterceptor(MyBatisPlusExtensionProperties properties) {
MybatisPlusInterceptor interceptor = new MybatisPlusInterceptor();
// 数据权限插件
MyBatisPlusExtensionProperties.DataPermissionProperties dataPermissionProperties = properties.getDataPermission();
MyBatisPlusExtensionProperties.DataPermissionProperties dataPermissionProperties = properties
.getDataPermission();
if (null != dataPermissionProperties && dataPermissionProperties.isEnabled()) {
interceptor.addInnerInterceptor(new DataPermissionInterceptor(SpringUtil.getBean(DataPermissionHandler.class)));
interceptor.addInnerInterceptor(new DataPermissionInterceptor(SpringUtil
.getBean(DataPermissionHandler.class)));
}
// 分页插件
MyBatisPlusExtensionProperties.PaginationProperties paginationProperties = properties.getPagination();
Expand Down
7 changes: 0 additions & 7 deletions continew-starter-dependencies/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@
<flatten.version>1.6.0</flatten.version>
<spotless.version>2.43.0</spotless.version>
<sonar.version>3.11.0.3922</sonar.version>
<maven-compiler-plugin.verison>3.11.0</maven-compiler-plugin.verison>
</properties>

<dependencyManagement>
Expand Down Expand Up @@ -655,12 +654,6 @@
</plugins>
<pluginManagement>
<plugins>
<!-- 编译插件 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>${maven-compiler-plugin.verison}</version>
</plugin>
<!-- 统一版本号插件 -->
<plugin>
<groupId>org.codehaus.mojo</groupId>
Expand Down

0 comments on commit 5b0f89a

Please sign in to comment.