Skip to content

Commit 6eeecc1

Browse files
DevMobileASwing3284brunu
authored
Fix decoding OpenAPIDateWithoutTime (#17146)
* Fix decoding OpenAPIDateWithoutTime which in previous implementation only worked when used with CodableHelper, because it encoded to String, but tried to decode from Date afterwards * update samples * Update OpenAPIDateWithoutTime.mustache * Update sample projects --------- Co-authored-by: William Cheng <wing328hk@gmail.com> Co-authored-by: Bruno Coelho <4brunu@users.noreply.github.com> Co-authored-by: Bruno Coelho <4brunu@gmail.com>
1 parent 1105759 commit 6eeecc1

File tree

3 files changed

+39
-4
lines changed

3 files changed

+39
-4
lines changed

modules/openapi-generator/src/main/resources/swift5/OpenAPIDateWithoutTime.mustache

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,21 @@ import Foundation
2323
case wrappedDate
2424
case timezone
2525
}
26+
27+
{{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}public{{/nonPublicApi}} enum DecodingError: Error {
28+
case notADateString
29+
}
2630

2731
/// On decoding ISO8601 timezone is assumed
2832
{{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}public{{/nonPublicApi}} init(from decoder: Decoder) throws {
2933
let container = try decoder.singleValueContainer()
30-
self.wrappedDate = try container.decode(Date.self)
34+
35+
let dateString = try container.decode(String.self)
36+
guard let date = OpenISO8601DateFormatter.withoutTime.date(from: dateString) else {
37+
throw DecodingError.notADateString
38+
}
39+
self.wrappedDate = date
40+
3141
self.timezone = OpenISO8601DateFormatter.withoutTime.timeZone
3242
}
3343

@@ -60,10 +70,14 @@ import Foundation
6070
return wrappedDate.addingTimeInterval(
6171
Double(timezone.secondsFromGMT(for: wrappedDate)))
6272
}
73+
74+
{{#nonPublicApi}}internal{{/nonPublicApi}}{{^nonPublicApi}}public{{/nonPublicApi}} static func == (lhs: Self, rhs: Self) -> Bool {
75+
Calendar.current.compare(lhs.wrappedDate, to: rhs.wrappedDate, toGranularity: .day) == .orderedSame
76+
}
6377
}
6478

6579
extension OpenAPIDateWithoutTime: JSONEncodable {
6680
func encodeToJSON() -> Any {
6781
return OpenISO8601DateFormatter.withoutTime.string(from: self.normalizedWrappedDate())
6882
}
69-
}
83+
}

samples/client/petstore/swift5/alamofireLibrary/PetstoreClient/Classes/OpenAPIs/OpenAPIDateWithoutTime.swift

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,21 @@ public struct OpenAPIDateWithoutTime: Codable, Hashable, Equatable {
2323
case wrappedDate
2424
case timezone
2525
}
26+
27+
public enum DecodingError: Error {
28+
case notADateString
29+
}
2630

2731
/// On decoding ISO8601 timezone is assumed
2832
public init(from decoder: Decoder) throws {
2933
let container = try decoder.singleValueContainer()
30-
self.wrappedDate = try container.decode(Date.self)
34+
35+
let dateString = try container.decode(String.self)
36+
guard let date = OpenISO8601DateFormatter.withoutTime.date(from: dateString) else {
37+
throw DecodingError.notADateString
38+
}
39+
self.wrappedDate = date
40+
3141
self.timezone = OpenISO8601DateFormatter.withoutTime.timeZone
3242
}
3343

@@ -60,10 +70,14 @@ public struct OpenAPIDateWithoutTime: Codable, Hashable, Equatable {
6070
return wrappedDate.addingTimeInterval(
6171
Double(timezone.secondsFromGMT(for: wrappedDate)))
6272
}
73+
74+
public static func == (lhs: Self, rhs: Self) -> Bool {
75+
Calendar.current.compare(lhs.wrappedDate, to: rhs.wrappedDate, toGranularity: .day) == .orderedSame
76+
}
6377
}
6478

6579
extension OpenAPIDateWithoutTime: JSONEncodable {
6680
func encodeToJSON() -> Any {
6781
return OpenISO8601DateFormatter.withoutTime.string(from: self.normalizedWrappedDate())
6882
}
69-
}
83+
}

samples/client/petstore/swift5/alamofireLibrary/SwaggerClientTests/SwaggerClientTests/DateFormatTests.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,4 +111,11 @@ class DateFormatTests: XCTestCase {
111111
XCTAssert(jsonString == exampleJSONString, "Encoded JSON String: \(jsonString) should match: \(exampleJSONString)")
112112
}
113113

114+
func testCodableOpenAPIDateWithoutTime() throws {
115+
let sut = OpenAPIDateWithoutTime(wrappedDate: Date(timeIntervalSince1970: 0))
116+
let encodedDate = try JSONEncoder().encode(sut)
117+
let decodedDate = try JSONDecoder().decode(OpenAPIDateWithoutTime.self, from: encodedDate)
118+
119+
XCTAssert(sut == decodedDate, "Decoded date: \(decodedDate) should match initially given date: \(String(describing: sut))")
120+
}
114121
}

0 commit comments

Comments
 (0)