From 8025e547dcceb39f4e5f99b19e6a413bb907f034 Mon Sep 17 00:00:00 2001 From: zhaixian Date: Tue, 4 Jun 2024 14:48:06 +0800 Subject: [PATCH] Enhance robustness of AliyunpanFile JSON decoding (#60) * feat: enhance robustness of AliyunpanFile JSON decoding * Bump version to 0.3.4 --- AliyunpanSDK.podspec | 2 +- Sources/AliyunpanSDK/AliyunpanSDK.swift | 2 +- Sources/AliyunpanSDK/Info.plist | 2 +- .../AliyunpanSDK/Model/AliyunpanFile.swift | 47 ++++++++++++++++- Tests/AliyunpanSDKTests/JSONTest.swift | 50 +++++++++++++++++++ 5 files changed, 98 insertions(+), 5 deletions(-) diff --git a/AliyunpanSDK.podspec b/AliyunpanSDK.podspec index bf5809d..d50fbc8 100644 --- a/AliyunpanSDK.podspec +++ b/AliyunpanSDK.podspec @@ -1,6 +1,6 @@ Pod::Spec.new do |spec| spec.name = "AliyunpanSDK" - spec.version = "0.3.3" + spec.version = "0.3.4" spec.summary = "Aliyunpan OpenSDK-iOS" spec.description = <<-DESC diff --git a/Sources/AliyunpanSDK/AliyunpanSDK.swift b/Sources/AliyunpanSDK/AliyunpanSDK.swift index c9866ff..5b8a640 100644 --- a/Sources/AliyunpanSDK/AliyunpanSDK.swift +++ b/Sources/AliyunpanSDK/AliyunpanSDK.swift @@ -52,4 +52,4 @@ public class Aliyunpan { } } -let version = "0.3.3" +let version = "0.3.4" diff --git a/Sources/AliyunpanSDK/Info.plist b/Sources/AliyunpanSDK/Info.plist index 7c43e29..35364a7 100644 --- a/Sources/AliyunpanSDK/Info.plist +++ b/Sources/AliyunpanSDK/Info.plist @@ -15,7 +15,7 @@ CFBundlePackageType FMWK CFBundleShortVersionString - 0.3.3 + 0.3.4 CFBundleSignature ???? CFBundleVersion diff --git a/Sources/AliyunpanSDK/Model/AliyunpanFile.swift b/Sources/AliyunpanSDK/Model/AliyunpanFile.swift index ed66b4a..d00cac3 100644 --- a/Sources/AliyunpanSDK/Model/AliyunpanFile.swift +++ b/Sources/AliyunpanSDK/Model/AliyunpanFile.swift @@ -18,7 +18,7 @@ public struct AliyunpanFile: Codable { public let file_extension: String? /// 文件 hash public let content_hash: String? - public var category: FileCategory? + public let category: FileCategory? public let type: FileType? public let mime_type: String? /// 缩略图 @@ -34,7 +34,50 @@ public struct AliyunpanFile: Codable { /// 视频信息 public let video_media_metadata: MediaMetadata? /// 视频预览信息 - public var video_preview_metadata: AudioMetaData? + public let video_preview_metadata: AudioMetaData? + + init(drive_id: String, file_id: String, parent_file_id: String, name: String, size: Int64?, file_extension: String?, content_hash: String?, category: FileCategory? = nil, type: FileType?, mime_type: String?, thumbnail: URL?, url: URL?, created_at: Date?, updated_at: Date?, play_cursor: String?, image_media_metadata: MediaMetadata?, video_media_metadata: MediaMetadata?, video_preview_metadata: AudioMetaData? = nil) { + self.drive_id = drive_id + self.file_id = file_id + self.parent_file_id = parent_file_id + self.name = name + self.size = size + self.file_extension = file_extension + self.content_hash = content_hash + self.category = category + self.type = type + self.mime_type = mime_type + self.thumbnail = thumbnail + self.url = url + self.created_at = created_at + self.updated_at = updated_at + self.play_cursor = play_cursor + self.image_media_metadata = image_media_metadata + self.video_media_metadata = video_media_metadata + self.video_preview_metadata = video_preview_metadata + } + + public init(from decoder: Decoder) throws { + let container = try decoder.container(keyedBy: CodingKeys.self) + self.drive_id = try container.decode(String.self, forKey: .drive_id) + self.file_id = try container.decode(String.self, forKey: .file_id) + self.parent_file_id = try container.decode(String.self, forKey: .parent_file_id) + self.name = try container.decode(String.self, forKey: .name) + self.size = try? container.decodeIfPresent(Int64.self, forKey: .size) + self.file_extension = try? container.decodeIfPresent(String.self, forKey: .file_extension) + self.content_hash = try? container.decodeIfPresent(String.self, forKey: .content_hash) + self.category = try? container.decodeIfPresent(AliyunpanFile.FileCategory.self, forKey: .category) + self.type = try? container.decodeIfPresent(AliyunpanFile.FileType.self, forKey: .type) + self.mime_type = try? container.decodeIfPresent(String.self, forKey: .mime_type) + self.thumbnail = try? container.decodeIfPresent(URL.self, forKey: .thumbnail) + self.url = try? container.decodeIfPresent(URL.self, forKey: .url) + self.created_at = try? container.decodeIfPresent(Date.self, forKey: .created_at) + self.updated_at = try? container.decodeIfPresent(Date.self, forKey: .updated_at) + self.play_cursor = try container.decodeIfPresent(String.self, forKey: .play_cursor) + self.image_media_metadata = try? container.decodeIfPresent(AliyunpanFile.MediaMetadata.self, forKey: .image_media_metadata) + self.video_media_metadata = try? container.decodeIfPresent(AliyunpanFile.MediaMetadata.self, forKey: .video_media_metadata) + self.video_preview_metadata = try? container.decodeIfPresent(AliyunpanFile.AudioMetaData.self, forKey: .video_preview_metadata) + } } extension AliyunpanFile: CustomStringConvertible { diff --git a/Tests/AliyunpanSDKTests/JSONTest.swift b/Tests/AliyunpanSDKTests/JSONTest.swift index 3edb0ab..983ca7d 100644 --- a/Tests/AliyunpanSDKTests/JSONTest.swift +++ b/Tests/AliyunpanSDKTests/JSONTest.swift @@ -129,4 +129,54 @@ class JSONTests: XCTestCase { ISO8601DateFormatter().date(from: json["updated_at"]!), ISO8601DateFormatter().date(from: "2023-06-28T20:00:20.022+08:00")) } + + func testNilArg() throws { + let json = """ +{ + "trashed": null, + "name": "music.mp3", + "thumbnail": null, + "type": "filexx", + "category": "audioxx", + "hidden": false, + "status": "available", + "description": null, + "meta": null, + "url": "https://stg111-enet.cn-shanghai.data.alicloudccp.com", + "size": 561701, + "starred": false, + "location": null, + "deleted": null, + "channel": null, + "user_tags": null, + "mime_type": "audio/mpeg", + "parent_file_id": "root", + "drive_id": "drive_id1", + "file_id": "file_id1", + "file_extension": "mp3", + "revision_id": null, + "content_hash": "content_hash1", + "content_hash_name": "sha1", + "encrypt_mode": "none", + "domain_id": "stg111", + "user_meta": null, + "content_type": null, + "created_at": "2023-06-28", + "updated_at": "2023-06-28T12:00:20.022Z", + "local_created_at": null, + "local_modified_at": null, + "trashed_at": null, + "punish_flag": 0, + "video_media_metadata": null, + "image_media_metadata": null, + "play_cursor": null, + "video_preview_metadata": null, + "streams_info": null +} +""" + let file = try JSONParameterDecoder().decode(AliyunpanFile.self, from: json.data(using: .utf8)!) + XCTAssertNil(file.created_at) + XCTAssertNil(file.category) + XCTAssertNil(file.type) + } }