Skip to content

Commit

Permalink
fix dates formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
olivier-lando committed Jul 24, 2024
1 parent 03c136e commit 239f10c
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,10 @@ public Map<String, Object> postProcessOperationsWithModels(Map<String, Object> 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");
});

Expand Down
4 changes: 2 additions & 2 deletions templates/csharp/ApiClient.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
18 changes: 15 additions & 3 deletions templates/java/libraries/okhttp-gson/ApiClient.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -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.*;
Expand Down Expand Up @@ -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) {
Expand Down
4 changes: 4 additions & 0 deletions templates/swift5/OpenISO8601DateFormatter.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 239f10c

Please sign in to comment.