From 239f10c53b8001caa7ab9c718328a313d08fa73e Mon Sep 17 00:00:00 2001 From: Olivier Lando Date: Wed, 24 Jul 2024 09:47:23 +0200 Subject: [PATCH] fix dates formatting --- .../java/video/api/client/generator/Go.java | 4 ++++ templates/csharp/ApiClient.mustache | 4 ++-- .../libraries/okhttp-gson/ApiClient.mustache | 18 +++++++++++++++--- .../swift5/OpenISO8601DateFormatter.mustache | 4 ++++ 4 files changed, 25 insertions(+), 5 deletions(-) diff --git a/apivideo-generator/src/main/java/video/api/client/generator/Go.java b/apivideo-generator/src/main/java/video/api/client/generator/Go.java index d077b99a..304fae04 100644 --- a/apivideo-generator/src/main/java/video/api/client/generator/Go.java +++ b/apivideo-generator/src/main/java/video/api/client/generator/Go.java @@ -90,6 +90,10 @@ public Map postProcessOperationsWithModels(Map o } operation.queryParams.forEach(queryParam -> { + if(queryParam.dataFormat != null && queryParam.dataFormat.equalsIgnoreCase("date-time")) { + operation.allParams.stream().filter(p -> p.baseName.equals(queryParam.baseName)).forEach(p -> p.dataType = "time.Time"); + queryParam.dataType = "time.Time"; + } if(queryParam.vendorExtensions.containsKey("x-is-deep-object")) additionalImports.add("fmt"); }); diff --git a/templates/csharp/ApiClient.mustache b/templates/csharp/ApiClient.mustache index 4fa83096..03987425 100644 --- a/templates/csharp/ApiClient.mustache +++ b/templates/csharp/ApiClient.mustache @@ -401,9 +401,9 @@ namespace {{packageName}}.Client public string ParameterToString(object obj) { if (obj is DateTime dateTime) - return dateTime.ToString(); + return dateTime.ToString("yyyy-MM-ddTHH:mm:ssKT"); else if (obj is DateTimeOffset dateTimeOffset) - return dateTimeOffset.ToString(); + return dateTimeOffset.ToString("yyyy-MM-ddTHH:mm:ssKT"); else if (obj is bool isTrue) return isTrue ? "true" : "false"; else if (obj is IList list) diff --git a/templates/java/libraries/okhttp-gson/ApiClient.mustache b/templates/java/libraries/okhttp-gson/ApiClient.mustache index bace44cf..58fddec9 100644 --- a/templates/java/libraries/okhttp-gson/ApiClient.mustache +++ b/templates/java/libraries/okhttp-gson/ApiClient.mustache @@ -54,6 +54,7 @@ import java.text.DateFormat; import java.time.LocalDate; import java.time.OffsetDateTime; import java.time.format.DateTimeFormatter; +import java.time.ZoneId; {{/java8}} {{/threetenbp}} import java.util.*; @@ -461,9 +462,20 @@ public class ApiClient { if (param == null) { return ""; } else if (param instanceof Date {{#joda}}|| param instanceof DateTime || param instanceof LocalDate{{/joda}}{{#jsr310}}|| param instanceof OffsetDateTime || param instanceof LocalDate{{/jsr310}}) { - //Serialize to json string and remove the " enclosing characters - String jsonStr = json.serialize(param); - return jsonStr.substring(1, jsonStr.length() - 1); + DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ssXXX"); + + String formattedDate; + if (param instanceof Date) { + OffsetDateTime odt = ((Date) param).toInstant().atZone(ZoneId.systemDefault()).toOffsetDateTime(); + formattedDate = odt.format(formatter); + } else if (param instanceof OffsetDateTime) { + formattedDate = ((OffsetDateTime) param).format(formatter); + } else { + OffsetDateTime odt = ((LocalDate) param).atStartOfDay(ZoneId.systemDefault()).toOffsetDateTime(); + formattedDate = odt.format(formatter); + } + + return formattedDate; } else if (param instanceof Collection) { StringBuilder b = new StringBuilder(); for (Object o : (Collection) param) { diff --git a/templates/swift5/OpenISO8601DateFormatter.mustache b/templates/swift5/OpenISO8601DateFormatter.mustache index 29c28dac..eaa09aca 100644 --- a/templates/swift5/OpenISO8601DateFormatter.mustache +++ b/templates/swift5/OpenISO8601DateFormatter.mustache @@ -35,6 +35,10 @@ import Foundation setup() } + override {{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}public{{/nonPublicApi}} func string(from date: Date) -> String { + return OpenISO8601DateFormatter.withoutSeconds.string(from: date) + } + override {{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}public{{/nonPublicApi}} func date(from string: String) -> Date? { if let result = super.date(from: string) { return result