tart icon indicating copy to clipboard operation
tart copied to clipboard

Swift 6 Support & Sendable issue

Open nholloh opened this issue 9 months ago • 1 comments

While working on !1022 I saw that the compiler currently issues a warning about the ISO8601DateFormatter used when decoding the TokenResponse, as the ISO8601DateFormatter does not conform to Sendable.

  static func parse(fromData: Data) throws -> Self {
    let decoder = Config.jsonDecoder()

    decoder.keyDecodingStrategy = .convertFromSnakeCase

    let dateFormatter = ISO8601DateFormatter()
    dateFormatter.formatOptions = [.withInternetDateTime]
    dateFormatter.timeZone = TimeZone(secondsFromGMT: 0)

    decoder.dateDecodingStrategy = .custom { decoder in
      let container = try decoder.singleValueContainer()
      let dateString = try container.decode(String.self)

      return dateFormatter.date(from: dateString) ?? Date()
    }

    var response = try decoder.decode(TokenResponse.self, from: fromData)
    response.issuedAt = response.issuedAt ?? Date()

    guard response.token != nil || response.accessToken != nil else {
      throw DecodingError.keyNotFound(CodingKeys.token, .init(codingPath: [], debugDescription: "Missing token or access_token. One must be present."))
    }

    return response
  }

Since this is an error in Swift 6, it should probably be addressed. Before doing the easy thing moving the instantiation and configuration of the ISO8601DateFormatter into the closure I wanted to understand what the reason was to choose a custom decoder over the standard .iso8601 date decoding strategy?

nholloh avatar Feb 03 '25 18:02 nholloh