forked from loafle/openapi-generator-original
add DateWithoutTime
wip
This commit is contained in:
+8
-4
@@ -134,6 +134,7 @@ public class Swift5ClientCodegen extends DefaultCodegen implements CodegenConfig
|
||||
"String",
|
||||
"Data",
|
||||
"Date",
|
||||
"DateWithoutTime",
|
||||
"Character",
|
||||
"UUID",
|
||||
"URL",
|
||||
@@ -221,7 +222,7 @@ public class Swift5ClientCodegen extends DefaultCodegen implements CodegenConfig
|
||||
typeMapping.put("array", "Array");
|
||||
typeMapping.put("map", "Dictionary");
|
||||
typeMapping.put("set", "Set");
|
||||
typeMapping.put("date", "Date");
|
||||
typeMapping.put("date", "DateWithoutTime");
|
||||
typeMapping.put("Date", "Date");
|
||||
typeMapping.put("DateTime", "Date");
|
||||
typeMapping.put("boolean", "Bool");
|
||||
@@ -538,9 +539,6 @@ public class Swift5ClientCodegen extends DefaultCodegen implements CodegenConfig
|
||||
supportingFiles.add(new SupportingFile("CodableHelper.mustache",
|
||||
sourceFolder,
|
||||
"CodableHelper.swift"));
|
||||
supportingFiles.add(new SupportingFile("OpenISO8601DateFormatter.mustache",
|
||||
sourceFolder,
|
||||
"OpenISO8601DateFormatter.swift"));
|
||||
supportingFiles.add(new SupportingFile("JSONDataEncoding.mustache",
|
||||
sourceFolder,
|
||||
"JSONDataEncoding.swift"));
|
||||
@@ -572,6 +570,12 @@ public class Swift5ClientCodegen extends DefaultCodegen implements CodegenConfig
|
||||
supportingFiles.add(new SupportingFile("Extensions.mustache",
|
||||
sourceFolder,
|
||||
"Extensions.swift"));
|
||||
supportingFiles.add(new SupportingFile("OpenISO8601DateFormatter.mustache",
|
||||
sourceFolder,
|
||||
"OpenISO8601DateFormatter.swift"));
|
||||
supportingFiles.add(new SupportingFile("DateWithoutTime.mustache",
|
||||
sourceFolder,
|
||||
"DateWithoutTime.swift"));
|
||||
supportingFiles.add(new SupportingFile("APIs.mustache",
|
||||
sourceFolder,
|
||||
"APIs.swift"));
|
||||
|
||||
@@ -0,0 +1,55 @@
|
||||
// DateWithoutTime.swift
|
||||
//
|
||||
// Generated by openapi-generator
|
||||
// https://openapi-generator.tech
|
||||
//
|
||||
|
||||
import Foundation
|
||||
|
||||
/// Represents a date without time information (e.g. a birthday) for transmission from and to a REST API
|
||||
///
|
||||
/// This type is used as a representation for openapi specs `date` format which does not contain
|
||||
/// time information as opposed to the `date-time` format. Although it internally uses `Date` for
|
||||
/// (de-)serialization as well the generator needs to be able to distinguish between the two formats.
|
||||
/// - note: As `Date` is agnostic to timezones (and calendars), timezone information is needed to be able to add
|
||||
/// an appropriate padding in order to transform to GMT+0 which is the assumed timezone in ISO 8601.
|
||||
/// When decoding, GMT+0 can be assumed (again: ISO8601) so there is no padding necessary and wrappedDate
|
||||
/// can be used safely.
|
||||
public struct DateWithoutTime: Codable, Hashable, Equatable {
|
||||
{{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}public{{/nonPublicApi}} let wrappedDate: Date
|
||||
{{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}public{{/nonPublicApi}} let timezone: TimeZone
|
||||
|
||||
{{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}public{{/nonPublicApi}} enum CodingKeys: CodingKey, CaseIterable {
|
||||
case wrappedDate
|
||||
case timezone
|
||||
}
|
||||
|
||||
{{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}public{{/nonPublicApi}} init(from decoder: Decoder) throws {
|
||||
let container = try decoder.singleValueContainer()
|
||||
self.wrappedDate = try container.decode(Date.self)
|
||||
self.timezone = OpenISO8601DateFormatter.withoutTime.timeZone
|
||||
}
|
||||
|
||||
{{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}public{{/nonPublicApi}} init?(wrappedDate: Date?, timezone: TimeZone = .current) {
|
||||
guard let wrappedDate = wrappedDate else {
|
||||
return nil
|
||||
}
|
||||
|
||||
self.init(wrappedDate: wrappedDate, timezone: timezone)
|
||||
}
|
||||
|
||||
{{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}public{{/nonPublicApi}} init(wrappedDate: Date, timezone: TimeZone) {
|
||||
self.wrappedDate = wrappedDate
|
||||
self.timezone = timezone
|
||||
}
|
||||
|
||||
{{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}public{{/nonPublicApi}} func encode(to encoder: Encoder) throws {
|
||||
var container = encoder.singleValueContainer()
|
||||
try container.encode(OpenISO8601DateFormatter.withoutTime.string(from: normalizedWrappedDate()))
|
||||
}
|
||||
|
||||
internal func normalizedWrappedDate() -> Date {
|
||||
return wrappedDate.addingTimeInterval(
|
||||
Double(timezone.secondsFromGMT(for: wrappedDate)))
|
||||
}
|
||||
}
|
||||
@@ -93,6 +93,12 @@ extension Date: JSONEncodable {
|
||||
}
|
||||
}
|
||||
|
||||
extension DateWithoutTime: JSONEncodable {
|
||||
func encodeToJSON() -> Any {
|
||||
return OpenISO8601DateFormatter.withoutTime.string(from: self.normalizedWrappedDate())
|
||||
}
|
||||
}
|
||||
|
||||
extension JSONEncodable where Self: Encodable {
|
||||
func encodeToJSON() -> Any {
|
||||
guard let data = try? CodableHelper.jsonEncoder.encode(self) else {
|
||||
|
||||
+2
-2
@@ -131,8 +131,8 @@ public class Swift5ClientCodegenTest {
|
||||
final Operation p = openAPI.getPaths().get(path).getPost();
|
||||
final CodegenOperation op = codegen.fromOperation(path, "post", p, null);
|
||||
|
||||
Assert.assertEquals(op.returnType, "Date");
|
||||
Assert.assertEquals(op.bodyParam.dataType, "Date");
|
||||
Assert.assertEquals(op.returnType, "DateWithoutTime");
|
||||
Assert.assertEquals(op.bodyParam.dataType, "DateWithoutTime");
|
||||
}
|
||||
|
||||
@Test(enabled = true)
|
||||
|
||||
+2
-2
@@ -114,10 +114,10 @@ public class Swift5ModelTest {
|
||||
|
||||
final CodegenProperty property7 = cm.vars.get(6);
|
||||
Assert.assertEquals(property7.baseName, "dateOfBirth");
|
||||
Assert.assertEquals(property7.dataType, "Date");
|
||||
Assert.assertEquals(property7.dataType, "DateWithoutTime");
|
||||
Assert.assertEquals(property7.name, "dateOfBirth");
|
||||
Assert.assertNull(property7.defaultValue);
|
||||
Assert.assertEquals(property7.baseType, "Date");
|
||||
Assert.assertEquals(property7.baseType, "DateWithoutTime");
|
||||
Assert.assertFalse(property7.required);
|
||||
Assert.assertFalse(property7.isContainer);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user