Fix some pixelfed hashtags not being decodable

This commit is contained in:
Shadowfacts 2022-04-06 21:34:40 -04:00
parent 3d0402c1e0
commit 2798a199aa
1 changed files with 14 additions and 0 deletions

View File

@ -7,6 +7,8 @@
//
import Foundation
import WebURL
import WebURLFoundationExtras
public class Hashtag: Codable {
public let name: String
@ -20,6 +22,18 @@ public class Hashtag: Codable {
self.history = nil
}
public required init(from decoder: Decoder) throws {
let container = try decoder.container(keyedBy: CodingKeys.self)
self.name = try container.decode(String.self, forKey: .name)
// pixelfed (possibly others) don't fully escape special characters in the hashtag url
if let url = URL(try container.decode(WebURL.self, forKey: .url)) {
self.url = url
} else {
throw DecodingError.dataCorruptedError(forKey: .url, in: container, debugDescription: "unable to convert WebURL to URL")
}
self.history = try container.decodeIfPresent([History].self, forKey: .history)
}
private enum CodingKeys: String, CodingKey {
case name
case url