[
https://issues.apache.org/jira/browse/SCB-873?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16605456#comment-16605456
]
ASF GitHub Bot commented on SCB-873:
------------------------------------
wujimin closed pull request #884: [SCB-873] Make the validation result display
the parameter name instead of arg0, arg1
URL: https://github.com/apache/incubator-servicecomb-java-chassis/pull/884
This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:
As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):
diff --git
a/demo/demo-jaxrs/jaxrs-client/src/main/java/org/apache/servicecomb/demo/jaxrs/client/JaxrsClient.java
b/demo/demo-jaxrs/jaxrs-client/src/main/java/org/apache/servicecomb/demo/jaxrs/client/JaxrsClient.java
index f28ffbd8b..7834980c5 100644
---
a/demo/demo-jaxrs/jaxrs-client/src/main/java/org/apache/servicecomb/demo/jaxrs/client/JaxrsClient.java
+++
b/demo/demo-jaxrs/jaxrs-client/src/main/java/org/apache/servicecomb/demo/jaxrs/client/JaxrsClient.java
@@ -304,9 +304,7 @@ private static void testValidatorAddFail(RestTemplate
template, String cseUrlPre
// ignored
Map data = (Map) e.getErrorData();
TestMgr.check(
- "[ConstraintViolationImpl{interpolatedMessage=",
- data.get("message").toString().substring(0,
- "[ConstraintViolationImpl{interpolatedMessage=".length()));
+ true, data.get("message").toString().contains("propertyPath=add.b"));
}
TestMgr.check(true, isExcep);
@@ -332,9 +330,7 @@ private static void testValidatorSayHiFail(RestTemplate
template, String cseUrlP
// Message dependends on locale, so just check the short part.
Map data = (Map) e.getErrorData();
TestMgr.check(
- "[ConstraintViolationImpl{interpolatedMessage=",
- data.get("message").toString().substring(0,
- "[ConstraintViolationImpl{interpolatedMessage=".length()));
+ true,
data.get("message").toString().contains("propertyPath=sayHi.name"));
}
TestMgr.check(true, isExcep);
}
@@ -367,9 +363,7 @@ private static void testValidatorExchangeFail(RestTemplate
template, String cseU
// Message dependends on locale, so just check the short part.
Map data = (Map) e.getErrorData();
TestMgr.check(
- "[ConstraintViolationImpl{interpolatedMessage",
- data.get("message").toString().substring(0,
- "[ConstraintViolationImpl{interpolatedMessage".length()));
+ true,
data.get("message").toString().contains("propertyPath=sayHello.student.age"));
}
TestMgr.check(true, isExcep);
}
diff --git
a/demo/demo-jaxrs/jaxrs-client/src/main/java/org/apache/servicecomb/demo/jaxrs/client/validation/ValidationServiceClient.java
b/demo/demo-jaxrs/jaxrs-client/src/main/java/org/apache/servicecomb/demo/jaxrs/client/validation/ValidationServiceClient.java
index 727a05026..5600ddaaa 100644
---
a/demo/demo-jaxrs/jaxrs-client/src/main/java/org/apache/servicecomb/demo/jaxrs/client/validation/ValidationServiceClient.java
+++
b/demo/demo-jaxrs/jaxrs-client/src/main/java/org/apache/servicecomb/demo/jaxrs/client/validation/ValidationServiceClient.java
@@ -19,6 +19,8 @@
import java.util.ArrayList;
+import javax.ws.rs.core.Response.Status;
+
import org.apache.servicecomb.core.CseContext;
import org.apache.servicecomb.demo.TestMgr;
import org.apache.servicecomb.demo.jaxrs.server.validation.ValidationModel;
@@ -52,7 +54,9 @@ private static void testValidation() {
template.postForObject(urlPrefix + "/validate", model,
ValidationModel.class);
TestMgr.check(false, true);
} catch (InvocationException e) {
- TestMgr.check(e.getErrorData().toString().contains("age"), true);
+ TestMgr.check(400, e.getStatus().getStatusCode());
+ TestMgr.check(Status.BAD_REQUEST, e.getReasonPhrase());
+
TestMgr.check(e.getErrorData().toString().contains("propertyPath=errorCode.request.age"),
true);
}
try {
@@ -61,7 +65,9 @@ private static void testValidation() {
template.postForObject(urlPrefix + "/validate", model,
ValidationModel.class);
TestMgr.check(false, true);
} catch (InvocationException e) {
- TestMgr.check(e.getErrorData().toString().contains("member"), true);
+ TestMgr.check(400, e.getStatus().getStatusCode());
+ TestMgr.check(Status.BAD_REQUEST, e.getReasonPhrase());
+
TestMgr.check(e.getErrorData().toString().contains("propertyPath=errorCode.request.members"),
true);
}
String strResult = template.getForObject(urlPrefix +
"/validateQuery?name=", String.class);
@@ -71,7 +77,9 @@ private static void testValidation() {
template.getForObject(urlPrefix + "/validateQuery", String.class);
TestMgr.check(false, true);
} catch (InvocationException e) {
- TestMgr.check(e.getErrorData().toString().contains("null"), true);
+ TestMgr.check(400, e.getStatus().getStatusCode());
+ TestMgr.check(Status.BAD_REQUEST, e.getReasonPhrase());
+
TestMgr.check(e.getErrorData().toString().contains("propertyPath=queryValidate.name"),
true);
}
}
}
diff --git
a/swagger/swagger-generator/generator-core/src/main/java/org/apache/servicecomb/swagger/generator/core/utils/ParamUtils.java
b/swagger/swagger-generator/generator-core/src/main/java/org/apache/servicecomb/swagger/generator/core/utils/ParamUtils.java
index 924bc651a..3046103d3 100644
---
a/swagger/swagger-generator/generator-core/src/main/java/org/apache/servicecomb/swagger/generator/core/utils/ParamUtils.java
+++
b/swagger/swagger-generator/generator-core/src/main/java/org/apache/servicecomb/swagger/generator/core/utils/ParamUtils.java
@@ -17,6 +17,7 @@
package org.apache.servicecomb.swagger.generator.core.utils;
+import java.lang.reflect.Executable;
import java.lang.reflect.Method;
import java.lang.reflect.Type;
import java.util.List;
@@ -56,8 +57,17 @@ public static String getParameterName(String existName,
Method method, int param
return existName;
}
+ public static String getParameterName(Executable methodOrConstructor, int
parameterIndex) {
+ MethodParameter methodParameter =
MethodParameter.forMethodOrConstructor(methodOrConstructor, parameterIndex);
+ return getParameterName(methodParameter, parameterIndex);
+ }
+
public static String getParameterName(Method method, int paramIdx) {
MethodParameter methodParameter = new MethodParameter(method, paramIdx);
+ return getParameterName(methodParameter, paramIdx);
+ }
+
+ public static String getParameterName(MethodParameter methodParameter, int
paramIdx) {
methodParameter.initParameterNameDiscovery(parameterNameDiscoverer);
String paramName = methodParameter.getParameterName();
@@ -140,8 +150,7 @@ public static void setParameterType(Type paramType,
AbstractSerializableParamete
String.format(
"not allow such type of param:[%s], param name is [%s]",
property.getClass(),
- parameter.getName())
- );
+ parameter.getName()));
}
parameter.setProperty(property);
}
diff --git
a/swagger/swagger-invocation/invocation-validator/src/main/java/org/apache/servicecomb/swagger/invocation/validator/DefaultParameterNameProvider.java
b/swagger/swagger-invocation/invocation-validator/src/main/java/org/apache/servicecomb/swagger/invocation/validator/DefaultParameterNameProvider.java
new file mode 100644
index 000000000..0188b6742
--- /dev/null
+++
b/swagger/swagger-invocation/invocation-validator/src/main/java/org/apache/servicecomb/swagger/invocation/validator/DefaultParameterNameProvider.java
@@ -0,0 +1,55 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.servicecomb.swagger.invocation.validator;
+
+import java.lang.reflect.AccessibleObject;
+import java.lang.reflect.Constructor;
+import java.lang.reflect.Executable;
+import java.lang.reflect.Method;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+import java.util.Map;
+import javax.validation.ParameterNameProvider;
+
+import org.apache.servicecomb.foundation.common.concurrent.ConcurrentHashMapEx;
+import org.apache.servicecomb.swagger.generator.core.utils.ParamUtils;
+
+
+public class DefaultParameterNameProvider implements ParameterNameProvider {
+ private final Map<AccessibleObject, List<String>> methodCache = new
ConcurrentHashMapEx<>();
+
+ @Override
+ public List<String> getParameterNames(Constructor<?> constructor) {
+ return methodCache.computeIfAbsent(constructor, k ->
getParameterNamesEx(constructor));
+ }
+
+ @Override
+ public List<String> getParameterNames(Method method) {
+ return methodCache.computeIfAbsent(method, k ->
getParameterNamesEx(method));
+ }
+
+ private List<String> getParameterNamesEx(Executable methodOrConstructor) {
+ int parameterCount = methodOrConstructor.getParameterCount();
+ List<String> parameterNames = new ArrayList<>(parameterCount);
+
+ for (int i = 0; i < parameterCount; i++) {
+ parameterNames.add(ParamUtils.getParameterName(methodOrConstructor, i));
+ }
+ return Collections.unmodifiableList(parameterNames);
+ }
+}
diff --git
a/swagger/swagger-invocation/invocation-validator/src/main/java/org/apache/servicecomb/swagger/invocation/validator/ParameterValidator.java
b/swagger/swagger-invocation/invocation-validator/src/main/java/org/apache/servicecomb/swagger/invocation/validator/ParameterValidator.java
index fcb389725..8a645e0d0 100644
---
a/swagger/swagger-invocation/invocation-validator/src/main/java/org/apache/servicecomb/swagger/invocation/validator/ParameterValidator.java
+++
b/swagger/swagger-invocation/invocation-validator/src/main/java/org/apache/servicecomb/swagger/invocation/validator/ParameterValidator.java
@@ -43,7 +43,11 @@
throws ConstraintViolationException {
if (null == executableValidator) {
- ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
+ ValidatorFactory factory =
+ Validation.byDefaultProvider()
+ .configure()
+ .parameterNameProvider(new DefaultParameterNameProvider())
+ .buildValidatorFactory();
executableValidator = factory.getValidator().forExecutables();
}
Set<ConstraintViolation<Object>> violations =
diff --git
a/swagger/swagger-invocation/invocation-validator/src/test/java/org/apache/servicecomb/swagger/invocation/validator/TestDefaultParameterNameProvider.java
b/swagger/swagger-invocation/invocation-validator/src/test/java/org/apache/servicecomb/swagger/invocation/validator/TestDefaultParameterNameProvider.java
new file mode 100644
index 000000000..fcb228fe2
--- /dev/null
+++
b/swagger/swagger-invocation/invocation-validator/src/test/java/org/apache/servicecomb/swagger/invocation/validator/TestDefaultParameterNameProvider.java
@@ -0,0 +1,115 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.servicecomb.swagger.invocation.validator;
+
+import java.lang.reflect.Constructor;
+import java.lang.reflect.Method;
+import org.hamcrest.Matchers;
+import org.junit.Assert;
+import org.junit.Test;
+
+public class TestDefaultParameterNameProvider {
+ static class ValidatorForTest {
+ static class Student {
+ private String name;
+
+ int age;
+
+ public String getName() {
+ return name;
+ }
+
+ public void setName(String name) {
+ this.name = name;
+ }
+
+ public int getAge() {
+ return age;
+ }
+
+ public void setAge(int age) {
+ this.age = age;
+ }
+ }
+
+ private String grade;
+
+ private int number;
+
+ public ValidatorForTest() {
+ }
+
+ public ValidatorForTest(String grade, int number) {
+ this.grade = grade;
+ this.number = number;
+ }
+
+ public int add(int a, int b) {
+ return a + b;
+ }
+
+ public String sayHi(String hi) {
+ return hi + " sayhi";
+ }
+
+ public Student sayHello(Student student) {
+ return student;
+ }
+
+ public String setTest(String grade) {
+ this.grade = grade;
+ return this.grade;
+ }
+
+ public int getNumber() {
+ return number;
+ }
+
+ public void setNumber(int number) {
+ this.number = number;
+ }
+ }
+
+ Class<ValidatorForTest> validatorForTest = ValidatorForTest.class;
+
+ DefaultParameterNameProvider parameterNameProvider = new
DefaultParameterNameProvider();
+
+ @Test
+ public void testMethod() throws NoSuchMethodException {
+ Method method = validatorForTest.getMethod("add", int.class, int.class);
+ Assert.assertThat(parameterNameProvider.getParameterNames(method),
Matchers.contains("a", "b"));
+ method = validatorForTest.getMethod("sayHi", String.class);
+ Assert.assertThat(parameterNameProvider.getParameterNames(method),
Matchers.contains("hi"));
+ method = validatorForTest.getMethod("sayHello",
ValidatorForTest.Student.class);
+ Assert.assertThat(parameterNameProvider.getParameterNames(method),
Matchers.contains("student"));
+ method = validatorForTest.getMethod("setTest", String.class);
+ Assert.assertThat(parameterNameProvider.getParameterNames(method),
Matchers.contains("grade"));
+ method = validatorForTest.getMethod("getNumber");
+
Assert.assertTrue(parameterNameProvider.getParameterNames(method).isEmpty());
+ method = validatorForTest.getMethod("setNumber", int.class);
+ Assert.assertThat(parameterNameProvider.getParameterNames(method),
Matchers.contains("number"));
+ }
+
+ @Test
+ public void testConstructor() throws NoSuchMethodException {
+ Constructor<ValidatorForTest> constructor =
validatorForTest.getConstructor(String.class, int.class);
+ Assert.assertThat(parameterNameProvider.getParameterNames(constructor),
Matchers.contains("grade", "number"));
+ constructor = validatorForTest.getConstructor();
+
Assert.assertTrue(parameterNameProvider.getParameterNames(constructor).isEmpty());
+
+ }
+}
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
[email protected]
> Make the validation result display the parameter name instead of arg0, arg1
> ---------------------------------------------------------------------------
>
> Key: SCB-873
> URL: https://issues.apache.org/jira/browse/SCB-873
> Project: Apache ServiceComb
> Issue Type: Improvement
> Components: Java-Chassis
> Affects Versions: java-chassis-1.0.0
> Reporter: WeiChao
> Assignee: WeiChao
> Priority: Major
> Fix For: java-chassis-1.1.0
>
>
--
This message was sent by Atlassian JIRA
(v7.6.3#76005)