Skip to content

Commit 61aa83e

Browse files
- fixed a bug in Operators left behind when removing the Generic on Mapper
- added a test for dictionaries of custom objects
1 parent d53448c commit 61aa83e

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

ObjectMapper/Core/Operators.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ public func <=<T: Mappable>(inout left: Dictionary<String, T>, right: Map) {
8484
}
8585

8686
// Optional Dictionary <String, T: Mappable>
87-
public func <=<T: Mappable, U>(inout left: Dictionary<String, T>?, right: Map) {
87+
public func <=<T: Mappable>(inout left: Dictionary<String, T>?, right: Map) {
8888
if right.mappingType == MappingType.fromJSON {
8989
FromJSON<T>().optionalObjectDictionary(&left, object: right.currentValue)
9090
} else {

ObjectMapperTests/ObjectMapperTests.swift

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,20 @@ class ObjectMapperTests: XCTestCase {
247247
}
248248
}
249249

250+
func testDictionaryOfCustomObjects(){
251+
let percentage1: Double = 0.1
252+
let percentage2: Double = 1792.41
253+
254+
let JSON = "{\"tasks\": { \"task1\": {\"taskId\":103,\"percentage\":\(percentage1)}, \"task2\": {\"taskId\":108,\"percentage\":\(percentage2)}}}"
255+
256+
let taskDict = Mapper<TaskDictionary>().map(string: JSON)
257+
if let task = taskDict.tasks?["task1"] {
258+
XCTAssertEqual(task.percentage!, percentage1, "Percentage 1 should be the same")
259+
} else {
260+
XCTAssert(false, "Dictionary not mapped")
261+
}
262+
}
263+
250264
func testDoubleParsing(){
251265
let percentage1: Double = 1792.41
252266

@@ -429,6 +443,21 @@ class Task: Mappable {
429443
}
430444
}
431445

446+
class TaskDictionary: Mappable {
447+
var test: String?
448+
var tasks: [String : Task]?
449+
450+
required init(){
451+
452+
}
453+
454+
func map(map: Map) {
455+
test <= map["test"]
456+
tasks <= map["tasks"]
457+
}
458+
}
459+
460+
432461
// Confirm that struct can conform to `Mappable`
433462
struct Student: Mappable {
434463
var name: String?

0 commit comments

Comments
 (0)