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 }