From ba2619ce56a7057ca5aafca7440810169f2897a9 Mon Sep 17 00:00:00 2001 From: Gokberk Kocak Date: Thu, 23 Mar 2023 16:36:44 +0000 Subject: [PATCH 1/2] feat: add localdate type and add mapper module detect --- helpers/createReturnStatement.js | 3 ++- helpers/typeConvert.js | 1 + template/ApiClient.java.handlebars | 2 ++ template/ApiModel.java.handlebars | 2 ++ template/IApiClient.java.handlebars | 2 +- 5 files changed, 8 insertions(+), 2 deletions(-) diff --git a/helpers/createReturnStatement.js b/helpers/createReturnStatement.js index 59af776..fece445 100644 --- a/helpers/createReturnStatement.js +++ b/helpers/createReturnStatement.js @@ -27,7 +27,8 @@ const createReturnStatement = (responseSchema) => { returnStatement = "responseBodyString"; break; default: - mapperStatements = `ObjectMapper deserMapper = new ObjectMapper();\n`; + mapperStatements = `ObjectMapper deserMapper = new ObjectMapper();deserMapper.findAndRegisterModules();\n`; + mapperStatements += `${responseType} ${toParamName( responseType )} = deserMapper.readValue(responseBodyString, ${mapperListHandle( diff --git a/helpers/typeConvert.js b/helpers/typeConvert.js index fa9439c..ecc982c 100644 --- a/helpers/typeConvert.js +++ b/helpers/typeConvert.js @@ -11,6 +11,7 @@ const fromFormat = (propFormat, shouldBox) => { case "double": return shouldBox ? "Double" : "double"; case "date": + return "LocalDate"; case "date-time": return "ZonedDateTime"; case "byte": diff --git a/template/ApiClient.java.handlebars b/template/ApiClient.java.handlebars index f7b7317..f0b6054 100644 --- a/template/ApiClient.java.handlebars +++ b/template/ApiClient.java.handlebars @@ -6,6 +6,7 @@ import java.util.HashMap; import java.util.List; import java.util.stream.Collectors; import java.time.ZonedDateTime; +import java.time.LocalDate; import okhttp3.*; import com.fasterxml.jackson.databind.*; @@ -61,6 +62,7 @@ public class ApiClient{{_tag.name}} implements IApiClient{{_tag.name}} { var requestUri = _baseAddress.concat("{{setPathParameters @root.path _sortedParameters}}"){{#if (queryParametersExist _sortedParameters)}}.concat(queryString.toString()){{/if}}; {{#if (bodyParameterExists _sortedParameters)}} ObjectMapper serMapper = new ObjectMapper(); + serMapper.findAndRegisterModules(); String jsonBody = serMapper.writeValueAsString(body); RequestBody httpBody = RequestBody.create(jsonBody, MediaType.parse("application/json; charset=utf-8")); {{/if}} diff --git a/template/ApiModel.java.handlebars b/template/ApiModel.java.handlebars index 8a12862..f3f6e42 100644 --- a/template/ApiModel.java.handlebars +++ b/template/ApiModel.java.handlebars @@ -2,6 +2,8 @@ package {{_options.[generator.package]}}; import java.time.ZonedDateTime; import java.util.List; +import java.time.LocalDate; + {{#each components.schemas}} {{> model}} diff --git a/template/IApiClient.java.handlebars b/template/IApiClient.java.handlebars index cd647d5..03e4825 100644 --- a/template/IApiClient.java.handlebars +++ b/template/IApiClient.java.handlebars @@ -8,7 +8,7 @@ import com.fasterxml.jackson.databind.JsonMappingException; import java.io.IOException; import java.util.HashMap; import java.time.ZonedDateTime; - +import java.time.LocalDate; // // {{_info.title}} From 4a3808ab62b0ed7b6639064a3c4dddfffff775bc Mon Sep 17 00:00:00 2001 From: Gokberk Kocak Date: Wed, 29 Mar 2023 10:20:47 +0100 Subject: [PATCH 2/2] fix: fix date/date-time tests --- .../test/java/com/openapi/forge/MethodCallHandler.java | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/features/src/test/java/com/openapi/forge/MethodCallHandler.java b/features/src/test/java/com/openapi/forge/MethodCallHandler.java index 8178fff..7e081c4 100644 --- a/features/src/test/java/com/openapi/forge/MethodCallHandler.java +++ b/features/src/test/java/com/openapi/forge/MethodCallHandler.java @@ -12,6 +12,8 @@ import java.net.MalformedURLException; import java.net.URL; import java.net.URLClassLoader; +import java.time.ZonedDateTime; +import java.time.format.DateTimeFormatter; import java.util.Arrays; import java.util.List; import java.util.Map; @@ -132,7 +134,13 @@ public String getPropertyOnObject( Class.forName(packageName + "." + latestResponseType, false, classLoader); Method getProp = propClass.getDeclaredMethod("get" + StringUtils.capitalize(propName)); getProp.setAccessible(true); // Otherwise causes IllegalAccessException. - return getProp.invoke(resultOfMethodCall).toString(); + Object resultObject = getProp.invoke(resultOfMethodCall); + // Default ZonedDateTime formatting doesn't match with ISO format. + if (resultObject instanceof ZonedDateTime) { + return DateTimeFormatter.ISO_INSTANT.format((ZonedDateTime) resultObject); + } else { + return resultObject.toString(); + } } catch (ClassNotFoundException e) { throw new RuntimeException("This is probably an issue with the test code:\t\r\n" + e); } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {