You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@servicecomb.apache.org by GitBox <gi...@apache.org> on 2018/08/17 09:52:56 UTC

[GitHub] liubao68 closed pull request #877: [SCB-847][revert]make response default and compatible to old behavior

liubao68 closed pull request #877: [SCB-847][revert]make response default and compatible to old behavior
URL: https://github.com/apache/incubator-servicecomb-java-chassis/pull/877
 
 
   

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/MultiErrorCodeServiceClient.java b/demo/demo-jaxrs/jaxrs-client/src/main/java/org/apache/servicecomb/demo/jaxrs/client/MultiErrorCodeServiceClient.java
index 4c249b8ee..24d90cbad 100644
--- a/demo/demo-jaxrs/jaxrs-client/src/main/java/org/apache/servicecomb/demo/jaxrs/client/MultiErrorCodeServiceClient.java
+++ b/demo/demo-jaxrs/jaxrs-client/src/main/java/org/apache/servicecomb/demo/jaxrs/client/MultiErrorCodeServiceClient.java
@@ -16,8 +16,12 @@
  */
 package org.apache.servicecomb.demo.jaxrs.client;
 
+import java.util.List;
+import java.util.Map;
+
 import javax.ws.rs.core.Response.Status;
 
+import org.apache.servicecomb.common.rest.codec.RestObjectMapperFactory;
 import org.apache.servicecomb.core.CseContext;
 import org.apache.servicecomb.demo.DemoConst;
 import org.apache.servicecomb.demo.TestMgr;
@@ -46,6 +50,7 @@ public static void runTest() {
       testErrorCodeWithHeader();
       testErrorCodeWithHeaderJAXRS();
       testErrorCodeWithHeaderJAXRSUsingRowType();
+      testNoClientErrorCode();
     }
   }
 
@@ -186,4 +191,30 @@ private static void testErrorCodeWithHeaderJAXRSUsingRowType() {
     TestMgr.check(result.getBody().getCode(), 200);
     TestMgr.check(result.getHeaders().getFirst("x-code"), 200);
   }
+
+  private static void testNoClientErrorCode() {
+    JsonObject requestJson = new JsonObject();
+    requestJson.put("code", 200);
+    requestJson.put("message", "test message");
+
+    ResponseEntity<List> listResult = template
+        .postForEntity(SERVER + "/MultiErrorCodeService/noClientErrorCode", requestJson, List.class);
+    TestMgr.check(listResult.getStatusCode(), 200);
+    Map mapResult = RestObjectMapperFactory.getRestObjectMapper().convertValue(listResult.getBody().get(0), Map.class);
+    TestMgr.check(mapResult.get("message"), "test message");
+    TestMgr.check(mapResult.get("code"), 200);
+    TestMgr.check(mapResult.get("t200"), 200);
+
+    try {
+      requestJson.put("code", 400);
+      template
+          .postForEntity(SERVER + "/MultiErrorCodeService/noClientErrorCode", requestJson, Object.class);
+    } catch (InvocationException e) {
+      TestMgr.check(e.getStatusCode(), 400);
+      mapResult = RestObjectMapperFactory.getRestObjectMapper().convertValue(e.getErrorData(), Map.class);
+      TestMgr.check(mapResult.get("message"), "test message");
+      TestMgr.check(mapResult.get("code"), 400);
+      TestMgr.check(mapResult.get("t400"), 400);
+    }
+  }
 }
diff --git a/demo/demo-jaxrs/jaxrs-server/src/main/java/org/apache/servicecomb/demo/jaxrs/server/multiErrorCode/MultiErrorCodeService.java b/demo/demo-jaxrs/jaxrs-server/src/main/java/org/apache/servicecomb/demo/jaxrs/server/multiErrorCode/MultiErrorCodeService.java
index 5004e958e..eb0c2f408 100644
--- a/demo/demo-jaxrs/jaxrs-server/src/main/java/org/apache/servicecomb/demo/jaxrs/server/multiErrorCode/MultiErrorCodeService.java
+++ b/demo/demo-jaxrs/jaxrs-server/src/main/java/org/apache/servicecomb/demo/jaxrs/server/multiErrorCode/MultiErrorCodeService.java
@@ -17,6 +17,9 @@
 
 package org.apache.servicecomb.demo.jaxrs.server.multiErrorCode;
 
+import java.util.ArrayList;
+import java.util.List;
+
 import javax.ws.rs.POST;
 import javax.ws.rs.Path;
 import javax.ws.rs.core.Response.Status;
@@ -139,4 +142,27 @@ public Response errorCodeWithHeader(MultiRequest request) {
     }
     return response;
   }
+
+  @Path("/noClientErrorCode")
+  @POST
+  @ApiResponses({
+      @ApiResponse(code = 400, response = NoClientErrorCode400.class, message = "")})
+  public List<NoClientErrorCode200> noClientErrorCode(MultiRequest request) {
+    javax.ws.rs.core.Response response;
+    if (request.getCode() == 400) {
+      NoClientErrorCode400 r = new NoClientErrorCode400();
+      r.setCode(request.getCode());
+      r.setMessage(request.getMessage());
+      r.setT400(400);
+      throw new InvocationException(Status.BAD_REQUEST, r);
+    } else {
+      NoClientErrorCode200 r = new NoClientErrorCode200();
+      r.setCode(request.getCode());
+      r.setMessage(request.getMessage());
+      r.setT200(200);
+      List<NoClientErrorCode200> result = new ArrayList<>();
+      result.add(r);
+      return result;
+    }
+  }
 }
