Skip to content
This repository has been archived by the owner on Jun 18, 2019. It is now read-only.

Commit

Permalink
Merge pull request #210 from ivasic/master
Browse files Browse the repository at this point in the history
Fixes unboxing with keypaths that contain keys identical to the first one
  • Loading branch information
JohnSundell authored Jan 17, 2018
2 parents 0f43982 + 2cb4de8 commit 5b825e2
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 3 deletions.
5 changes: 2 additions & 3 deletions Sources/Unboxer.swift
Original file line number Diff line number Diff line change
Expand Up @@ -224,14 +224,13 @@ private extension Unboxer {
case .keyPath(let keyPath):
var node: UnboxPathNode = self.dictionary
let components = keyPath.components(separatedBy: ".")
let lastKey = components.last

for key in components {
for (index, key) in components.enumerated() {
guard let nextValue = node.unboxPathValue(forKey: key) else {
throw UnboxPathError.missingKey(key)
}

if key == lastKey {
if index == components.index(before: components.endIndex) {
return try transform(nextValue).orThrow(UnboxPathError.invalidValue(nextValue, key))
}

Expand Down
25 changes: 25 additions & 0 deletions Tests/UnboxTests/UnboxTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1744,6 +1744,31 @@ class UnboxTests: XCTestCase {
XCTFail("\(error)")
}
}

func testKeypathsWithSameKeys() {
struct Model: Unboxable {
let value: String

init(unboxer: Unboxer) throws {
self.value = try unboxer.unbox(keyPath: "key.nested.key")
}
}

let dictionary: UnboxableDictionary = [
"key": [
"nested": [
"key": "value"
]
]
]

do {
let model: Model = try unbox(dictionary: dictionary)
XCTAssertEqual(model.value, "value")
} catch {
XCTFail("\(error)")
}
}
}

private func UnboxTestDictionaryWithAllRequiredKeysWithValidValues(nested: Bool) -> UnboxableDictionary {
Expand Down

0 comments on commit 5b825e2

Please sign in to comment.