This is an automated email from the ASF dual-hosted git repository.
zhangzicheng pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/shenyu.git
The following commit(s) were added to refs/heads/master by this push:
new 21f4dbae2 feature: api doc client annotation add generated attribute
to control whether it is necessary to generate in admin. (#4845)
21f4dbae2 is described below
commit 21f4dbae2e29f9a01a66ac37169d97507f9330a0
Author: zhengpeng <[email protected]>
AuthorDate: Fri Jul 14 14:01:35 2023 +0800
feature: api doc client annotation add generated attribute to control
whether it is necessary to generate in admin. (#4845)
Co-authored-by: xiaoyu <[email protected]>
---
.../shenyu/client/apidocs/annotations/ApiDoc.java | 6 ++++++
.../client/apidocs/annotations/ApiModule.java | 5 +++++
.../AbstractContextRefreshedEventListener.java | 21 +++++++++++++++------
3 files changed, 26 insertions(+), 6 deletions(-)
diff --git
a/shenyu-client/shenyu-client-api-docs-annotations/src/main/java/org/apache/shenyu/client/apidocs/annotations/ApiDoc.java
b/shenyu-client/shenyu-client-api-docs-annotations/src/main/java/org/apache/shenyu/client/apidocs/annotations/ApiDoc.java
index 7413f0f48..8a0d34213 100644
---
a/shenyu-client/shenyu-client-api-docs-annotations/src/main/java/org/apache/shenyu/client/apidocs/annotations/ApiDoc.java
+++
b/shenyu-client/shenyu-client-api-docs-annotations/src/main/java/org/apache/shenyu/client/apidocs/annotations/ApiDoc.java
@@ -44,4 +44,10 @@ public @interface ApiDoc {
* @return tags
*/
String[] tags() default "";
+
+ /**
+ * control whether it is necessary to generate in admin.
+ * @return generated
+ */
+ boolean generated() default true;
}
diff --git
a/shenyu-client/shenyu-client-api-docs-annotations/src/main/java/org/apache/shenyu/client/apidocs/annotations/ApiModule.java
b/shenyu-client/shenyu-client-api-docs-annotations/src/main/java/org/apache/shenyu/client/apidocs/annotations/ApiModule.java
index 5cd24101e..2be6a6a9c 100644
---
a/shenyu-client/shenyu-client-api-docs-annotations/src/main/java/org/apache/shenyu/client/apidocs/annotations/ApiModule.java
+++
b/shenyu-client/shenyu-client-api-docs-annotations/src/main/java/org/apache/shenyu/client/apidocs/annotations/ApiModule.java
@@ -38,4 +38,9 @@ public @interface ApiModule {
*/
String value();
+ /**
+ * control whether it is necessary to generate in admin.
+ * @return generated
+ */
+ boolean generated() default true;
}
diff --git
a/shenyu-client/shenyu-client-core/src/main/java/org/apache/shenyu/client/core/client/AbstractContextRefreshedEventListener.java
b/shenyu-client/shenyu-client-core/src/main/java/org/apache/shenyu/client/core/client/AbstractContextRefreshedEventListener.java
index 17849264b..bae61442e 100644
---
a/shenyu-client/shenyu-client-core/src/main/java/org/apache/shenyu/client/core/client/AbstractContextRefreshedEventListener.java
+++
b/shenyu-client/shenyu-client-core/src/main/java/org/apache/shenyu/client/core/client/AbstractContextRefreshedEventListener.java
@@ -57,6 +57,7 @@ import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Arrays;
+import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Objects;
@@ -138,20 +139,25 @@ public abstract class
AbstractContextRefreshedEventListener<T, A extends Annotat
private void handleApiDoc(final Object bean, final Map<String, T> beans) {
Class<?> apiModuleClass = AopUtils.isAopProxy(bean) ?
AopUtils.getTargetClass(bean) : bean.getClass();
- final Method[] methods =
ReflectionUtils.getUniqueDeclaredMethods(apiModuleClass);
- for (Method method : methods) {
- if (method.isAnnotationPresent(ApiDoc.class)) {
- List<ApiDocRegisterDTO> apis = buildApiDocDTO(bean, method,
beans);
- for (ApiDocRegisterDTO apiDocRegisterDTO : apis) {
- publisher.publishEvent(apiDocRegisterDTO);
+ ApiModule apiModule =
apiModuleClass.getDeclaredAnnotation(ApiModule.class);
+ if (Objects.nonNull(apiModule) && apiModule.generated()) {
+ final Method[] methods =
ReflectionUtils.getUniqueDeclaredMethods(apiModuleClass);
+ for (Method method : methods) {
+ if (method.isAnnotationPresent(ApiDoc.class)) {
+ List<ApiDocRegisterDTO> apis = buildApiDocDTO(bean,
method, beans);
+ for (ApiDocRegisterDTO apiDocRegisterDTO : apis) {
+ publisher.publishEvent(apiDocRegisterDTO);
+ }
}
}
}
}
private List<ApiDocRegisterDTO> buildApiDocDTO(final Object bean, final
Method method, final Map<String, T> beans) {
+ AtomicBoolean generated = new AtomicBoolean(false);
Pair<String, List<String>> pairs =
Stream.of(method.getDeclaredAnnotations()).filter(ApiDoc.class::isInstance).findAny().map(item
-> {
ApiDoc apiDoc = (ApiDoc) item;
+ generated.set(apiDoc.generated());
String[] tags = apiDoc.tags();
List<String> tagsList = new ArrayList<>();
if (tags.length > 0 && StringUtils.isNotBlank(tags[0])) {
@@ -159,6 +165,9 @@ public abstract class
AbstractContextRefreshedEventListener<T, A extends Annotat
}
return Pair.of(apiDoc.desc(), tagsList);
}).orElse(Pair.of("", new ArrayList<>()));
+ if (!generated.get()) {
+ return Collections.emptyList();
+ }
Class<?> clazz = AopUtils.isAopProxy(bean) ?
AopUtils.getTargetClass(bean) : bean.getClass();
String superPath = buildApiSuperPath(clazz,
AnnotatedElementUtils.findMergedAnnotation(clazz, getAnnotationType()));
if (superPath.contains("*")) {