Fix crash decoding statuses sent from certain applications
If an application provides its URL as an empty string, decoding it would cause throw an error because Foundation's URL class does not accept empty strings. Instead, during parsing, consume the invalid URL and replace it with a `nil` app URL.
This commit is contained in:
parent
ad09e36907
commit
787dc9f24f
|
@ -12,6 +12,19 @@ public class Application: Decodable {
|
||||||
public let name: String
|
public let name: String
|
||||||
public let website: URL?
|
public let website: URL?
|
||||||
|
|
||||||
|
public required init(from decoder: Decoder) throws {
|
||||||
|
let container = try decoder.container(keyedBy: CodingKeys.self)
|
||||||
|
|
||||||
|
self.name = try container.decode(String.self, forKey: .name)
|
||||||
|
|
||||||
|
if let websiteStr = try container.decodeIfPresent(String.self, forKey: .website),
|
||||||
|
let url = URL(string: websiteStr) {
|
||||||
|
self.website = url
|
||||||
|
} else {
|
||||||
|
self.website = nil
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private enum CodingKeys: String, CodingKey {
|
private enum CodingKeys: String, CodingKey {
|
||||||
case name
|
case name
|
||||||
case website
|
case website
|
||||||
|
|
Loading…
Reference in New Issue