Skip to content

Commit 559abbe

Browse files
committed
Update swift5 CodableHelper
1 parent 7ddbe80 commit 559abbe

File tree

6 files changed

+198
-42
lines changed

6 files changed

+198
-42
lines changed

modules/swagger-codegen/src/main/resources/swift5/CodableHelper.mustache

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ import Foundation
99

1010
public typealias EncodeResult = (data: Data?, error: Error?)
1111

12+
enum DateError: String, Error {
13+
case invalidDate
14+
}
15+
1216
open class CodableHelper {
1317
1418
public static var dateformatter: DateFormatter?
@@ -18,16 +22,39 @@ open class CodableHelper {
1822
var returnedError: Error? = nil
1923
2024
let decoder = JSONDecoder()
25+
2126
if let df = self.dateformatter {
2227
decoder.dateDecodingStrategy = .formatted(df)
2328
} else {
2429
decoder.dataDecodingStrategy = .base64
25-
let formatter = DateFormatter()
26-
formatter.calendar = Calendar(identifier: .iso8601)
27-
formatter.locale = Locale(identifier: "en_US_POSIX")
28-
formatter.timeZone = TimeZone(secondsFromGMT: 0)
29-
formatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSXXXXX"
30-
decoder.dateDecodingStrategy = .formatted(formatter)
30+
decoder.dateDecodingStrategy = .custom({ (decoder) -> Date in
31+
let container = try decoder.singleValueContainer()
32+
let dateStr = try container.decode(String.self)
33+
34+
let formatters = [
35+
"yyyy-MM-dd",
36+
"yyyy-MM-dd'T'HH:mm:ssZZZZZ",
37+
"yyyy-MM-dd'T'HH:mm:ss.SSSZZZZZ",
38+
"yyyy-MM-dd'T'HH:mm:ss'Z'",
39+
"yyyy-MM-dd'T'HH:mm:ss.SSS",
40+
"yyyy-MM-dd'T'HH:mm:ss.SSS'Z'",
41+
"yyyy-MM-dd HH:mm:ss"
42+
].map { (format: String) -> DateFormatter in
43+
let formatter = DateFormatter()
44+
formatter.locale = Locale(identifier: "en_US_POSIX")
45+
formatter.dateFormat = format
46+
return formatter
47+
}
48+
49+
for formatter in formatters {
50+
51+
if let date = formatter.date(from: dateStr) {
52+
return date
53+
}
54+
}
55+
56+
throw DateError.invalidDate
57+
})
3158
}
3259

3360
do {
@@ -63,5 +90,4 @@ open class CodableHelper {
6390

6491
return (returnedData, returnedError)
6592
}
66-
6793
}

samples/client/petstore/swift5/default/PetstoreClient/Classes/Swaggers/CodableHelper.swift

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ import Foundation
99

1010
public typealias EncodeResult = (data: Data?, error: Error?)
1111

12+
enum DateError: String, Error {
13+
case invalidDate
14+
}
15+
1216
open class CodableHelper {
1317

1418
public static var dateformatter: DateFormatter?
@@ -18,16 +22,39 @@ open class CodableHelper {
1822
var returnedError: Error? = nil
1923

2024
let decoder = JSONDecoder()
25+
2126
if let df = self.dateformatter {
2227
decoder.dateDecodingStrategy = .formatted(df)
2328
} else {
2429
decoder.dataDecodingStrategy = .base64
25-
let formatter = DateFormatter()
26-
formatter.calendar = Calendar(identifier: .iso8601)
27-
formatter.locale = Locale(identifier: "en_US_POSIX")
28-
formatter.timeZone = TimeZone(secondsFromGMT: 0)
29-
formatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSXXXXX"
30-
decoder.dateDecodingStrategy = .formatted(formatter)
30+
decoder.dateDecodingStrategy = .custom({ (decoder) -> Date in
31+
let container = try decoder.singleValueContainer()
32+
let dateStr = try container.decode(String.self)
33+
34+
let formatters = [
35+
"yyyy-MM-dd",
36+
"yyyy-MM-dd'T'HH:mm:ssZZZZZ",
37+
"yyyy-MM-dd'T'HH:mm:ss.SSSZZZZZ",
38+
"yyyy-MM-dd'T'HH:mm:ss'Z'",
39+
"yyyy-MM-dd'T'HH:mm:ss.SSS",
40+
"yyyy-MM-dd'T'HH:mm:ss.SSS'Z'",
41+
"yyyy-MM-dd HH:mm:ss"
42+
].map { (format: String) -> DateFormatter in
43+
let formatter = DateFormatter()
44+
formatter.locale = Locale(identifier: "en_US_POSIX")
45+
formatter.dateFormat = format
46+
return formatter
47+
}
48+
49+
for formatter in formatters {
50+
51+
if let date = formatter.date(from: dateStr) {
52+
return date
53+
}
54+
}
55+
56+
throw DateError.invalidDate
57+
})
3158
}
3259

3360
do {
@@ -63,5 +90,4 @@ open class CodableHelper {
6390

6491
return (returnedData, returnedError)
6592
}
66-
6793
}

samples/client/petstore/swift5/objcCompatible/PetstoreClient/Classes/Swaggers/CodableHelper.swift

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ import Foundation
99

1010
public typealias EncodeResult = (data: Data?, error: Error?)
1111

12+
enum DateError: String, Error {
13+
case invalidDate
14+
}
15+
1216
open class CodableHelper {
1317

1418
public static var dateformatter: DateFormatter?
@@ -18,16 +22,39 @@ open class CodableHelper {
1822
var returnedError: Error? = nil
1923

2024
let decoder = JSONDecoder()
25+
2126
if let df = self.dateformatter {
2227
decoder.dateDecodingStrategy = .formatted(df)
2328
} else {
2429
decoder.dataDecodingStrategy = .base64
25-
let formatter = DateFormatter()
26-
formatter.calendar = Calendar(identifier: .iso8601)
27-
formatter.locale = Locale(identifier: "en_US_POSIX")
28-
formatter.timeZone = TimeZone(secondsFromGMT: 0)
29-
formatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSXXXXX"
30-
decoder.dateDecodingStrategy = .formatted(formatter)
30+
decoder.dateDecodingStrategy = .custom({ (decoder) -> Date in
31+
let container = try decoder.singleValueContainer()
32+
let dateStr = try container.decode(String.self)
33+
34+
let formatters = [
35+
"yyyy-MM-dd",
36+
"yyyy-MM-dd'T'HH:mm:ssZZZZZ",
37+
"yyyy-MM-dd'T'HH:mm:ss.SSSZZZZZ",
38+
"yyyy-MM-dd'T'HH:mm:ss'Z'",
39+
"yyyy-MM-dd'T'HH:mm:ss.SSS",
40+
"yyyy-MM-dd'T'HH:mm:ss.SSS'Z'",
41+
"yyyy-MM-dd HH:mm:ss"
42+
].map { (format: String) -> DateFormatter in
43+
let formatter = DateFormatter()
44+
formatter.locale = Locale(identifier: "en_US_POSIX")
45+
formatter.dateFormat = format
46+
return formatter
47+
}
48+
49+
for formatter in formatters {
50+
51+
if let date = formatter.date(from: dateStr) {
52+
return date
53+
}
54+
}
55+
56+
throw DateError.invalidDate
57+
})
3158
}
3259

3360
do {
@@ -63,5 +90,4 @@ open class CodableHelper {
6390

6491
return (returnedData, returnedError)
6592
}
66-
6793
}

samples/client/petstore/swift5/promisekit/PetstoreClient/Classes/Swaggers/CodableHelper.swift

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ import Foundation
99

1010
public typealias EncodeResult = (data: Data?, error: Error?)
1111

12+
enum DateError: String, Error {
13+
case invalidDate
14+
}
15+
1216
open class CodableHelper {
1317

1418
public static var dateformatter: DateFormatter?
@@ -18,16 +22,39 @@ open class CodableHelper {
1822
var returnedError: Error? = nil
1923

2024
let decoder = JSONDecoder()
25+
2126
if let df = self.dateformatter {
2227
decoder.dateDecodingStrategy = .formatted(df)
2328
} else {
2429
decoder.dataDecodingStrategy = .base64
25-
let formatter = DateFormatter()
26-
formatter.calendar = Calendar(identifier: .iso8601)
27-
formatter.locale = Locale(identifier: "en_US_POSIX")
28-
formatter.timeZone = TimeZone(secondsFromGMT: 0)
29-
formatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSXXXXX"
30-
decoder.dateDecodingStrategy = .formatted(formatter)
30+
decoder.dateDecodingStrategy = .custom({ (decoder) -> Date in
31+
let container = try decoder.singleValueContainer()
32+
let dateStr = try container.decode(String.self)
33+
34+
let formatters = [
35+
"yyyy-MM-dd",
36+
"yyyy-MM-dd'T'HH:mm:ssZZZZZ",
37+
"yyyy-MM-dd'T'HH:mm:ss.SSSZZZZZ",
38+
"yyyy-MM-dd'T'HH:mm:ss'Z'",
39+
"yyyy-MM-dd'T'HH:mm:ss.SSS",
40+
"yyyy-MM-dd'T'HH:mm:ss.SSS'Z'",
41+
"yyyy-MM-dd HH:mm:ss"
42+
].map { (format: String) -> DateFormatter in
43+
let formatter = DateFormatter()
44+
formatter.locale = Locale(identifier: "en_US_POSIX")
45+
formatter.dateFormat = format
46+
return formatter
47+
}
48+
49+
for formatter in formatters {
50+
51+
if let date = formatter.date(from: dateStr) {
52+
return date
53+
}
54+
}
55+
56+
throw DateError.invalidDate
57+
})
3158
}
3259

3360
do {
@@ -63,5 +90,4 @@ open class CodableHelper {
6390

6491
return (returnedData, returnedError)
6592
}
66-
6793
}

samples/client/petstore/swift5/rxswift/PetstoreClient/Classes/Swaggers/CodableHelper.swift

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ import Foundation
99

1010
public typealias EncodeResult = (data: Data?, error: Error?)
1111

12+
enum DateError: String, Error {
13+
case invalidDate
14+
}
15+
1216
open class CodableHelper {
1317

1418
public static var dateformatter: DateFormatter?
@@ -18,16 +22,39 @@ open class CodableHelper {
1822
var returnedError: Error? = nil
1923

2024
let decoder = JSONDecoder()
25+
2126
if let df = self.dateformatter {
2227
decoder.dateDecodingStrategy = .formatted(df)
2328
} else {
2429
decoder.dataDecodingStrategy = .base64
25-
let formatter = DateFormatter()
26-
formatter.calendar = Calendar(identifier: .iso8601)
27-
formatter.locale = Locale(identifier: "en_US_POSIX")
28-
formatter.timeZone = TimeZone(secondsFromGMT: 0)
29-
formatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSXXXXX"
30-
decoder.dateDecodingStrategy = .formatted(formatter)
30+
decoder.dateDecodingStrategy = .custom({ (decoder) -> Date in
31+
let container = try decoder.singleValueContainer()
32+
let dateStr = try container.decode(String.self)
33+
34+
let formatters = [
35+
"yyyy-MM-dd",
36+
"yyyy-MM-dd'T'HH:mm:ssZZZZZ",
37+
"yyyy-MM-dd'T'HH:mm:ss.SSSZZZZZ",
38+
"yyyy-MM-dd'T'HH:mm:ss'Z'",
39+
"yyyy-MM-dd'T'HH:mm:ss.SSS",
40+
"yyyy-MM-dd'T'HH:mm:ss.SSS'Z'",
41+
"yyyy-MM-dd HH:mm:ss"
42+
].map { (format: String) -> DateFormatter in
43+
let formatter = DateFormatter()
44+
formatter.locale = Locale(identifier: "en_US_POSIX")
45+
formatter.dateFormat = format
46+
return formatter
47+
}
48+
49+
for formatter in formatters {
50+
51+
if let date = formatter.date(from: dateStr) {
52+
return date
53+
}
54+
}
55+
56+
throw DateError.invalidDate
57+
})
3158
}
3259

3360
do {
@@ -63,5 +90,4 @@ open class CodableHelper {
6390

6491
return (returnedData, returnedError)
6592
}
66-
6793
}

samples/client/test/swift5/default/TestClient/Classes/Swaggers/CodableHelper.swift

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ import Foundation
99

1010
public typealias EncodeResult = (data: Data?, error: Error?)
1111

12+
enum DateError: String, Error {
13+
case invalidDate
14+
}
15+
1216
open class CodableHelper {
1317

1418
public static var dateformatter: DateFormatter?
@@ -18,16 +22,39 @@ open class CodableHelper {
1822
var returnedError: Error? = nil
1923

2024
let decoder = JSONDecoder()
25+
2126
if let df = self.dateformatter {
2227
decoder.dateDecodingStrategy = .formatted(df)
2328
} else {
2429
decoder.dataDecodingStrategy = .base64
25-
let formatter = DateFormatter()
26-
formatter.calendar = Calendar(identifier: .iso8601)
27-
formatter.locale = Locale(identifier: "en_US_POSIX")
28-
formatter.timeZone = TimeZone(secondsFromGMT: 0)
29-
formatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSXXXXX"
30-
decoder.dateDecodingStrategy = .formatted(formatter)
30+
decoder.dateDecodingStrategy = .custom({ (decoder) -> Date in
31+
let container = try decoder.singleValueContainer()
32+
let dateStr = try container.decode(String.self)
33+
34+
let formatters = [
35+
"yyyy-MM-dd",
36+
"yyyy-MM-dd'T'HH:mm:ssZZZZZ",
37+
"yyyy-MM-dd'T'HH:mm:ss.SSSZZZZZ",
38+
"yyyy-MM-dd'T'HH:mm:ss'Z'",
39+
"yyyy-MM-dd'T'HH:mm:ss.SSS",
40+
"yyyy-MM-dd'T'HH:mm:ss.SSS'Z'",
41+
"yyyy-MM-dd HH:mm:ss"
42+
].map { (format: String) -> DateFormatter in
43+
let formatter = DateFormatter()
44+
formatter.locale = Locale(identifier: "en_US_POSIX")
45+
formatter.dateFormat = format
46+
return formatter
47+
}
48+
49+
for formatter in formatters {
50+
51+
if let date = formatter.date(from: dateStr) {
52+
return date
53+
}
54+
}
55+
56+
throw DateError.invalidDate
57+
})
3158
}
3259

3360
do {
@@ -63,5 +90,4 @@ open class CodableHelper {
6390

6491
return (returnedData, returnedError)
6592
}
66-
6793
}

0 commit comments

Comments
 (0)