Skip to content

Commit

Permalink
ObjectProtection 增加空字串的判斷
Browse files Browse the repository at this point in the history
  • Loading branch information
Darktt committed Dec 2, 2024
1 parent 442d286 commit 41b0cfa
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 4 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ on:

jobs:
build:

runs-on: macos-latest

steps:
- uses: actions/checkout@v4
- name: Select Xcode
Expand Down
31 changes: 30 additions & 1 deletion JsonDecodeProtectionTests/ObjectProtectionTest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
}

Expand Down Expand Up @@ -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<SomeObject.SubObject>? = object.subObjects
let expect: Array<SomeObject.SubObject>? = nil

XCTAssertEqual(actual, expect)
}
}
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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"

## 功能說明

Expand Down
3 changes: 2 additions & 1 deletion SourceCode/JsonProtection/ObjectProtection.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down

0 comments on commit 41b0cfa

Please sign in to comment.