diff --git a/demo/demo-jaxrs/jaxrs-server/src/main/java/org/apache/servicecomb/demo/jaxrs/server/multiErrorCode/NoClientErrorCode200.java b/demo/demo-jaxrs/jaxrs-server/src/main/java/org/apache/servicecomb/demo/jaxrs/server/multiErrorCode/NoClientErrorCode200.java
new file mode 100644
index 000000000..df11cf750
--- /dev/null
+++ b/demo/demo-jaxrs/jaxrs-server/src/main/java/org/apache/servicecomb/demo/jaxrs/server/multiErrorCode/NoClientErrorCode200.java
@@ -0,0 +1,50 @@
+/*
+ * 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.demo.jaxrs.server.multiErrorCode;
+
+public class NoClientErrorCode200 {
+  private String message;
+
+  private int code;
+
+  private long t200;
+
+  public String getMessage() {
+    return message;
+  }
+
+  public void setMessage(String message) {
+    this.message = message;
+  }
+
+  public int getCode() {
+    return code;
+  }
+
+  public void setCode(int code) {
+    this.code = code;
+  }
+
+  public long getT200() {
+    return t200;
+  }
+
+  public void setT200(long t200) {
+    this.t200 = t200;
+  }
+}
diff --git a/demo/demo-jaxrs/jaxrs-server/src/main/java/org/apache/servicecomb/demo/jaxrs/server/multiErrorCode/NoClientErrorCode400.java b/demo/demo-jaxrs/jaxrs-server/src/main/java/org/apache/servicecomb/demo/jaxrs/server/multiErrorCode/NoClientErrorCode400.java
new file mode 100644
index 000000000..cb482288e
--- /dev/null
+++ b/demo/demo-jaxrs/jaxrs-server/src/main/java/org/apache/servicecomb/demo/jaxrs/server/multiErrorCode/NoClientErrorCode400.java
@@ -0,0 +1,50 @@
+/*
+ * 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.demo.jaxrs.server.multiErrorCode;
+
+public class NoClientErrorCode400 {
+  private String message;
+
+  private int code;
+
+  private long t400;
+
+  public String getMessage() {
+    return message;
+  }
+
+  public void setMessage(String message) {
+    this.message = message;
+  }
+
+  public int getCode() {
+    return code;
+  }
+
+  public void setCode(int code) {
+    this.code = code;
+  }
+
+  public long getT400() {
+    return t400;
+  }
+
+  public void setT400(long t400) {
+    this.t400 = t400;
+  }
+}
diff --git a/swagger/swagger-invocation/invocation-core/src/main/java/org/apache/servicecomb/swagger/invocation/response/ResponsesMeta.java b/swagger/swagger-invocation/invocation-core/src/main/java/org/apache/servicecomb/swagger/invocation/response/ResponsesMeta.java
index 5c5337569..856e3f00d 100644
--- a/swagger/swagger-invocation/invocation-core/src/main/java/org/apache/servicecomb/swagger/invocation/response/ResponsesMeta.java
+++ b/swagger/swagger-invocation/invocation-core/src/main/java/org/apache/servicecomb/swagger/invocation/response/ResponsesMeta.java
@@ -64,9 +64,8 @@ public void init(SwaggerToClassGenerator swaggerToClassGenerator, Operation oper
       }
 
       Integer statusCode = Integer.parseInt(entry.getKey());
-      ResponseMeta codeMeta = new ResponseMeta();
-      codeMeta.init(swaggerToClassGenerator, entry.getValue());
-      ResponseMeta responseMeta = responseMap.put(statusCode, codeMeta);
+      ResponseMeta responseMeta = responseMap.computeIfAbsent(statusCode, k -> new ResponseMeta());
+      responseMeta.init(swaggerToClassGenerator, entry.getValue());
     }
 
     if (defaultResponse == null) {
diff --git a/swagger/swagger-invocation/invocation-core/src/test/java/org/apache/servicecomb/swagger/invocation/response/TestResponsesMeta.java b/swagger/swagger-invocation/invocation-core/src/test/java/org/apache/servicecomb/swagger/invocation/response/TestResponsesMeta.java
index 695504094..f6077ced8 100644
--- a/swagger/swagger-invocation/invocation-core/src/test/java/org/apache/servicecomb/swagger/invocation/response/TestResponsesMeta.java
+++ b/swagger/swagger-invocation/invocation-core/src/test/java/org/apache/servicecomb/swagger/invocation/response/TestResponsesMeta.java
@@ -54,13 +54,13 @@ public void test() {
     meta.init(swaggerToClassGenerator, operation, int.class);
 
     ResponseMeta resp = meta.findResponseMeta(200);
-    // Response is based on swagger type and is Integer type.
-    Assert.assertEquals(Integer.class, resp.getJavaType().getRawClass());
+    // Response currently is based on return type not swagger type
+    Assert.assertEquals(int.class, resp.getJavaType().getRawClass());
 
     resp = meta.findResponseMeta(201);
-    // Response is based on swagger type and is Integer type. For this test case there is one problem need to discuss.
+    // Response currently is based on return type not swagger type. For this test case there is one problem need to discuss.
     // If SUCCESS family, do we should use OK response type?
-    Assert.assertEquals(Integer.class, resp.getJavaType().getRawClass());
+    Assert.assertEquals(int.class, resp.getJavaType().getRawClass());
 
     resp = meta.findResponseMeta(400);
     Assert.assertEquals(String.class, resp.getJavaType().getRawClass());


 

----------------------------------------------------------------
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:
users@infra.apache.org


With regards,
Apache Git Services