Skip to content

Commit

Permalink
Merge 17475fa into 89d4c4b
Browse files Browse the repository at this point in the history
  • Loading branch information
liubao68 committed Feb 24, 2020
2 parents 89d4c4b + 17475fa commit d853b7b
Show file tree
Hide file tree
Showing 8 changed files with 201 additions and 3 deletions.
4 changes: 4 additions & 0 deletions common/common-rest/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
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;

Expand Down Expand Up @@ -74,6 +75,7 @@ public StringBuffer format(Date date, StringBuffer toAppendTo, FieldPosition fie
// custom types
module.addSerializer(JsonObject.class, new JsonObjectSerializer());
registerModule(module);
registerModule(new JavaTimeModule());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

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;
Expand All @@ -33,8 +34,15 @@ public PathVarParamWriter(RestParam param) {

@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);
}
}
Original file line number Diff line number Diff line change
@@ -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")));
}
}
Original file line number Diff line number Diff line change
@@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public void testSchemaNotChange(SCBEngine scbEngine) {
}

public void testRegisteredBasePath() {
TestMgr.check(13, RegistryUtils.getMicroservice().getPaths().size());
TestMgr.check(14, RegistryUtils.getMicroservice().getPaths().size());
}

private String getSwaggerContent(Swagger swagger) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit d853b7b

Please sign in to comment.