diff --git a/continew-starter-api-doc/src/main/java/top/continew/starter/apidoc/autoconfigure/SpringDocAutoConfiguration.java b/continew-starter-api-doc/src/main/java/top/continew/starter/apidoc/autoconfigure/SpringDocAutoConfiguration.java index bebdb50c..a651a607 100644 --- a/continew-starter-api-doc/src/main/java/top/continew/starter/apidoc/autoconfigure/SpringDocAutoConfiguration.java +++ b/continew-starter-api-doc/src/main/java/top/continew/starter/apidoc/autoconfigure/SpringDocAutoConfiguration.java @@ -17,16 +17,11 @@ package top.continew.starter.apidoc.autoconfigure; import cn.hutool.core.map.MapUtil; -import cn.hutool.core.util.ClassUtil; -import cn.hutool.core.util.StrUtil; -import com.fasterxml.jackson.databind.type.CollectionType; -import com.fasterxml.jackson.databind.type.SimpleType; import io.swagger.v3.oas.models.Components; import io.swagger.v3.oas.models.OpenAPI; import io.swagger.v3.oas.models.info.Contact; import io.swagger.v3.oas.models.info.Info; import io.swagger.v3.oas.models.info.License; -import io.swagger.v3.oas.models.media.Schema; import io.swagger.v3.oas.models.security.SecurityRequirement; import io.swagger.v3.oas.models.security.SecurityScheme; import jakarta.annotation.PostConstruct; @@ -48,7 +43,7 @@ import org.springframework.http.CacheControl; import org.springframework.web.servlet.config.annotation.EnableWebMvc; import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; -import top.continew.starter.apidoc.handler.GenericEnumHandler; +import top.continew.starter.apidoc.handler.BaseEnumParameterHandler; import top.continew.starter.apidoc.handler.OpenApiHandler; import top.continew.starter.core.autoconfigure.project.ProjectProperties; import top.continew.starter.core.util.GeneralPropertySourceFactory; @@ -154,17 +149,16 @@ public class SpringDocAutoConfiguration implements WebMvcConfigurer { } /** - * 自定义参数配置(针对 BaseEnum 展示枚举值和描述) + * 自定义 BaseEnum 枚举参数配置(针对实现了 BaseEnum 的枚举,优化其枚举值和描述展示) * - * @return {@link GenericEnumHandler } + * @return {@link BaseEnumParameterHandler } * @since 2.4.0 */ @Bean - public GenericEnumHandler customParameterCustomizer() { - return new GenericEnumHandler(); + public BaseEnumParameterHandler customParameterCustomizer() { + return new BaseEnumParameterHandler(); } - @PostConstruct public void postConstruct() { log.debug("[ContiNew Starter] - Auto Configuration 'ApiDoc' completed initialization."); diff --git a/continew-starter-api-doc/src/main/java/top/continew/starter/apidoc/handler/BaseEnumParameterHandler.java b/continew-starter-api-doc/src/main/java/top/continew/starter/apidoc/handler/BaseEnumParameterHandler.java new file mode 100644 index 00000000..4e880a68 --- /dev/null +++ b/continew-starter-api-doc/src/main/java/top/continew/starter/apidoc/handler/BaseEnumParameterHandler.java @@ -0,0 +1,118 @@ +/* + * Copyright (c) 2022-present Charles7c Authors. All Rights Reserved. + *
+ * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0; + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + *
+ * http://www.gnu.org/licenses/lgpl.html + *
+ * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package top.continew.starter.apidoc.handler; + +import cn.hutool.core.util.ClassUtil; +import cn.hutool.core.util.StrUtil; +import com.fasterxml.jackson.databind.type.CollectionType; +import com.fasterxml.jackson.databind.type.SimpleType; +import io.swagger.v3.core.converter.AnnotatedType; +import io.swagger.v3.oas.models.media.Schema; +import io.swagger.v3.oas.models.parameters.Parameter; +import org.springdoc.core.customizers.ParameterCustomizer; +import org.springdoc.core.customizers.PropertyCustomizer; +import org.springframework.core.MethodParameter; + +import java.lang.reflect.Type; +import java.util.Arrays; +import java.util.List; +import top.continew.starter.apidoc.util.DocUtils; +import top.continew.starter.core.enums.BaseEnum; + +/** + * 自定义 BaseEnum 枚举参数处理器 + *
+ * 针对实现了 BaseEnum 的枚举,优化其枚举值和描述展示 + *
+ * + * @author echo + * @since 2.5.2 + */ +public class BaseEnumParameterHandler implements ParameterCustomizer, PropertyCustomizer { + + @Override + public Parameter customize(Parameter parameterModel, MethodParameter methodParameter) { + 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; + } + // 自定义枚举描述并封装参数配置 + configureSchema(parameterModel.getSchema(), parameterType); + parameterModel.setDescription(appendEnumDescription(description, parameterType)); + return parameterModel; + } + + @Override + public Schema customize(Schema schema, AnnotatedType type) { + Class> rawClass = resolveRawClass(type.getType()); + // 判断是否为 BaseEnum 的子类型 + if (!ClassUtil.isAssignable(BaseEnum.class, rawClass)) { + return schema; + } + // 自定义参数描述并封装参数配置 + configureSchema(schema, rawClass); + schema.setDescription(appendEnumDescription(schema.getDescription(), rawClass)); + return schema; + } + + /** + * 封装 Schema 配置 + * + * @param schema Schema + * @param enumClass 枚举类型 + */ + private void configureSchema(Schema schema, Class> enumClass) { + BaseEnum[] enums = (BaseEnum[])enumClass.getEnumConstants(); + List+ * Licensed under the GNU LESSER GENERAL PUBLIC LICENSE 3.0; + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + *
+ * http://www.gnu.org/licenses/lgpl.html + *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
package top.continew.starter.apidoc.util;
-import cn.hutool.core.collection.CollUtil;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.Arrays;
-import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.Map;
-import java.util.Objects;
-import java.util.Set;
-import java.util.function.Function;
import java.util.stream.Collectors;
import org.springframework.web.bind.annotation.RestController;
import top.continew.starter.core.enums.BaseEnum;
@@ -17,116 +28,91 @@ import top.continew.starter.core.enums.BaseEnum;
/**
* 接口文档工具类
*
- * @Author echo
- * @date 2024/07/31
+ * @author echo
+ * @since 2.5.2
*/
public class DocUtils {
- private DocUtils() {
- }
+ private DocUtils() {
+ }
- /**
- * 获取枚举值类型
- *
- * @param enumClass 枚举类型
- * @return 枚举值类型
- */
- public static String getEnumValueTypeAsString(Class> enumClass) {
- // 获取枚举类实现的所有接口
- Type[] interfaces = enumClass.getGenericInterfaces();
- // 定义枚举值类型的映射
- Map