diff --git a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/Swift5ClientCodegen.java b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/Swift5ClientCodegen.java index de4fe8899e9e..d4c680030fe0 100644 --- a/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/Swift5ClientCodegen.java +++ b/modules/openapi-generator/src/main/java/org/openapitools/codegen/languages/Swift5ClientCodegen.java @@ -136,7 +136,7 @@ public class Swift5ClientCodegen extends DefaultCodegen implements CodegenConfig "String", "Data", "Date", - "DateWithoutTime", + "OpenAPIDateWithoutTime", "Character", "UUID", "URL", @@ -528,7 +528,7 @@ public class Swift5ClientCodegen extends DefaultCodegen implements CodegenConfig } additionalProperties.put(USE_CUSTOM_DATE_WITHOUT_TIME, useCustomDateWithoutTime); if (useCustomDateWithoutTime) { - typeMapping.put("date", "DateWithoutTime"); + typeMapping.put("date", "OpenAPIDateWithoutTime"); } else { typeMapping.put("date", "Date"); } @@ -588,9 +588,11 @@ public class Swift5ClientCodegen extends DefaultCodegen implements CodegenConfig supportingFiles.add(new SupportingFile("OpenISO8601DateFormatter.mustache", sourceFolder, "OpenISO8601DateFormatter.swift")); - supportingFiles.add(new SupportingFile("DateWithoutTime.mustache", + if (additionalProperties.containsKey(USE_CUSTOM_DATE_WITHOUT_TIME)) { + supportingFiles.add(new SupportingFile("OpenAPIDateWithoutTime.mustache", sourceFolder, - "DateWithoutTime.swift")); + "OpenAPIDateWithoutTime.swift")); + } supportingFiles.add(new SupportingFile("APIs.mustache", sourceFolder, "APIs.swift")); diff --git a/modules/openapi-generator/src/main/resources/swift5/Extensions.mustache b/modules/openapi-generator/src/main/resources/swift5/Extensions.mustache index c7343cdeddfa..b18821ae14b1 100644 --- a/modules/openapi-generator/src/main/resources/swift5/Extensions.mustache +++ b/modules/openapi-generator/src/main/resources/swift5/Extensions.mustache @@ -93,12 +93,6 @@ 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 { diff --git a/modules/openapi-generator/src/main/resources/swift5/DateWithoutTime.mustache b/modules/openapi-generator/src/main/resources/swift5/OpenAPIDateWithoutTime.mustache similarity index 88% rename from modules/openapi-generator/src/main/resources/swift5/DateWithoutTime.mustache rename to modules/openapi-generator/src/main/resources/swift5/OpenAPIDateWithoutTime.mustache index 6c26fdb0b11a..9ffb2c4f86d8 100644 --- a/modules/openapi-generator/src/main/resources/swift5/DateWithoutTime.mustache +++ b/modules/openapi-generator/src/main/resources/swift5/OpenAPIDateWithoutTime.mustache @@ -1,4 +1,4 @@ -// DateWithoutTime.swift +// OpenAPIDateWithoutTime.swift // // Generated by openapi-generator // https://openapi-generator.tech @@ -15,7 +15,7 @@ import Foundation /// 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. -{{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}public{{/nonPublicApi}} struct DateWithoutTime: Codable, Hashable, Equatable { +{{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}public{{/nonPublicApi}} struct OpenAPIDateWithoutTime: Codable, Hashable, Equatable { {{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}public{{/nonPublicApi}} let wrappedDate: Date {{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}public{{/nonPublicApi}} let timezone: TimeZone @@ -40,7 +40,7 @@ import Foundation self.init(wrappedDate: wrappedDate, timezone: timezone) } - /// Designated Initializer for `DateWithoutTime` + /// Designated Initializer for `OpenAPIDateWithoutTime` /// /// Since usually `Date`s without time components - as e.g. birthdays - are created Calendar- and timezone-aware // it is important to also provide a timezone so that the generator is able to normalize the supplied Date to ISO8601 (GMT+0) @@ -56,8 +56,14 @@ import Foundation } /// Normalizes the wrappedDate to GMT+0 according to the supplied timezone - internal func normalizedWrappedDate() -> Date { + fileprivate func normalizedWrappedDate() -> Date { return wrappedDate.addingTimeInterval( Double(timezone.secondsFromGMT(for: wrappedDate))) } +} + +extension OpenAPIDateWithoutTime: JSONEncodable { + func encodeToJSON() -> Any { + return OpenISO8601DateFormatter.withoutTime.string(from: self.normalizedWrappedDate()) + } } \ No newline at end of file diff --git a/modules/openapi-generator/src/test/java/org/openapitools/codegen/swift5/Swift5ClientCodegenTest.java b/modules/openapi-generator/src/test/java/org/openapitools/codegen/swift5/Swift5ClientCodegenTest.java index 1b590c74fdc2..59928dbc2662 100644 --- a/modules/openapi-generator/src/test/java/org/openapitools/codegen/swift5/Swift5ClientCodegenTest.java +++ b/modules/openapi-generator/src/test/java/org/openapitools/codegen/swift5/Swift5ClientCodegenTest.java @@ -150,7 +150,7 @@ public class Swift5ClientCodegenTest { Assert.assertEquals(op.bodyParam.dataType, "Date"); } - @Test(description = "returns DateWithoutTime when response format is date and cli option is enabled", enabled = true) + @Test(description = "returns OpenAPIDateWithoutTime when response format is date and cli option is enabled", enabled = true) public void dateWithoutTimeTest() { final OpenAPI openAPI = TestUtils.parseFlattenSpec("src/test/resources/2_0/datePropertyTest.json"); final DefaultCodegen codegen = new Swift5ClientCodegen(); @@ -162,8 +162,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, "DateWithoutTime"); - Assert.assertEquals(op.bodyParam.dataType, "DateWithoutTime"); + Assert.assertEquals(op.returnType, "OpenAPIDateWithoutTime"); + Assert.assertEquals(op.bodyParam.dataType, "OpenAPIDateWithoutTime"); } @Test(enabled = true) diff --git a/modules/openapi-generator/src/test/java/org/openapitools/codegen/swift5/Swift5ModelTest.java b/modules/openapi-generator/src/test/java/org/openapitools/codegen/swift5/Swift5ModelTest.java index 4fa8016e4161..6a20d384a0ed 100644 --- a/modules/openapi-generator/src/test/java/org/openapitools/codegen/swift5/Swift5ModelTest.java +++ b/modules/openapi-generator/src/test/java/org/openapitools/codegen/swift5/Swift5ModelTest.java @@ -155,10 +155,10 @@ public class Swift5ModelTest { Assert.assertFalse(property3.isContainer); Assert.assertEquals(property7.baseName, "dateOfBirth"); - Assert.assertEquals(property7.dataType, "DateWithoutTime"); + Assert.assertEquals(property7.dataType, "OpenAPIDateWithoutTime"); Assert.assertEquals(property7.name, "dateOfBirth"); Assert.assertNull(property7.defaultValue); - Assert.assertEquals(property7.baseType, "DateWithoutTime"); + Assert.assertEquals(property7.baseType, "OpenAPIDateWithoutTime"); Assert.assertFalse(property7.required); Assert.assertFalse(property7.isContainer); }