You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@dubbo.apache.org by li...@apache.org on 2021/02/04 06:55:08 UTC

[dubbo-spi-extensions] 42/44: 增加:枚举类型设置了允许值后,检查设置的允许值是否在枚举值中

This is an automated email from the ASF dual-hosted git repository.

liujun pushed a commit to branch 2.7.x
in repository https://gitbox.apache.org/repos/asf/dubbo-spi-extensions.git

commit 69b3d1d35a59f6d8a56ced5ec903018730a49411
Author: qq213539 <21...@qq.com>
AuthorDate: Wed Feb 3 16:10:19 2021 +0800

    增加:枚举类型设置了允许值后,检查设置的允许值是否在枚举值中
---
 .../core/DubboApiDocsAnnotationScanner.java        | 39 +++++++++++++++-------
 1 file changed, 27 insertions(+), 12 deletions(-)

diff --git a/dubbo-api-docs/dubbo-api-docs-core/src/main/java/org/apache/dubbo/apidocs/core/DubboApiDocsAnnotationScanner.java b/dubbo-api-docs/dubbo-api-docs-core/src/main/java/org/apache/dubbo/apidocs/core/DubboApiDocsAnnotationScanner.java
index 2c70a59..f9fb930 100644
--- a/dubbo-api-docs/dubbo-api-docs-core/src/main/java/org/apache/dubbo/apidocs/core/DubboApiDocsAnnotationScanner.java
+++ b/dubbo-api-docs/dubbo-api-docs-core/src/main/java/org/apache/dubbo/apidocs/core/DubboApiDocsAnnotationScanner.java
@@ -411,22 +411,37 @@ public class DubboApiDocsAnnotationScanner implements ApplicationListener<Applic
         } else if (Enum.class.isAssignableFrom(classType)) {
             // process enum
             param.setHtmlType(HtmlTypeEnum.SELECT);
+
+            Object[] enumConstants = classType.getEnumConstants();
+            String[] enumValues = new String[enumConstants.length];
+            try {
+                Method getNameMethod = classType.getMethod(METHOD_NAME_NAME);
+                for (int i = 0; i < enumConstants.length; i++) {
+                    Object obj = enumConstants[i];
+                    enumValues[i] = (String) getNameMethod.invoke(obj);
+                }
+            } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
+                LOG.error(e.getMessage(), e);
+            }
+
             if (!hasAllowableValues) {
                 // If there is no optional value, it is taken from the enumeration.
-                //TODO If there is an optional value, it is necessary
-                // to check whether the optional value matches the enumeration. It is add it later
-                Object[] enumConstants = classType.getEnumConstants();
-                String[] enumAllowableValues = new String[enumConstants.length];
-                try {
-                    Method getNameMethod = classType.getMethod(METHOD_NAME_NAME);
-                    for (int i = 0; i < enumConstants.length; i++) {
-                        Object obj = enumConstants[i];
-                        enumAllowableValues[i] = (String) getNameMethod.invoke(obj);
+                param.setAllowableValues(enumValues);
+            } else {
+                // If there has allowable values, it is necessary to check whether the allowable values matches the enumeration.
+                boolean checkSuccess = true;
+                String[] allowableValues = param.getAllowableValues();
+                for (String allowableValue : allowableValues) {
+                    for (String enumValue : enumValues) {
+                        if (!StringUtils.equals(enumValue, allowableValue)) {
+                            checkSuccess = false;
+                        }
                     }
-                } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
-                    LOG.error(e.getMessage(), e);
                 }
-                param.setAllowableValues(enumAllowableValues);
+                if (!checkSuccess) {
+                    LOG.error("The allowed value in the @RequestParam annotation does not match the " +
+                            "annotated enumeration " + classType.getCanonicalName() + ", please check!");
+                }
             }
             processed = true;
         }