Browse Source

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.
simple-swift-promises
Shadowfacts 6 months ago
parent
commit
787dc9f24f
Signed by: Shadowfacts <me@shadowfacts.net> GPG Key ID: 94A5AB95422746E5
1 changed files with 13 additions and 0 deletions
  1. 13
    0
      Pachyderm/Model/Application.swift

+ 13
- 0
Pachyderm/Model/Application.swift View File

@@ -12,6 +12,19 @@ public class Application: Decodable {
12 12
     public let name: String
13 13
     public let website: URL?
14 14
     
15
+    public required init(from decoder: Decoder) throws {
16
+        let container = try decoder.container(keyedBy: CodingKeys.self)
17
+        
18
+        self.name = try container.decode(String.self, forKey: .name)
19
+        
20
+        if let websiteStr = try container.decodeIfPresent(String.self, forKey: .website),
21
+            let url = URL(string: websiteStr) {
22
+            self.website = url
23
+        } else {
24
+            self.website = nil
25
+        }
26
+    }
27
+    
15 28
     private enum CodingKeys: String, CodingKey {
16 29
         case name
17 30
         case website

Loading…
Cancel
Save