You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shenyu.apache.org by zh...@apache.org on 2023/02/08 01:53:44 UTC

[shenyu] branch master updated: [type:refactor] refactor api-doc-client add tag (#4362)

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 50371b619 [type:refactor] refactor api-doc-client add tag (#4362)
50371b619 is described below

commit 50371b61975983ce6e8681bb01b714b3fc0a8f42
Author: zhengpeng <84...@qq.com>
AuthorDate: Wed Feb 8 09:53:33 2023 +0800

    [type:refactor] refactor api-doc-client add tag (#4362)
    
    * refactor api-doc-client add tag
---
 .../apache/shenyu/admin/model/entity/TagDO.java    |  1 +
 .../AbstractShenyuClientRegisterServiceImpl.java   | 29 +++++++++++++
 .../shenyu/client/apidocs/annotations/ApiDoc.java  |  6 +++
 .../AbstractContextRefreshedEventListener.java     | 17 ++++++--
 .../register/common/dto/ApiDocRegisterDTO.java     | 47 +++++++++++++++++++---
 5 files changed, 91 insertions(+), 9 deletions(-)

diff --git a/shenyu-admin/src/main/java/org/apache/shenyu/admin/model/entity/TagDO.java b/shenyu-admin/src/main/java/org/apache/shenyu/admin/model/entity/TagDO.java
index f8db2f9eb..f6c7336f4 100644
--- a/shenyu-admin/src/main/java/org/apache/shenyu/admin/model/entity/TagDO.java
+++ b/shenyu-admin/src/main/java/org/apache/shenyu/admin/model/entity/TagDO.java
@@ -168,6 +168,7 @@ public final class TagDO extends BaseDO {
                 tagDO.setDateCreated(currentTime);
             } else {
                 tagDO.setId(item.getId());
+                tagDO.setDateCreated(currentTime);
             }
             return tagDO;
         }).orElse(null);
diff --git a/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/register/AbstractShenyuClientRegisterServiceImpl.java b/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/register/AbstractShenyuClientRegisterServiceImpl.java
index 9a6288884..65ff9011a 100644
--- a/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/register/AbstractShenyuClientRegisterServiceImpl.java
+++ b/shenyu-admin/src/main/java/org/apache/shenyu/admin/service/register/AbstractShenyuClientRegisterServiceImpl.java
@@ -23,11 +23,14 @@ import org.apache.shenyu.admin.listener.DataChangedEvent;
 import org.apache.shenyu.admin.model.dto.ApiDTO;
 import org.apache.shenyu.admin.model.dto.RuleConditionDTO;
 import org.apache.shenyu.admin.model.dto.RuleDTO;
+import org.apache.shenyu.admin.model.dto.TagDTO;
 import org.apache.shenyu.admin.model.entity.SelectorDO;
+import org.apache.shenyu.admin.model.vo.TagVO;
 import org.apache.shenyu.admin.service.ApiService;
 import org.apache.shenyu.admin.service.MetaDataService;
 import org.apache.shenyu.admin.service.RuleService;
 import org.apache.shenyu.admin.service.SelectorService;
+import org.apache.shenyu.admin.service.TagService;
 import org.apache.shenyu.admin.service.impl.UpstreamCheckService;
 import org.apache.shenyu.admin.utils.CommonUpstreamUtils;
 import org.apache.shenyu.admin.utils.ShenyuResultMessage;
@@ -42,6 +45,7 @@ import org.apache.shenyu.common.enums.ParamTypeEnum;
 import org.apache.shenyu.common.exception.ShenyuException;
 import org.apache.shenyu.common.utils.PathUtils;
 import org.apache.shenyu.common.utils.PluginNameAdapter;
+import org.apache.shenyu.common.utils.UUIDUtils;
 import org.apache.shenyu.register.common.dto.ApiDocRegisterDTO;
 import org.apache.shenyu.register.common.dto.MetaDataRegisterDTO;
 import org.apache.shenyu.register.common.dto.URIRegisterDTO;
@@ -49,6 +53,7 @@ import org.apache.shenyu.register.common.enums.EventType;
 import org.springframework.context.ApplicationEventPublisher;
 
 import javax.annotation.Resource;
+import java.util.ArrayList;
 import java.util.Collections;
 import java.util.List;
 import java.util.Objects;
@@ -86,6 +91,9 @@ public abstract class AbstractShenyuClientRegisterServiceImpl extends FallbackSh
     @Resource
     private ApiService apiService;
 
+    @Resource
+    private TagService tagService;
+
     /**
      * Selector handler string.
      *
@@ -147,6 +155,27 @@ public abstract class AbstractShenyuClientRegisterServiceImpl extends FallbackSh
         if (apiDocRegisterDTO.getEventType().equals(EventType.REGISTER)) {
             ApiDTO apiDTO = buildApiDTO(apiDocRegisterDTO);
             apiService.deleteByApiPathHttpMethodRpcType(apiDTO.getApiPath(), apiDTO.getHttpMethod(), apiDTO.getRpcType());
+            List<String> tagsIds = new ArrayList<>();
+            List<String> tags = Collections.singletonList(apiDocRegisterDTO.getContextPath());
+            if (CollectionUtils.isNotEmpty(apiDocRegisterDTO.getTags())) {
+                tags = apiDocRegisterDTO.getTags();
+            }
+            for (String tag : tags) {
+                List<TagVO> byQuery = tagService.findByQuery(tag);
+                if (CollectionUtils.isNotEmpty(byQuery)) {
+                    tagsIds.addAll(byQuery.stream().map(TagVO::getId).collect(Collectors.toList()));
+                } else {
+                    TagDTO tagDTO = new TagDTO();
+                    String id = UUIDUtils.getInstance().generateShortUuid();
+                    tagDTO.setTagDesc(tag);
+                    tagDTO.setName(tag);
+                    tagDTO.setParentTagId(AdminConstants.TAG_ROOT_PARENT_ID);
+                    tagDTO.setId(id);
+                    tagService.create(tagDTO);
+                    tagsIds.add(id);
+                }
+            }
+            apiDTO.setTagIds(tagsIds);
             apiService.createOrUpdate(apiDTO);
         } else if (apiDocRegisterDTO.getEventType().equals(EventType.OFFLINE)) {
             String contextPath = apiDocRegisterDTO.getContextPath();
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 c89fe8c4c..7413f0f48 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
@@ -38,4 +38,10 @@ public @interface ApiDoc {
      * @return String
      */
     String desc();
+
+    /**
+     * tags.
+     * @return tags
+     */
+    String[] tags() default "";
 }
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 54f96cd18..98818252b 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
@@ -20,6 +20,7 @@ package org.apache.shenyu.client.core.client;
 import com.google.common.collect.Lists;
 import org.apache.commons.collections4.MapUtils;
 import org.apache.commons.lang3.StringUtils;
