Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Issue 9178] Handle multiple formats when decoding dates #9730

Merged
merged 4 commits into from
Oct 3, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ import Foundation

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

enum DateError: String, Error {
case invalidDate
}

open class CodableHelper {

open static var dateformatter: DateFormatter?
Expand All @@ -18,16 +22,39 @@ open class CodableHelper {
var returnedError: Error? = nil

let decoder = JSONDecoder()

if let df = self.dateformatter {
decoder.dateDecodingStrategy = .formatted(df)
} else {
decoder.dataDecodingStrategy = .base64
let formatter = DateFormatter()
formatter.calendar = Calendar(identifier: .iso8601)
formatter.locale = Locale(identifier: "en_US_POSIX")
formatter.timeZone = TimeZone(secondsFromGMT: 0)
formatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSXXXXX"
decoder.dateDecodingStrategy = .formatted(formatter)
decoder.dateDecodingStrategy = .custom({ (decoder) -> Date in
let container = try decoder.singleValueContainer()
let dateStr = try container.decode(String.self)

let formatters = [
"yyyy-MM-dd",
"yyyy-MM-dd'T'HH:mm:ssZZZZZ",
"yyyy-MM-dd'T'HH:mm:ss.SSSZZZZZ",
"yyyy-MM-dd'T'HH:mm:ss'Z'",
"yyyy-MM-dd'T'HH:mm:ss.SSS",
"yyyy-MM-dd'T'HH:mm:ss.SSS'Z'",
"yyyy-MM-dd HH:mm:ss"
].map { (format: String) -> DateFormatter in
let formatter = DateFormatter()
formatter.locale = Locale(identifier: "en_US_POSIX")
formatter.dateFormat = format
return formatter
}

for formatter in formatters {

if let date = formatter.date(from: dateStr) {
return date
}
}

throw DateError.invalidDate
})
}

do {
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2.4.0-SNAPSHOT
2.4.9-SNAPSHOT
Original file line number Diff line number Diff line change
@@ -1 +1 @@
2.4.3-SNAPSHOT
2.4.9-SNAPSHOT
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,7 @@ open class FakeClassnameTags123API {
let parameters = JSONEncodingHelper.encodingParameters(forEncodableObject: body)

var url = URLComponents(string: URLString)
url?.queryItems = APIHelper.mapValuesToQueryItems([
])
url?.queryItems = APIHelper.mapValuesToQueryItems([:])

let requestBuilder: RequestBuilder<Client>.Type = PetstoreClientAPI.requestBuilderFactory.getBuilder()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ class AlamofireRequestBuilderFactory: RequestBuilderFactory {
// Store manager to retain its reference
private var managerStore: [String: Alamofire.SessionManager] = [:]

// Sync queue to manage safe access to the store manager
private let syncQueue = DispatchQueue(label: "thread-safe-sync-queue", attributes: .concurrent)

open class AlamofireRequestBuilder<T>: RequestBuilder<T> {
required public init(method: String, URLString: String, parameters: [String : Any]?, isBody: Bool, headers: [String : String] = [:]) {
super.init(method: method, URLString: URLString, parameters: parameters, isBody: isBody, headers: headers)
Expand Down Expand Up @@ -58,7 +61,9 @@ open class AlamofireRequestBuilder<T>: RequestBuilder<T> {
let managerId:String = UUID().uuidString
// Create a new manager for each request to customize its request header
let manager = createSessionManager()
managerStore[managerId] = manager
syncQueue.async(flags: .barrier) {
managerStore[managerId] = manager
}

let encoding:ParameterEncoding = isBody ? JSONDataEncoding() : URLEncoding()

Expand Down Expand Up @@ -112,7 +117,9 @@ open class AlamofireRequestBuilder<T>: RequestBuilder<T> {
}

let cleanupRequest = {
_ = managerStore.removeValue(forKey: managerId)
syncQueue.async(flags: .barrier) {
_ = managerStore.removeValue(forKey: managerId)
}
}

let validatedRequest = request.validate()
Expand Down Expand Up @@ -314,7 +321,9 @@ open class AlamofireDecodableRequestBuilder<T:Decodable>: AlamofireRequestBuilde
}

let cleanupRequest = {
_ = managerStore.removeValue(forKey: managerId)
syncQueue.async(flags: .barrier) {
_ = managerStore.removeValue(forKey: managerId)
}
}

let validatedRequest = request.validate()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,10 @@ import Foundation

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

enum DateError: String, Error {
case invalidDate
}

open class CodableHelper {

open static var dateformatter: DateFormatter?
Expand All @@ -18,16 +22,39 @@ open class CodableHelper {
var returnedError: Error? = nil

let decoder = JSONDecoder()

if let df = self.dateformatter {
decoder.dateDecodingStrategy = .formatted(df)
} else {
decoder.dataDecodingStrategy = .base64
let formatter = DateFormatter()
formatter.calendar = Calendar(identifier: .iso8601)
formatter.locale = Locale(identifier: "en_US_POSIX")
formatter.timeZone = TimeZone(secondsFromGMT: 0)
formatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSSXXXXX"
decoder.dateDecodingStrategy = .formatted(formatter)
decoder.dateDecodingStrategy = .custom({ (decoder) -> Date in
let container = try decoder.singleValueContainer()
let dateStr = try container.decode(String.self)

let formatters = [
"yyyy-MM-dd",
"yyyy-MM-dd'T'HH:mm:ssZZZZZ",
"yyyy-MM-dd'T'HH:mm:ss.SSSZZZZZ",
"yyyy-MM-dd'T'HH:mm:ss'Z'",
"yyyy-MM-dd'T'HH:mm:ss.SSS",
"yyyy-MM-dd'T'HH:mm:ss.SSS'Z'",
"yyyy-MM-dd HH:mm:ss"
].map { (format: String) -> DateFormatter in
let formatter = DateFormatter()
formatter.locale = Locale(identifier: "en_US_POSIX")
formatter.dateFormat = format
return formatter
}

for formatter in formatters {

if let date = formatter.date(from: dateStr) {
return date
}
}

throw DateError.invalidDate
})
}

do {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ end
post_install do |installer|
installer.pods_project.targets.each do |target|
target.build_configurations.each do |configuration|
configuration.build_settings['SWIFT_VERSION'] = "3.0"
configuration.build_settings['SWIFT_VERSION'] = "4.2"
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,18 @@ PODS:
DEPENDENCIES:
- PetstoreClient (from `../`)

SPEC REPOS:
https://github.com/cocoapods/specs.git:
- Alamofire

EXTERNAL SOURCES:
PetstoreClient:
:path: ../
:path: "../"

SPEC CHECKSUMS:
Alamofire: f28cdffd29de33a7bfa022cbd63ae95a27fae140
PetstoreClient: fef7934ac90bd28c2b81cc5234615112f9ace5f5
PetstoreClient: 9f1c86c3f7194758e922fb64277bfb4fc60bbf20

PODFILE CHECKSUM: 417049e9ed0e4680602b34d838294778389bd418
PODFILE CHECKSUM: 065fc3a3ec186d4f60ef8c7d9624786f07955f2f

COCOAPODS: 1.4.0
COCOAPODS: 1.7.1

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading