You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@servicecomb.apache.org by li...@apache.org on 2020/02/25 01:35:03 UTC

[servicecomb-java-chassis] branch master updated: [SCB-1737] support ISO 8601 data and time (part1: REST)

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

liubao pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/servicecomb-java-chassis.git


The following commit(s) were added to refs/heads/master by this push:
     new 2c2f44f  [SCB-1737] support ISO 8601 data and time (part1: REST)
2c2f44f is described below

commit 2c2f44fe27b5f70deea9f8cc4d12306520cf22d0
Author: liubao <bi...@qq.com>
AuthorDate: Mon Feb 24 17:32:26 2020 +0800

    [SCB-1737] support ISO 8601 data and time (part1: REST)
---
 common/common-rest/pom.xml                         |  4 +
 .../common/rest/codec/RestObjectMapper.java        |  2 +
 .../definition/path/AbstractUrlParamWriter.java    |  7 ++
 .../rest/definition/path/PathVarParamWriter.java   | 12 ++-
 .../demo/springmvc/client/TestDateTimeSchema.java  | 95 ++++++++++++++++++++++
 .../demo/springmvc/server/DateTimeSchema.java      | 81 ++++++++++++++++++
 .../springmvc/server/ProducerTestsAfterBootup.java |  2 +-
 .../src/main/resources/microservice.yaml           |  1 +
 8 files changed, 201 insertions(+), 3 deletions(-)

diff --git a/common/common-rest/pom.xml b/common/common-rest/pom.xml
index 73dc341..9b79b8f 100644
--- a/common/common-rest/pom.xml
+++ b/common/common-rest/pom.xml
@@ -60,5 +60,9 @@
       <artifactId>swagger-generator-jaxrs</artifactId>
       <scope>test</scope>
     </dependency>
+    <dependency>
+      <groupId>com.fasterxml.jackson.datatype</groupId>
+      <artifactId>jackson-datatype-jsr310</artifactId>
+    </dependency>
   </dependencies>
 </project>
diff --git a/common/common-rest/src/main/java/org/apache/servicecomb/common/rest/codec/RestObjectMapper.java b/common/common-rest/src/main/java/org/apache/servicecomb/common/rest/codec/RestObjectMapper.java
index 8c315c5..924871e 100644
--- a/common/common-rest/src/main/java/org/apache/servicecomb/common/rest/codec/RestObjectMapper.java
+++ b/common/common-rest/src/main/java/org/apache/servicecomb/common/rest/codec/RestObjectMapper.java
@@ -30,6 +30,7 @@ import com.fasterxml.jackson.databind.SerializationFeature;
 import com.fasterxml.jackson.databind.SerializerProvider;
 import com.fasterxml.jackson.databind.module.SimpleModule;
 import com.fasterxml.jackson.databind.type.TypeFactory;
+import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
 
 import io.vertx.core.json.JsonObject;
 
@@ -74,6 +75,7 @@ public class RestObjectMapper extends AbstractRestObjectMapper {
     // custom types
     module.addSerializer(JsonObject.class, new JsonObjectSerializer());
     registerModule(module);
+    registerModule(new JavaTimeModule());
   }
 
   @Override
diff --git a/common/common-rest/src/main/java/org/apache/servicecomb/common/rest/definition/path/AbstractUrlParamWriter.java b/common/common-rest/src/main/java/org/apache/servicecomb/common/rest/definition/path/AbstractUrlParamWriter.java
index f25a42c..2ab28c1 100644
--- a/common/common-rest/src/main/java/org/apache/servicecomb/common/rest/definition/path/AbstractUrlParamWriter.java
+++ b/common/common-rest/src/main/java/org/apache/servicecomb/common/rest/definition/path/AbstractUrlParamWriter.java
@@ -25,6 +25,13 @@ public abstract class AbstractUrlParamWriter implements UrlParamWriter {
   protected RestParam param;
 
   protected Object getParamValue(Map<String, Object> args) {
+    if (param == null) {
+      // Wrong server definition
+      //  @GetMapping(path = "/getLocalDateTime/{paramX}")
+      //  public LocalDateTime getLocalDateTimePath(@PathParam("paramY") LocalDateTime date) {
+      throw new IllegalArgumentException("Path parameter name not valid in provider. Check if provider "
+          + "path pattern has the parameter name.");
+    }
     return args.get(param.getParamName());
   }
 }
diff --git a/common/common-rest/src/main/java/org/apache/servicecomb/common/rest/definition/path/PathVarParamWriter.java b/common/common-rest/src/main/java/org/apache/servicecomb/common/rest/definition/path/PathVarParamWriter.java
index fb753d9..4cd9641 100644
--- a/common/common-rest/src/main/java/org/apache/servicecomb/common/rest/definition/path/PathVarParamWriter.java
+++ b/common/common-rest/src/main/java/org/apache/servicecomb/common/rest/definition/path/PathVarParamWriter.java
@@ -19,6 +19,7 @@ package org.apache.servicecomb.common.rest.definition.path;
 
 import java.util.Map;
 
