From ecf7420823f036d2cae731cce2bda732b6b311b9 Mon Sep 17 00:00:00 2001 From: Sean Yang Date: Tue, 26 Nov 2024 12:32:25 +0800 Subject: [PATCH] Enhance parameter resolve --- .../rest/argument/CompositeArgumentResolver.java | 9 +++++++-- .../protocol/tri/rest/mapping/meta/BeanMeta.java | 3 +++ .../rpc/protocol/tri/rest/openapi/Helper.java | 7 ++++++- .../rpc/protocol/tri/rest/util/TypeUtils.java | 16 +++++++++++++--- 4 files changed, 29 insertions(+), 6 deletions(-) diff --git a/dubbo-rpc/dubbo-rpc-triple/src/main/java/org/apache/dubbo/rpc/protocol/tri/rest/argument/CompositeArgumentResolver.java b/dubbo-rpc/dubbo-rpc-triple/src/main/java/org/apache/dubbo/rpc/protocol/tri/rest/argument/CompositeArgumentResolver.java index ffc859b4e30..a3a4a77bd71 100644 --- a/dubbo-rpc/dubbo-rpc-triple/src/main/java/org/apache/dubbo/rpc/protocol/tri/rest/argument/CompositeArgumentResolver.java +++ b/dubbo-rpc/dubbo-rpc-triple/src/main/java/org/apache/dubbo/rpc/protocol/tri/rest/argument/CompositeArgumentResolver.java @@ -18,6 +18,7 @@ import org.apache.dubbo.remoting.http12.HttpRequest; import org.apache.dubbo.remoting.http12.HttpResponse; +import org.apache.dubbo.remoting.http12.rest.ParamType; import org.apache.dubbo.rpc.model.FrameworkModel; import org.apache.dubbo.rpc.protocol.tri.rest.Messages; import org.apache.dubbo.rpc.protocol.tri.rest.mapping.meta.AnnotationMeta; @@ -89,8 +90,12 @@ public NamedValueMeta getNamedValueMeta(ParameterMeta parameter) { } for (ArgumentResolver resolver : resolvers) { - if (resolver.accept(parameter) && resolver instanceof AbstractArgumentResolver) { - return ((AbstractArgumentResolver) resolver).getNamedValueMeta(parameter); + if (resolver.accept(parameter)) { + if (resolver instanceof AbstractArgumentResolver) { + return ((AbstractArgumentResolver) resolver).getNamedValueMeta(parameter); + } else { + return new NamedValueMeta().setParamType(ParamType.Attribute); + } } } diff --git a/dubbo-rpc/dubbo-rpc-triple/src/main/java/org/apache/dubbo/rpc/protocol/tri/rest/mapping/meta/BeanMeta.java b/dubbo-rpc/dubbo-rpc-triple/src/main/java/org/apache/dubbo/rpc/protocol/tri/rest/mapping/meta/BeanMeta.java index c689c2101a9..9d74e4ec6b1 100644 --- a/dubbo-rpc/dubbo-rpc-triple/src/main/java/org/apache/dubbo/rpc/protocol/tri/rest/mapping/meta/BeanMeta.java +++ b/dubbo-rpc/dubbo-rpc-triple/src/main/java/org/apache/dubbo/rpc/protocol/tri/rest/mapping/meta/BeanMeta.java @@ -242,6 +242,9 @@ public ConstructorParameterMeta[] getParameters() { private ConstructorParameterMeta[] initParameters(RestToolKit toolKit, String prefix, Constructor ct) { Parameter[] cps = ct.getParameters(); int len = cps.length; + if (len == 0) { + return new ConstructorParameterMeta[0]; + } String[] parameterNames = toolKit == null ? null : toolKit.getParameterNames(ct); ConstructorParameterMeta[] parameters = new ConstructorParameterMeta[len]; for (int i = 0; i < len; i++) { diff --git a/dubbo-rpc/dubbo-rpc-triple/src/main/java/org/apache/dubbo/rpc/protocol/tri/rest/openapi/Helper.java b/dubbo-rpc/dubbo-rpc-triple/src/main/java/org/apache/dubbo/rpc/protocol/tri/rest/openapi/Helper.java index 32fa725abf6..a435b1adb9f 100644 --- a/dubbo-rpc/dubbo-rpc-triple/src/main/java/org/apache/dubbo/rpc/protocol/tri/rest/openapi/Helper.java +++ b/dubbo-rpc/dubbo-rpc-triple/src/main/java/org/apache/dubbo/rpc/protocol/tri/rest/openapi/Helper.java @@ -81,7 +81,12 @@ public static Collection guessHttpMethod(MethodMeta method) { if (GET.name().equals(httpMethod)) { for (ParameterMeta parameter : method.getParameters()) { ParamType paramType = parameter.getNamedValueMeta().paramType(); - if (paramType != null) { + if (paramType == null) { + if (parameter.isSimple()) { + continue; + } + return Arrays.asList(GET.name(), POST.name()); + } else { switch (paramType) { case Form: case Part: diff --git a/dubbo-rpc/dubbo-rpc-triple/src/main/java/org/apache/dubbo/rpc/protocol/tri/rest/util/TypeUtils.java b/dubbo-rpc/dubbo-rpc-triple/src/main/java/org/apache/dubbo/rpc/protocol/tri/rest/util/TypeUtils.java index 54ddde9eedf..a96f42aacee 100644 --- a/dubbo-rpc/dubbo-rpc-triple/src/main/java/org/apache/dubbo/rpc/protocol/tri/rest/util/TypeUtils.java +++ b/dubbo-rpc/dubbo-rpc-triple/src/main/java/org/apache/dubbo/rpc/protocol/tri/rest/util/TypeUtils.java @@ -20,6 +20,7 @@ import org.apache.dubbo.common.utils.ArrayUtils; import org.apache.dubbo.common.utils.ClassUtils; import org.apache.dubbo.common.utils.ConcurrentHashSet; +import org.apache.dubbo.common.utils.StringUtils; import java.io.File; import java.lang.reflect.Array; @@ -128,14 +129,23 @@ public static List getSystemPrefixes() { } public static void addSystemPrefixes(String... prefixes) { - SYSTEM_PREFIXES.addAll(Arrays.asList(prefixes)); + for (String prefix : prefixes) { + if (StringUtils.isNotEmpty(prefix)) { + SYSTEM_PREFIXES.add(prefix); + } + } } public static boolean isSystemType(Class type) { String name = type.getName(); List systemPrefixes = getSystemPrefixes(); - for (int i = 0, size = systemPrefixes.size(); i < size; i++) { - if (name.startsWith(systemPrefixes.get(i))) { + for (int i = systemPrefixes.size() - 1; i >= 0; i--) { + String prefix = systemPrefixes.get(i); + if (prefix.charAt(0) == '!') { + if (name.regionMatches(0, prefix, 1, prefix.length() - 1)) { + return false; + } + } else if (name.startsWith(prefix)) { return true; } }