+import org.apache.commons.lang3.tuple.Pair;
 import org.apache.shenyu.client.apidocs.annotations.ApiDoc;
 import org.apache.shenyu.client.apidocs.annotations.ApiModule;
 import org.apache.shenyu.client.core.constant.ShenyuClientConstants;
@@ -50,6 +51,8 @@ import org.springframework.util.ReflectionUtils;
 
 import java.lang.annotation.Annotation;
 import java.lang.reflect.Method;
+import java.util.ArrayList;
+import java.util.Arrays;
 import java.util.List;
 import java.util.Map;
 import java.util.Objects;
@@ -140,10 +143,15 @@ public abstract class AbstractContextRefreshedEventListener<T, A extends Annotat
     }
 
     private List<ApiDocRegisterDTO> buildApiDocDTO(final Object bean, final Method method, final Map<String, T> beans) {
-        String apiDesc = Stream.of(method.getDeclaredAnnotations()).filter(item -> item instanceof ApiDoc).findAny().map(item -> {
+        Pair<String, List<String>> pairs = Stream.of(method.getDeclaredAnnotations()).filter(item -> item instanceof ApiDoc).findAny().map(item -> {
             ApiDoc apiDoc = (ApiDoc) item;
-            return apiDoc.desc();
-        }).orElse("");
+            String[] tags = apiDoc.tags();
+            List<String> tagsList = new ArrayList<>();
+            if (tags.length > 0 && StringUtils.isNotBlank(tags[0])) {
+                tagsList = Arrays.asList(tags);
+            }
+            return Pair.of(apiDoc.desc(), tagsList);
+        }).orElse(Pair.of("", new ArrayList<>()));
         Class<?> clazz = AopUtils.isAopProxy(bean) ? AopUtils.getTargetClass(bean) : bean.getClass();
         String superPath = buildApiSuperPath(clazz, AnnotatedElementUtils.findMergedAnnotation(clazz, getAnnotationType()));
         if (superPath.indexOf("*") > 0) {
@@ -173,7 +181,8 @@ public abstract class AbstractContextRefreshedEventListener<T, A extends Annotat
                         .document("{}")
                         .rpcType(sextet.getValue4().getName())
                         .version(sextet.getValue5())
-                        .apiDesc(apiDesc)
+                        .apiDesc(pairs.getLeft())
+                        .tags(pairs.getRight())
                         .apiPath(apiPath)
                         .apiSource(ApiSourceEnum.ANNOTATION_GENERATION.getValue())
                         .state(ApiStateEnum.PUBLISHED.getState())
diff --git a/shenyu-register-center/shenyu-register-common/src/main/java/org/apache/shenyu/register/common/dto/ApiDocRegisterDTO.java b/shenyu-register-center/shenyu-register-common/src/main/java/org/apache/shenyu/register/common/dto/ApiDocRegisterDTO.java
index 918a24e78..3c5d1ce5c 100644
--- a/shenyu-register-center/shenyu-register-common/src/main/java/org/apache/shenyu/register/common/dto/ApiDocRegisterDTO.java
+++ b/shenyu-register-center/shenyu-register-common/src/main/java/org/apache/shenyu/register/common/dto/ApiDocRegisterDTO.java
@@ -21,6 +21,7 @@ import org.apache.shenyu.register.common.enums.EventType;
 import org.apache.shenyu.register.common.type.DataType;
 import org.apache.shenyu.register.common.type.DataTypeParent;
 
+import java.util.List;
 import java.util.Objects;
 
 /**
@@ -98,6 +99,27 @@ public class ApiDocRegisterDTO implements DataTypeParent {
      */
     private EventType eventType;
 
+    /**
+     * tags.
+     */
+    private List<String> tags;
+
+    /**
+     * getTags.
+     * @return tags
+     */
+    public List<String> getTags() {
+        return tags;
+    }
+
+    /**
+     * setTags.
+     * @param tags tags
+     */
+    public void setTags(final List<String> tags) {
+        this.tags = tags;
+    }
+
     @Override
     public DataType getType() {
         return DataType.API_DOC;
@@ -362,15 +384,15 @@ public class ApiDocRegisterDTO implements DataTypeParent {
             return false;
         }
         ApiDocRegisterDTO that = (ApiDocRegisterDTO) o;
-        return Objects.equals(contextPath, that.contextPath) && Objects.equals(apiPath, that.apiPath) && Objects.equals(httpMethod, that.httpMethod)
-                && Objects.equals(consume, that.consume) && Objects.equals(produce, that.produce) && Objects.equals(version, that.version)
-                && Objects.equals(rpcType, that.rpcType) && Objects.equals(state, that.state) && Objects.equals(ext, that.ext) && Objects.equals(apiOwner, that.apiOwner)
-                && Objects.equals(apiDesc, that.apiDesc) && Objects.equals(apiSource, that.apiSource) && Objects.equals(document, that.document) && eventType == that.eventType;
+        return Objects.equals(contextPath, that.contextPath) && Objects.equals(apiPath, that.apiPath) && Objects.equals(httpMethod, that.httpMethod) && Objects.equals(consume, that.consume)
+                && Objects.equals(produce, that.produce) && Objects.equals(version, that.version) && Objects.equals(rpcType, that.rpcType) && Objects.equals(state, that.state)
+                && Objects.equals(ext, that.ext) && Objects.equals(apiOwner, that.apiOwner) && Objects.equals(apiDesc, that.apiDesc) && Objects.equals(apiSource, that.apiSource)
+                && Objects.equals(document, that.document) && eventType == that.eventType && Objects.equals(tags, that.tags);
     }
 
     @Override
     public int hashCode() {
-        return Objects.hash(contextPath, apiPath, httpMethod, consume, produce, version, rpcType, state, ext, apiOwner, apiDesc, apiSource, document, eventType);
+        return Objects.hash(contextPath, apiPath, httpMethod, consume, produce, version, rpcType, state, ext, apiOwner, apiDesc, apiSource, document, eventType, tags);
     }
 
     @Override
@@ -404,6 +426,8 @@ public class ApiDocRegisterDTO implements DataTypeParent {
                 + document
                 + ", eventType='"
                 + eventType
+                + ", tags='"
+                + tags
                 + '}';
     }
 
@@ -445,6 +469,8 @@ public class ApiDocRegisterDTO implements DataTypeParent {
 
         private EventType eventType;
 
+        private List<String> tags;
+
         private ApiDocRegisterDTOBuilder() {
         }
 
@@ -588,6 +614,16 @@ public class ApiDocRegisterDTO implements DataTypeParent {
             return this;
         }
 
+        /**
+         * build tags.
+         * @param tags tags
+         * @return ApiDocRegisterDTOBuilder
+         */
+        public ApiDocRegisterDTOBuilder tags(final List<String> tags) {
+            this.tags = tags;
+            return this;
+        }
+
         /**
          * build.
          * @return ApiDocRegisterDTO
@@ -608,6 +644,7 @@ public class ApiDocRegisterDTO implements DataTypeParent {
             apiDocRegisterDTO.setApiSource(apiSource);
             apiDocRegisterDTO.setDocument(document);
             apiDocRegisterDTO.setEventType(eventType);
+            apiDocRegisterDTO.setTags(tags);
             return apiDocRegisterDTO;
         }
     }