+import org.apache.servicecomb.common.rest.codec.RestObjectMapperFactory;
 import org.apache.servicecomb.common.rest.definition.RestParam;
 import org.apache.servicecomb.common.rest.definition.path.URLPathBuilder.URLPathStringBuilder;
 import org.apache.servicecomb.foundation.common.http.HttpUtils;
@@ -33,8 +34,15 @@ public class PathVarParamWriter extends AbstractUrlParamWriter {
 
   @Override
   public void write(URLPathStringBuilder builder, Map<String, Object> args) throws Exception {
-    String paramValue = getParamValue(args).toString();
-    String encodedPathParam = HttpUtils.encodePathParam(paramValue);
+    if (getParamValue(args) == null) {
+      throw new IllegalArgumentException("path parameter can not be null.");
+    }
+    String encodedPathParam = encodeNotNullValue(getParamValue(args));
     builder.appendPath(encodedPathParam);
   }
+
+  private String encodeNotNullValue(Object value) throws Exception {
+    String strValue = RestObjectMapperFactory.getRestObjectMapper().convertToString(value);
+    return HttpUtils.encodePathParam(strValue);
+  }
 }
diff --git a/demo/demo-springmvc/springmvc-client/src/main/java/org/apache/servicecomb/demo/springmvc/client/TestDateTimeSchema.java b/demo/demo-springmvc/springmvc-client/src/main/java/org/apache/servicecomb/demo/springmvc/client/TestDateTimeSchema.java
new file mode 100644
index 0000000..a55edf4
--- /dev/null
+++ b/demo/demo-springmvc/springmvc-client/src/main/java/org/apache/servicecomb/demo/springmvc/client/TestDateTimeSchema.java
@@ -0,0 +1,95 @@
+/*
+ * 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.springmvc.client;
+
+import java.time.LocalDate;
+import java.time.LocalDateTime;
+import java.time.format.DateTimeFormatter;
+import java.util.Date;
+
+import org.apache.servicecomb.demo.CategorizedTestCase;
+import org.apache.servicecomb.demo.TestMgr;
+import org.apache.servicecomb.provider.pojo.RpcReference;
+import org.springframework.stereotype.Component;
+
+interface DateTimeSchemaInf {
+  Date getDate(Date date);
+
+  Date getDatePath(Date date);
+
+  Date postDate(Date date);
+
+  LocalDate getLocalDate(LocalDate date);
+
+  LocalDate getLocalDatePath(LocalDate date);
+
+  LocalDate postLocalDate(LocalDate date);
+
+  LocalDateTime getLocalDateTime(LocalDateTime date);
+
+  LocalDateTime getLocalDateTimePath(LocalDateTime date);
+
+  LocalDateTime postLocalDateTime(LocalDateTime date);
+}
+
+@Component
+public class TestDateTimeSchema implements CategorizedTestCase {
+  @RpcReference(microserviceName = "springmvc", schemaId = "DateTimeSchema")
+  private DateTimeSchemaInf dateTimeSchemaInf;
+
+  @Override
+  public void testRestTransport() throws Exception {
+    testDateTimeSchema();
+  }
+
+  @Override
+  public void testHighwayTransport() throws Exception {
+
+  }
+
+  @Override
+  public void testAllTransport() throws Exception {
+
+  }
+
+  private void testDateTimeSchema() {
+    Date date = new Date();
+    TestMgr.check(date.getTime(), dateTimeSchemaInf.getDate(date).getTime());
+    TestMgr.check(date.getTime(), dateTimeSchemaInf.getDatePath(date).getTime());
+    TestMgr.check(date.getTime(), dateTimeSchemaInf.postDate(date).getTime());
+
+    LocalDate localDate = LocalDate.of(2020, 2, 1);
+    TestMgr.check(localDate.format(DateTimeFormatter.ofPattern("yyyy-MM-dd")),
+        dateTimeSchemaInf.getLocalDate(localDate).format(DateTimeFormatter.ofPattern("yyyy-MM-dd")));
+    TestMgr.check(localDate.format(DateTimeFormatter.ofPattern("yyyy-MM-dd")),
+        dateTimeSchemaInf.getLocalDatePath(localDate).format(DateTimeFormatter.ofPattern("yyyy-MM-dd")));
+    TestMgr.check(localDate.format(DateTimeFormatter.ofPattern("yyyy-MM-dd")),
+        dateTimeSchemaInf.postLocalDate(localDate).format(DateTimeFormatter.ofPattern("yyyy-MM-dd")));
+
+    LocalDateTime localDateTime = LocalDateTime.of(2020, 2, 1, 23, 23, 30, 333);
+    TestMgr.check(localDateTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSS")),
+        dateTimeSchemaInf.getLocalDateTime(localDateTime)
+            .format(DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSS")));
+    TestMgr.check(localDateTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSS")),
+        dateTimeSchemaInf.getLocalDateTimePath(localDateTime)
+            .format(DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSS")));
+    TestMgr.check(localDateTime.format(DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSS")),
+        dateTimeSchemaInf.postLocalDateTime(localDateTime)
+            .format(DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSS")));
+  }
+}
diff --git a/demo/demo-springmvc/springmvc-server/src/main/java/org/apache/servicecomb/demo/springmvc/server/DateTimeSchema.java b/demo/demo-springmvc/springmvc-server/src/main/java/org/apache/servicecomb/demo/springmvc/server/DateTimeSchema.java
new file mode 100644
index 0000000..ab9f8ed
--- /dev/null
+++ b/demo/demo-springmvc/springmvc-server/src/main/java/org/apache/servicecomb/demo/springmvc/server/DateTimeSchema.java
@@ -0,0 +1,81 @@
+/*
+ * 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.springmvc.server;
+
+import java.time.LocalDate;
+import java.time.LocalDateTime;
+import java.util.Date;
+
+import javax.ws.rs.PathParam;
+import javax.ws.rs.core.MediaType;
+
+import org.apache.servicecomb.provider.rest.common.RestSchema;
+import org.springframework.web.bind.annotation.GetMapping;
+import org.springframework.web.bind.annotation.PostMapping;
+import org.springframework.web.bind.annotation.RequestBody;
+import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RequestParam;
+
+@RestSchema(schemaId = "DateTimeSchema")
+@RequestMapping(path = "/dateTime", produces = MediaType.APPLICATION_JSON)
+public class DateTimeSchema {
+  @GetMapping(path = "/getDate")
+  public Date getDate(@RequestParam("date") Date date) {
+    return date;
+  }
+
+  @GetMapping(path = "/getDatePath/{date}")
+  public Date getDatePath(@PathParam("date") Date date) {
+    return date;
+  }
+
+  @PostMapping(path = "/postDate")
+  public Date postDate(@RequestBody Date date) {
+    return date;
+  }
+
+  @GetMapping(path = "/getLocalDate")
+  public LocalDate getLocalDate(@RequestParam("date") LocalDate date) {
+    return date;
+  }
+
+  @GetMapping(path = "/getLocalDate/{date}")
+  public LocalDate getLocalDatePath(@PathParam("date") LocalDate date) {
+    return date;
+  }
+
+  @PostMapping(path = "/postLocalDate")
+  public LocalDate postLocalDate(@RequestBody LocalDate date) {
+    return date;
+  }
+
+  @GetMapping(path = "/getLocalDateTime")
+  public LocalDateTime getLocalDateTime(@RequestParam("date") LocalDateTime date) {
+    return date;
+  }
+
+  @GetMapping(path = "/getLocalDateTime/{date}")
+  public LocalDateTime getLocalDateTimePath(@PathParam("date") LocalDateTime date) {
+    return date;
+  }
+
+  @PostMapping(path = "/postLocalDateTime")
+  public LocalDateTime postLocalDateTime(@RequestBody LocalDateTime date) {
+    return date;
+  }
+}
diff --git a/demo/demo-springmvc/springmvc-server/src/main/java/org/apache/servicecomb/demo/springmvc/server/ProducerTestsAfterBootup.java b/demo/demo-springmvc/springmvc-server/src/main/java/org/apache/servicecomb/demo/springmvc/server/ProducerTestsAfterBootup.java
index 155b690..562d959 100644
--- a/demo/demo-springmvc/springmvc-server/src/main/java/org/apache/servicecomb/demo/springmvc/server/ProducerTestsAfterBootup.java
+++ b/demo/demo-springmvc/springmvc-server/src/main/java/org/apache/servicecomb/demo/springmvc/server/ProducerTestsAfterBootup.java
@@ -89,7 +89,7 @@ public class ProducerTestsAfterBootup implements BootListener {
   }
 
   public void testRegisteredBasePath() {
-    TestMgr.check(13, RegistryUtils.getMicroservice().getPaths().size());
+    TestMgr.check(14, RegistryUtils.getMicroservice().getPaths().size());
   }
 
   private String getSwaggerContent(Swagger swagger) {
diff --git a/demo/demo-springmvc/springmvc-server/src/main/resources/microservice.yaml b/demo/demo-springmvc/springmvc-server/src/main/resources/microservice.yaml
index dcb8ae8..e20eac3 100644
--- a/demo/demo-springmvc/springmvc-server/src/main/resources/microservice.yaml
+++ b/demo/demo-springmvc/springmvc-server/src/main/resources/microservice.yaml
@@ -70,6 +70,7 @@ servicecomb:
     name: myDC
     region: my-Region
     availableZone: my-Zone
+  codec.printErrorMessage: true
 #########SSL options
 ssl.protocols: TLSv1.2
 ssl.authPeer: true