You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@dubbo.apache.org by ky...@apache.org on 2021/09/18 09:56:15 UTC

[dubbo] branch 3.0 updated: [3.0] Fix appendAttribute key (#8847)

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

kylixs pushed a commit to branch 3.0
in repository https://gitbox.apache.org/repos/asf/dubbo.git


The following commit(s) were added to refs/heads/3.0 by this push:
     new baec4b3  [3.0] Fix appendAttribute key (#8847)
baec4b3 is described below

commit baec4b3425449da4a6ac0c81f1f47dc142b45fa3
Author: 吴治国 <ch...@startdt.com>
AuthorDate: Sat Sep 18 17:56:03 2021 +0800

    [3.0] Fix appendAttribute key (#8847)
    
     Fix attribute name of config metadata
---
 .../org/apache/dubbo/config/AbstractConfig.java    |  8 ++++++-
 .../java/org/apache/dubbo/config/Constants.java    | 23 ++++++++++++++-----
 .../java/org/apache/dubbo/config/MethodConfig.java | 24 ++++++++++----------
 .../org/apache/dubbo/config/MethodConfigTest.java  | 24 ++++++++++----------
 .../org/apache/dubbo/config/MethodConfigTest.java  | 26 +++++++++++-----------
 5 files changed, 61 insertions(+), 44 deletions(-)

diff --git a/dubbo-common/src/main/java/org/apache/dubbo/config/AbstractConfig.java b/dubbo-common/src/main/java/org/apache/dubbo/config/AbstractConfig.java
index 7482510..1cfcb4b 100644
--- a/dubbo-common/src/main/java/org/apache/dubbo/config/AbstractConfig.java
+++ b/dubbo-common/src/main/java/org/apache/dubbo/config/AbstractConfig.java
@@ -176,12 +176,14 @@ public abstract class AbstractConfig implements Serializable {
                     if (method.getReturnType() == Object.class) {
                         continue;
                     }
-                    String key = calculatePropertyFromGetter(name);
+                    String key;
                     Parameter parameter = method.getAnnotation(Parameter.class);
                     if (asParameters) {
                         if (parameter != null && parameter.excluded()) {
                             continue;
                         }
+                        // get parameter key
+                        key = calculatePropertyFromGetter(name);
                         if (parameter != null && parameter.key().length() > 0) {
                             key = parameter.key();
                         }
@@ -190,6 +192,10 @@ public abstract class AbstractConfig implements Serializable {
                         if (parameter != null && !parameter.attribute()) {
                             continue;
                         }
+                        // get attribute name
+                        String propertyName = calculateAttributeFromGetter(method.getName());
+                        // convert camelCase/snake_case to kebab-case
+                        key = StringUtils.convertToSplitName(propertyName, "-");
                     }
                     Object value = method.invoke(config);
                     String str = String.valueOf(value).trim();
diff --git a/dubbo-common/src/main/java/org/apache/dubbo/config/Constants.java b/dubbo-common/src/main/java/org/apache/dubbo/config/Constants.java
index a08d014..4d6d783 100644
--- a/dubbo-common/src/main/java/org/apache/dubbo/config/Constants.java
+++ b/dubbo-common/src/main/java/org/apache/dubbo/config/Constants.java
@@ -93,18 +93,29 @@ public interface Constants {
 
     String SCOPE_NONE = "none";
 
+    String ON_INVOKE_METHOD_PARAMETER_KEY = "oninvoke.method";
 
-    String ON_INVOKE_METHOD_KEY = "oninvoke.method";
+    String ON_RETURN_METHOD_PARAMETER_KEY = "onreturn.method";
 
-    String ON_RETURN_METHOD_KEY = "onreturn.method";
+    String ON_THROW_METHOD_PARAMETER_KEY = "onthrow.method";
 
-    String ON_THROW_METHOD_KEY = "onthrow.method";
+    String ON_INVOKE_INSTANCE_PARAMETER_KEY = "oninvoke.instance";
 
-    String ON_INVOKE_INSTANCE_KEY = "oninvoke.instance";
+    String ON_RETURN_INSTANCE_PARAMETER_KEY = "onreturn.instance";
 
-    String ON_RETURN_INSTANCE_KEY = "onreturn.instance";
+    String ON_THROW_INSTANCE_PARAMETER_KEY = "onthrow.instance";
 
-    String ON_THROW_INSTANCE_KEY = "onthrow.instance";
+    String ON_INVOKE_METHOD_ATTRIBUTE_KEY = "oninvoke-method";
+
+    String ON_RETURN_METHOD_ATTRIBUTE_KEY = "onreturn-method";
+
+    String ON_THROW_METHOD_ATTRIBUTE_KEY = "onthrow-method";
+
+    String ON_INVOKE_INSTANCE_ATTRIBUTE_KEY = "oninvoke-instance";
+
+    String ON_RETURN_INSTANCE_ATTRIBUTE_KEY = "onreturn-instance";
+
+    String ON_THROW_INSTANCE_ATTRIBUTE_KEY = "onthrow-instance";
 
 
     // FIXME: is this still useful?
diff --git a/dubbo-common/src/main/java/org/apache/dubbo/config/MethodConfig.java b/dubbo-common/src/main/java/org/apache/dubbo/config/MethodConfig.java
index 9624970..1318e7f 100644
--- a/dubbo-common/src/main/java/org/apache/dubbo/config/MethodConfig.java
+++ b/dubbo-common/src/main/java/org/apache/dubbo/config/MethodConfig.java
@@ -30,12 +30,12 @@ import java.util.ArrayList;
 import java.util.Collections;
 import java.util.List;
 
-import static org.apache.dubbo.config.Constants.ON_INVOKE_INSTANCE_KEY;
-import static org.apache.dubbo.config.Constants.ON_INVOKE_METHOD_KEY;
-import static org.apache.dubbo.config.Constants.ON_RETURN_INSTANCE_KEY;
-import static org.apache.dubbo.config.Constants.ON_RETURN_METHOD_KEY;
-import static org.apache.dubbo.config.Constants.ON_THROW_INSTANCE_KEY;
-import static org.apache.dubbo.config.Constants.ON_THROW_METHOD_KEY;
+import static org.apache.dubbo.config.Constants.ON_INVOKE_INSTANCE_PARAMETER_KEY;
+import static org.apache.dubbo.config.Constants.ON_INVOKE_METHOD_PARAMETER_KEY;
+import static org.apache.dubbo.config.Constants.ON_RETURN_INSTANCE_PARAMETER_KEY;
+import static org.apache.dubbo.config.Constants.ON_RETURN_METHOD_PARAMETER_KEY;
+import static org.apache.dubbo.config.Constants.ON_THROW_INSTANCE_PARAMETER_KEY;
+import static org.apache.dubbo.config.Constants.ON_THROW_METHOD_PARAMETER_KEY;
 
 /**
  * The method configuration
@@ -399,7 +399,7 @@ public class MethodConfig extends AbstractMethodConfig {
         this.sticky = sticky;
     }
 
-    @Parameter(key = ON_RETURN_INSTANCE_KEY, excluded = true, attribute = true)
+    @Parameter(key = ON_RETURN_INSTANCE_PARAMETER_KEY, excluded = true, attribute = true)
     public Object getOnreturn() {
         return onreturn;
     }
@@ -408,7 +408,7 @@ public class MethodConfig extends AbstractMethodConfig {
         this.onreturn = onreturn;
     }
 
-    @Parameter(key = ON_RETURN_METHOD_KEY, excluded = true, attribute = true)
+    @Parameter(key = ON_RETURN_METHOD_PARAMETER_KEY, excluded = true, attribute = true)
     public String getOnreturnMethod() {
         return onreturnMethod;
     }
@@ -417,7 +417,7 @@ public class MethodConfig extends AbstractMethodConfig {
         this.onreturnMethod = onreturnMethod;
     }
 
-    @Parameter(key = ON_THROW_INSTANCE_KEY, excluded = true, attribute = true)
+    @Parameter(key = ON_THROW_INSTANCE_PARAMETER_KEY, excluded = true, attribute = true)
     public Object getOnthrow() {
         return onthrow;
     }
@@ -426,7 +426,7 @@ public class MethodConfig extends AbstractMethodConfig {
         this.onthrow = onthrow;
     }
 
-    @Parameter(key = ON_THROW_METHOD_KEY, excluded = true, attribute = true)
+    @Parameter(key = ON_THROW_METHOD_PARAMETER_KEY, excluded = true, attribute = true)
     public String getOnthrowMethod() {
         return onthrowMethod;
     }
@@ -435,7 +435,7 @@ public class MethodConfig extends AbstractMethodConfig {
         this.onthrowMethod = onthrowMethod;
     }
 
-    @Parameter(key = ON_INVOKE_INSTANCE_KEY, excluded = true, attribute = true)
+    @Parameter(key = ON_INVOKE_INSTANCE_PARAMETER_KEY, excluded = true, attribute = true)
     public Object getOninvoke() {
         return oninvoke;
     }
@@ -444,7 +444,7 @@ public class MethodConfig extends AbstractMethodConfig {
         this.oninvoke = oninvoke;
     }
 
-    @Parameter(key = ON_INVOKE_METHOD_KEY, excluded = true, attribute = true)
+    @Parameter(key = ON_INVOKE_METHOD_PARAMETER_KEY, excluded = true, attribute = true)
     public String getOninvokeMethod() {
         return oninvokeMethod;
     }
diff --git a/dubbo-compatible/src/test/java/org/apache/dubbo/config/MethodConfigTest.java b/dubbo-compatible/src/test/java/org/apache/dubbo/config/MethodConfigTest.java
index dd33eaa..94156a5 100644
--- a/dubbo-compatible/src/test/java/org/apache/dubbo/config/MethodConfigTest.java
+++ b/dubbo-compatible/src/test/java/org/apache/dubbo/config/MethodConfigTest.java
@@ -29,12 +29,12 @@ import java.util.Collections;
 import java.util.HashMap;
 import java.util.Map;
 
-import static org.apache.dubbo.config.Constants.ON_INVOKE_INSTANCE_KEY;
-import static org.apache.dubbo.config.Constants.ON_INVOKE_METHOD_KEY;
-import static org.apache.dubbo.config.Constants.ON_RETURN_INSTANCE_KEY;
-import static org.apache.dubbo.config.Constants.ON_RETURN_METHOD_KEY;
-import static org.apache.dubbo.config.Constants.ON_THROW_INSTANCE_KEY;
-import static org.apache.dubbo.config.Constants.ON_THROW_METHOD_KEY;
+import static org.apache.dubbo.config.Constants.ON_INVOKE_INSTANCE_ATTRIBUTE_KEY;
+import static org.apache.dubbo.config.Constants.ON_INVOKE_METHOD_ATTRIBUTE_KEY;
+import static org.apache.dubbo.config.Constants.ON_RETURN_INSTANCE_ATTRIBUTE_KEY;
+import static org.apache.dubbo.config.Constants.ON_RETURN_METHOD_ATTRIBUTE_KEY;
+import static org.apache.dubbo.config.Constants.ON_THROW_INSTANCE_ATTRIBUTE_KEY;
+import static org.apache.dubbo.config.Constants.ON_THROW_METHOD_ATTRIBUTE_KEY;
 import static org.hamcrest.MatcherAssert.assertThat;
 import static org.hamcrest.Matchers.contains;
 import static org.hamcrest.Matchers.equalTo;
@@ -124,7 +124,7 @@ public class MethodConfigTest {
         assertThat(method.getOnreturn(), equalTo((Object) "on-return-object"));
         Map<String, String> attributes = new HashMap<>();
         MethodConfig.appendAttributes(attributes, method);
-        assertThat(attributes, hasEntry((Object) ON_RETURN_INSTANCE_KEY, (Object) "on-return-object"));
+        assertThat(attributes, hasEntry((Object) ON_RETURN_INSTANCE_ATTRIBUTE_KEY, (Object) "on-return-object"));
         Map<String, String> parameters = new HashMap<String, String>();
         MethodConfig.appendParameters(parameters, method);
         assertThat(parameters.size(), is(0));
@@ -137,7 +137,7 @@ public class MethodConfigTest {
         assertThat(method.getOnreturnMethod(), equalTo("on-return-method"));
         Map<String, String> attributes = new HashMap<>();
         MethodConfig.appendAttributes(attributes, method);
-        assertThat(attributes, hasEntry((Object) ON_RETURN_METHOD_KEY, (Object) "on-return-method"));
+        assertThat(attributes, hasEntry((Object) ON_RETURN_METHOD_ATTRIBUTE_KEY, (Object) "on-return-method"));
         Map<String, String> parameters = new HashMap<String, String>();
         MethodConfig.appendParameters(parameters, method);
         assertThat(parameters.size(), is(0));
@@ -150,7 +150,7 @@ public class MethodConfigTest {
         assertThat(method.getOnthrow(), equalTo((Object) "on-throw-object"));
         Map<String, String> attributes = new HashMap<>();
         MethodConfig.appendAttributes(attributes, method);
-        assertThat(attributes, hasEntry((Object) ON_THROW_INSTANCE_KEY, (Object) "on-throw-object"));
+        assertThat(attributes, hasEntry((Object) ON_THROW_INSTANCE_ATTRIBUTE_KEY, (Object) "on-throw-object"));
         Map<String, String> parameters = new HashMap<String, String>();
         MethodConfig.appendParameters(parameters, method);
         assertThat(parameters.size(), is(0));
@@ -163,7 +163,7 @@ public class MethodConfigTest {
         assertThat(method.getOnthrowMethod(), equalTo("on-throw-method"));
         Map<String, String> attributes = new HashMap<>();
         MethodConfig.appendAttributes(attributes, method);
-        assertThat(attributes, hasEntry((Object) ON_THROW_METHOD_KEY, (Object) "on-throw-method"));
+        assertThat(attributes, hasEntry((Object) ON_THROW_METHOD_ATTRIBUTE_KEY, (Object) "on-throw-method"));
         Map<String, String> parameters = new HashMap<String, String>();
         MethodConfig.appendParameters(parameters, method);
         assertThat(parameters.size(), is(0));
@@ -176,7 +176,7 @@ public class MethodConfigTest {
         assertThat(method.getOninvoke(), equalTo((Object) "on-invoke-object"));
         Map<String, String> attributes = new HashMap<>();
         MethodConfig.appendAttributes(attributes, method);
-        assertThat(attributes, hasEntry((Object) ON_INVOKE_INSTANCE_KEY, (Object) "on-invoke-object"));
+        assertThat(attributes, hasEntry((Object) ON_INVOKE_INSTANCE_ATTRIBUTE_KEY, (Object) "on-invoke-object"));
         Map<String, String> parameters = new HashMap<String, String>();
         MethodConfig.appendParameters(parameters, method);
         assertThat(parameters.size(), is(0));
@@ -189,7 +189,7 @@ public class MethodConfigTest {
         assertThat(method.getOninvokeMethod(), equalTo("on-invoke-method"));
         Map<String, String> attributes = new HashMap<>();
         MethodConfig.appendAttributes(attributes, method);
-        assertThat(attributes, hasEntry((Object) ON_INVOKE_METHOD_KEY, (Object) "on-invoke-method"));
+        assertThat(attributes, hasEntry((Object) ON_INVOKE_METHOD_ATTRIBUTE_KEY, (Object) "on-invoke-method"));
         Map<String, String> parameters = new HashMap<String, String>();
         MethodConfig.appendParameters(parameters, method);
         assertThat(parameters.size(), is(0));
diff --git a/dubbo-config/dubbo-config-api/src/test/java/org/apache/dubbo/config/MethodConfigTest.java b/dubbo-config/dubbo-config-api/src/test/java/org/apache/dubbo/config/MethodConfigTest.java
index e27a359..23c7a60 100644
--- a/dubbo-config/dubbo-config-api/src/test/java/org/apache/dubbo/config/MethodConfigTest.java
+++ b/dubbo-config/dubbo-config-api/src/test/java/org/apache/dubbo/config/MethodConfigTest.java
@@ -20,10 +20,10 @@ package org.apache.dubbo.config;
 import org.apache.dubbo.config.annotation.Argument;
 import org.apache.dubbo.config.annotation.Method;
 import org.apache.dubbo.config.annotation.Reference;
-
 import org.apache.dubbo.config.api.DemoService;
 import org.apache.dubbo.config.bootstrap.DubboBootstrap;
 import org.apache.dubbo.config.provider.impl.DemoServiceImpl;
+
 import org.hamcrest.Matchers;
 import org.junit.jupiter.api.AfterEach;
 import org.junit.jupiter.api.Assertions;
@@ -37,12 +37,12 @@ import java.util.LinkedHashMap;
 import java.util.List;
 import java.util.Map;
 
-import static org.apache.dubbo.config.Constants.ON_INVOKE_INSTANCE_KEY;
-import static org.apache.dubbo.config.Constants.ON_INVOKE_METHOD_KEY;
-import static org.apache.dubbo.config.Constants.ON_RETURN_INSTANCE_KEY;
-import static org.apache.dubbo.config.Constants.ON_RETURN_METHOD_KEY;
-import static org.apache.dubbo.config.Constants.ON_THROW_INSTANCE_KEY;
-import static org.apache.dubbo.config.Constants.ON_THROW_METHOD_KEY;
+import static org.apache.dubbo.config.Constants.ON_INVOKE_INSTANCE_ATTRIBUTE_KEY;
+import static org.apache.dubbo.config.Constants.ON_INVOKE_METHOD_ATTRIBUTE_KEY;
+import static org.apache.dubbo.config.Constants.ON_RETURN_INSTANCE_ATTRIBUTE_KEY;
+import static org.apache.dubbo.config.Constants.ON_RETURN_METHOD_ATTRIBUTE_KEY;
+import static org.apache.dubbo.config.Constants.ON_THROW_INSTANCE_ATTRIBUTE_KEY;
+import static org.apache.dubbo.config.Constants.ON_THROW_METHOD_ATTRIBUTE_KEY;
 import static org.hamcrest.MatcherAssert.assertThat;
 import static org.hamcrest.Matchers.contains;
 import static org.hamcrest.Matchers.equalTo;
@@ -186,7 +186,7 @@ public class MethodConfigTest {
         assertThat(method.getOnreturn(), equalTo("on-return-object"));
         Map<String, String> attributes = new HashMap<>();
         MethodConfig.appendAttributes(attributes, method);
-        assertThat(attributes, hasEntry(ON_RETURN_INSTANCE_KEY, "on-return-object"));
+        assertThat(attributes, hasEntry(ON_RETURN_INSTANCE_ATTRIBUTE_KEY, "on-return-object"));
         Map<String, String> parameters = new HashMap<String, String>();
         MethodConfig.appendParameters(parameters, method);
         assertThat(parameters.size(), is(0));
@@ -199,7 +199,7 @@ public class MethodConfigTest {
         assertThat(method.getOnreturnMethod(), equalTo("on-return-method"));
         Map<String, String> attributes = new HashMap<>();
         MethodConfig.appendAttributes(attributes, method);
-        assertThat(attributes, hasEntry((Object) ON_RETURN_METHOD_KEY, (Object) "on-return-method"));
+        assertThat(attributes, hasEntry((Object) ON_RETURN_METHOD_ATTRIBUTE_KEY, (Object) "on-return-method"));
         Map<String, String> parameters = new HashMap<String, String>();
         MethodConfig.appendParameters(parameters, method);
         assertThat(parameters.size(), is(0));
@@ -212,7 +212,7 @@ public class MethodConfigTest {
         assertThat(method.getOnthrow(), equalTo((Object) "on-throw-object"));
         Map<String, String> attributes = new HashMap<>();
         MethodConfig.appendAttributes(attributes, method);
-        assertThat(attributes, hasEntry((Object) ON_THROW_INSTANCE_KEY, (Object) "on-throw-object"));
+        assertThat(attributes, hasEntry((Object) ON_THROW_INSTANCE_ATTRIBUTE_KEY, (Object) "on-throw-object"));
         Map<String, String> parameters = new HashMap<String, String>();
         MethodConfig.appendParameters(parameters, method);
         assertThat(parameters.size(), is(0));
@@ -225,7 +225,7 @@ public class MethodConfigTest {
         assertThat(method.getOnthrowMethod(), equalTo("on-throw-method"));
         Map<String, String> attributes = new HashMap<>();
         MethodConfig.appendAttributes(attributes, method);
-        assertThat(attributes, hasEntry((Object) ON_THROW_METHOD_KEY, (Object) "on-throw-method"));
+        assertThat(attributes, hasEntry((Object) ON_THROW_METHOD_ATTRIBUTE_KEY, (Object) "on-throw-method"));
         Map<String, String> parameters = new HashMap<String, String>();
         MethodConfig.appendParameters(parameters, method);
         assertThat(parameters.size(), is(0));
@@ -238,7 +238,7 @@ public class MethodConfigTest {
         assertThat(method.getOninvoke(), equalTo((Object) "on-invoke-object"));
         Map<String, String> attributes = new HashMap<>();
         MethodConfig.appendAttributes(attributes, method);
-        assertThat(attributes, hasEntry((Object) ON_INVOKE_INSTANCE_KEY, (Object) "on-invoke-object"));
+        assertThat(attributes, hasEntry((Object) ON_INVOKE_INSTANCE_ATTRIBUTE_KEY, (Object) "on-invoke-object"));
         Map<String, String> parameters = new HashMap<String, String>();
         MethodConfig.appendParameters(parameters, method);
         assertThat(parameters.size(), is(0));
@@ -251,7 +251,7 @@ public class MethodConfigTest {
         assertThat(method.getOninvokeMethod(), equalTo("on-invoke-method"));
         Map<String, String> attributes = new HashMap<>();
         MethodConfig.appendAttributes(attributes, method);
-        assertThat(attributes, hasEntry((Object) ON_INVOKE_METHOD_KEY, (Object) "on-invoke-method"));
+        assertThat(attributes, hasEntry((Object) ON_INVOKE_METHOD_ATTRIBUTE_KEY, (Object) "on-invoke-method"));
         Map<String, String> parameters = new HashMap<String, String>();
         MethodConfig.appendParameters(parameters, method);
         assertThat(parameters.size(), is(0));