From 8f6a0125380ed47dfa87ccb24a24ecaf08eaf4de Mon Sep 17 00:00:00 2001 From: Shadowfacts Date: Mon, 8 May 2023 17:05:06 -0400 Subject: [PATCH] Fix decoding statuses on GtS with empty strings for urls Closes #373 See #129 --- .../Sources/Pachyderm/Model/Status.swift | 8 ++- .../Tests/PachydermTests/StatusTests.swift | 53 +++++++++++++++++++ 2 files changed, 59 insertions(+), 2 deletions(-) create mode 100644 Packages/Pachyderm/Tests/PachydermTests/StatusTests.swift diff --git a/Packages/Pachyderm/Sources/Pachyderm/Model/Status.swift b/Packages/Pachyderm/Sources/Pachyderm/Model/Status.swift index a5ff6f37ad..e26224a237 100644 --- a/Packages/Pachyderm/Sources/Pachyderm/Model/Status.swift +++ b/Packages/Pachyderm/Sources/Pachyderm/Model/Status.swift @@ -51,8 +51,12 @@ public final class Status: StatusProtocol, Decodable, Sendable { do { self.url = try container.decodeIfPresent(WebURL.self, forKey: .url) } catch { - let s = (try? container.decode(String.self, forKey: .url)) ?? "" - throw DecodingError.dataCorruptedError(forKey: .url, in: container, debugDescription: "Could not decode URL '\(s)', reason: \(String(describing: error))") + let s = try? container.decode(String.self, forKey: .url) + if s == "" { + self.url = nil + } else { + throw DecodingError.dataCorrupted(.init(codingPath: container.codingPath + [CodingKeys.url], debugDescription: "Could not decode URL '\(s ?? "")'", underlyingError: error)) + } } self.account = try container.decode(Account.self, forKey: .account) self.inReplyToID = try container.decodeIfPresent(String.self, forKey: .inReplyToID) diff --git a/Packages/Pachyderm/Tests/PachydermTests/StatusTests.swift b/Packages/Pachyderm/Tests/PachydermTests/StatusTests.swift new file mode 100644 index 0000000000..3030af57d9 --- /dev/null +++ b/Packages/Pachyderm/Tests/PachydermTests/StatusTests.swift @@ -0,0 +1,53 @@ +// +// StatusTests.swift +// +// +// Created by Shadowfacts on 5/8/23. +// + +import XCTest +@testable import Pachyderm + +final class StatusTests: XCTestCase { + + func testDecode() { + let data = """ +{ + "id": "1", + "uri": "https://example.com/a/1", + "url": "", + "account": { + "id": "2", + "username": "a", + "acct": "a", + "display_name": "", + "locked": false, + "created_at": 0, + "followers_count": 0, + "following_count": 0, + "statuses_count": 0, + "note": "", + "url": "https://example.com/a" + }, + "content": "", + "created_at": 0, + "emojis": [], + "reblogs_count": 0, + "favourites_count": 0, + "sensitive": false, + "spoiler_text": "", + "visibility": "public", + "media_attachments": [], + "mentions": [], + "tags": [] +} +""".data(using: .utf8)! + do { + _ = try JSONDecoder().decode(Status.self, from: data) + } catch { + print(error) + XCTFail() + } + } + +}