From 41b0cfa4e1ba348935203a828e410779dfdc7235 Mon Sep 17 00:00:00 2001 From: Darktt Date: Mon, 2 Dec 2024 17:04:06 +0800 Subject: [PATCH] =?UTF-8?q?ObjectProtection=20=E5=A2=9E=E5=8A=A0=E7=A9=BA?= =?UTF-8?q?=E5=AD=97=E4=B8=B2=E7=9A=84=E5=88=A4=E6=96=B7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .github/workflows/main.yml | 2 +- .../ObjectProtectionTest.swift | 31 ++++++++++++++++++- README.md | 2 +- .../JsonProtection/ObjectProtection.swift | 3 +- 4 files changed, 34 insertions(+), 4 deletions(-) diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 845df33..a7a0dab 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -7,8 +7,8 @@ on: jobs: build: - runs-on: macos-latest + steps: - uses: actions/checkout@v4 - name: Select Xcode diff --git a/JsonDecodeProtectionTests/ObjectProtectionTest.swift b/JsonDecodeProtectionTests/ObjectProtectionTest.swift index cfa8080..079daab 100644 --- a/JsonDecodeProtectionTests/ObjectProtectionTest.swift +++ b/JsonDecodeProtectionTests/ObjectProtectionTest.swift @@ -21,13 +21,19 @@ struct SomeObject: Decodable extension SomeObject { - struct SubObject: Decodable + struct SubObject: Decodable, Equatable { private(set) var name: String? private(set) var number: Int? + + static + func == (lhs: SomeObject.SubObject, rhs: SomeObject.SubObject) -> Bool + { + lhs.name == rhs.name && lhs.number == rhs.number + } } } @@ -90,4 +96,27 @@ final class ObjectProtectionTest: XCTestCase XCTAssertEqual(actual, expect) } + + func testObjectProtectionWithEmptyString() throws + { + // Arrange + self.jsonString = """ + { + "subObjects": "", + "dices": "[1, 5, 1]" + } + """ + + let jsonData: Data = self.jsonString.data(using: .utf8)! + let jsonDecoder = JSONDecoder() + + // Act + let object = try jsonDecoder.decode(SomeObject.self, from: jsonData) + + // Assert + let actual: Array? = object.subObjects + let expect: Array? = nil + + XCTAssertEqual(actual, expect) + } } diff --git a/README.md b/README.md index a22f684..dc16d65 100644 --- a/README.md +++ b/README.md @@ -10,7 +10,7 @@ * File > Swift Packages > Add Package Dependency * Add https://github.com/Darktt/JsonProtection -* Select "Up to Next Major" with "1.2.0" +* Select "Up to Next Major" with "1.2.1" ## 功能說明 diff --git a/SourceCode/JsonProtection/ObjectProtection.swift b/SourceCode/JsonProtection/ObjectProtection.swift index 8c0a5a6..6fdf9de 100644 --- a/SourceCode/JsonProtection/ObjectProtection.swift +++ b/SourceCode/JsonProtection/ObjectProtection.swift @@ -88,7 +88,8 @@ extension ObjectProtection { func decode(with string: String) throws -> Value? { - guard let data: Data = string.data(using: .utf8) else { + guard !string.isEmpty, + let data: Data = string.data(using: .utf8) else { return nil }