diff --git a/modules/openapi-generator/src/main/resources/swift5/DateWithoutTime.mustache b/modules/openapi-generator/src/main/resources/swift5/DateWithoutTime.mustache index cb0c64d59df2..dc472f031811 100644 --- a/modules/openapi-generator/src/main/resources/swift5/DateWithoutTime.mustache +++ b/modules/openapi-generator/src/main/resources/swift5/DateWithoutTime.mustache @@ -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. -public struct DateWithoutTime: Codable, Hashable, Equatable { +{{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}public{{/nonPublicApi}} struct DateWithoutTime: Codable, Hashable, Equatable { {{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}public{{/nonPublicApi}} let wrappedDate: Date {{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}public{{/nonPublicApi}} let timezone: TimeZone diff --git a/samples/client/petstore/swift5/nonPublicApi/PetstoreClient/Classes/OpenAPIs/DateWithoutTime.swift b/samples/client/petstore/swift5/nonPublicApi/PetstoreClient/Classes/OpenAPIs/DateWithoutTime.swift new file mode 100644 index 000000000000..cb3c73aa0e50 --- /dev/null +++ b/samples/client/petstore/swift5/nonPublicApi/PetstoreClient/Classes/OpenAPIs/DateWithoutTime.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. +internal struct DateWithoutTime: Codable, Hashable, Equatable { + internal let wrappedDate: Date + internal let timezone: TimeZone + + internal enum CodingKeys: CodingKey, CaseIterable { + case wrappedDate + case timezone + } + + internal init(from decoder: Decoder) throws { + let container = try decoder.singleValueContainer() + self.wrappedDate = try container.decode(Date.self) + self.timezone = OpenISO8601DateFormatter.withoutTime.timeZone + } + + internal init?(wrappedDate: Date?, timezone: TimeZone = .current) { + guard let wrappedDate = wrappedDate else { + return nil + } + + self.init(wrappedDate: wrappedDate, timezone: timezone) + } + + internal init(wrappedDate: Date, timezone: TimeZone) { + self.wrappedDate = wrappedDate + self.timezone = timezone + } + + internal 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))) + } +} \ No newline at end of file