From 10d3cb953a5a34c51517bb789639987d2f0fe443 Mon Sep 17 00:00:00 2001 From: Vladislav Plakhov Date: Wed, 3 Mar 2021 16:05:36 +0400 Subject: [PATCH] Support for Java 8 Date/Time API in parameters #125 add support java.sql.Time --- .../haulmont/addon/restapi/api/common/RestParseUtils.java | 7 +++++++ .../haulmont/rest/demo/core/app/PortalTestServiceBean.java | 6 ++++-- .../com/haulmont/rest/demo/core/app/PortalTestService.java | 3 ++- .../portal/src/com/haulmont/rest/demo/rest-services-v2.xml | 1 + .../haulmont/rest/demo/http/rest/ServicesControllerFT.java | 2 +- .../rest/demo/http/rest/data/serviceWithDateParams.json | 3 ++- .../web/src/com/haulmont/rest/demo/rest-services-v2.xml | 1 + 7 files changed, 18 insertions(+), 5 deletions(-) diff --git a/modules/rest-api/src/com/haulmont/addon/restapi/api/common/RestParseUtils.java b/modules/rest-api/src/com/haulmont/addon/restapi/api/common/RestParseUtils.java index 56f3866..9ac37d7 100644 --- a/modules/rest-api/src/com/haulmont/addon/restapi/api/common/RestParseUtils.java +++ b/modules/rest-api/src/com/haulmont/addon/restapi/api/common/RestParseUtils.java @@ -104,6 +104,13 @@ public Object toObject(Type type, String value, @Nullable String modelVersion) t if (OffsetTime.class == clazz) { return Datatypes.getNN(OffsetTime.class).parse(value); } + if (Time.class == clazz) { + LocalTime result = Datatypes.getNN(LocalTime.class).parse(value); + if (result == null) { + return null; + } + return Time.valueOf(result); + } if (BigDecimal.class == clazz) return Datatypes.getNN(BigDecimal.class).parse(value); if (Boolean.class == clazz || Boolean.TYPE == clazz) return Datatypes.getNN(Boolean.class).parse(value); if (Long.class == clazz || Long.TYPE == clazz) return Datatypes.getNN(Long.class).parse(value); diff --git a/test/restapi-demo/modules/core/src/com/haulmont/rest/demo/core/app/PortalTestServiceBean.java b/test/restapi-demo/modules/core/src/com/haulmont/rest/demo/core/app/PortalTestServiceBean.java index 995b244..9e173e5 100644 --- a/test/restapi-demo/modules/core/src/com/haulmont/rest/demo/core/app/PortalTestServiceBean.java +++ b/test/restapi-demo/modules/core/src/com/haulmont/rest/demo/core/app/PortalTestServiceBean.java @@ -25,6 +25,7 @@ import javax.inject.Inject; import javax.validation.constraints.Pattern; import java.math.BigDecimal; +import java.sql.Time; import java.text.ParseException; import java.text.SimpleDateFormat; import java.time.*; @@ -55,12 +56,13 @@ public Integer sum(int number1, String number2) { } @Override - public String convertDate(LocalDate localDate, LocalDateTime localDateTime, LocalTime localTime, OffsetDateTime offsetDateTime, OffsetTime offsetTime) { + public String convertDate(LocalDate localDate, LocalDateTime localDateTime, LocalTime localTime, OffsetDateTime offsetDateTime, OffsetTime offsetTime, Time time) { StringBuilder builder = new StringBuilder(localDate.toString()).append(","); builder.append(localDateTime.toString()).append(","); builder.append(localTime.toString()).append(","); builder.append(offsetDateTime.toString()).append(","); - builder.append(offsetTime.toString()); + builder.append(offsetTime.toString()).append(","); + builder.append(time.toString()); return builder.toString(); } diff --git a/test/restapi-demo/modules/global/src/com/haulmont/rest/demo/core/app/PortalTestService.java b/test/restapi-demo/modules/global/src/com/haulmont/rest/demo/core/app/PortalTestService.java index 0c3efb4..1b6be82 100644 --- a/test/restapi-demo/modules/global/src/com/haulmont/rest/demo/core/app/PortalTestService.java +++ b/test/restapi-demo/modules/global/src/com/haulmont/rest/demo/core/app/PortalTestService.java @@ -14,6 +14,7 @@ import javax.validation.constraints.NotNull; import javax.validation.constraints.Pattern; import java.math.BigDecimal; +import java.sql.Time; import java.text.ParseException; import java.time.*; import java.util.Date; @@ -34,7 +35,7 @@ public interface PortalTestService { Integer sum(int number1, String number2); - String convertDate(LocalDate localDate, LocalDateTime localDateTime, LocalTime localTime, OffsetDateTime offsetDateTime, OffsetTime offsetTime); + String convertDate(LocalDate localDate, LocalDateTime localDateTime, LocalTime localTime, OffsetDateTime offsetDateTime, OffsetTime offsetTime, Time time); void methodWithCustomException(); diff --git a/test/restapi-demo/modules/portal/src/com/haulmont/rest/demo/rest-services-v2.xml b/test/restapi-demo/modules/portal/src/com/haulmont/rest/demo/rest-services-v2.xml index 5a9af17..039755f 100644 --- a/test/restapi-demo/modules/portal/src/com/haulmont/rest/demo/rest-services-v2.xml +++ b/test/restapi-demo/modules/portal/src/com/haulmont/rest/demo/rest-services-v2.xml @@ -21,6 +21,7 @@ + diff --git a/test/restapi-demo/modules/portal/test/com/haulmont/rest/demo/http/rest/ServicesControllerFT.java b/test/restapi-demo/modules/portal/test/com/haulmont/rest/demo/http/rest/ServicesControllerFT.java index 0b408f6..5466730 100644 --- a/test/restapi-demo/modules/portal/test/com/haulmont/rest/demo/http/rest/ServicesControllerFT.java +++ b/test/restapi-demo/modules/portal/test/com/haulmont/rest/demo/http/rest/ServicesControllerFT.java @@ -79,7 +79,7 @@ public void serviceWithDateParams() throws Exception { String requestBody = getFileContent("serviceWithDateParams.json", null); try (CloseableHttpResponse response = sendPost("/services/" + PortalTestService.NAME + "/convertDate", oauthToken, requestBody, null)) { assertEquals(HttpStatus.SC_OK, statusCode(response)); - assertEquals("2021-02-24,2021-02-24T15:15:15.053,15:15:15,2021-02-24T15:15:15.053+04:00,15:15:15+04:00", responseToString(response)); + assertEquals("2021-02-24,2021-02-24T15:15:15.053,15:15:15,2021-02-24T15:15:15.053+04:00,15:15:15+04:00,15:15:15", responseToString(response)); } } diff --git a/test/restapi-demo/modules/portal/test/com/haulmont/rest/demo/http/rest/data/serviceWithDateParams.json b/test/restapi-demo/modules/portal/test/com/haulmont/rest/demo/http/rest/data/serviceWithDateParams.json index 53642b7..7b36e53 100644 --- a/test/restapi-demo/modules/portal/test/com/haulmont/rest/demo/http/rest/data/serviceWithDateParams.json +++ b/test/restapi-demo/modules/portal/test/com/haulmont/rest/demo/http/rest/data/serviceWithDateParams.json @@ -3,5 +3,6 @@ "localDateTime": "2021-02-24 15:15:15.053", "localTime": "15:15:15", "offsetDateTime": "2021-02-24 15:15:15.053 +0400", - "offsetTime": "15:15:15 +0400" + "offsetTime": "15:15:15 +0400", + "time": "15:15:15" } \ No newline at end of file diff --git a/test/restapi-demo/modules/web/src/com/haulmont/rest/demo/rest-services-v2.xml b/test/restapi-demo/modules/web/src/com/haulmont/rest/demo/rest-services-v2.xml index 5a9af17..039755f 100644 --- a/test/restapi-demo/modules/web/src/com/haulmont/rest/demo/rest-services-v2.xml +++ b/test/restapi-demo/modules/web/src/com/haulmont/rest/demo/rest-services-v2.xml @@ -21,6 +21,7 @@ +