tristanhimmelman / ObjectMapper

Simple JSON Object mapping written in Swift
MIT License
9.16k stars 1.03k forks source link

mapping nested objects went wrong #1067

Open AbdallahJaja opened 5 years ago

AbdallahJaja commented 5 years ago

Your model:

i have this protocols and classes conforms to those protocols like below

protocols :

import ObjectMapper

public protocol People: Mappable {

    var name: String { get set }
    var age: Int { get set }
    var car: Car { get set }
}

extension People {

    public mutating func mapping(map: Map) {

        name <- map["name"]
        age <- map["age"]
        car <- map["car"]
    }
}

public protocol Car: Mappable {

    var type: String { get set }
    var year: Int { get set}
    var addons: [Addon] { get set}
}

extension Car {

    public mutating func mapping(map: Map) {

        type <- map["type"]
        year <- map["year"]
        addons <- map["addons"]
    }
}

public protocol Addon: Mappable {

    var autoStart: Bool { get set }
}

extension Addon {

    public mutating func mapping(map: Map) {

        autoStart <- map["auto_start"]
    }
}

classes :

public class People: Mappable, TestPod.People {

    public var name: String = ""
    public var age: Int = 0
    public var address: String = ""

    public var copyOfMan: TestPod.People? = nil
    public var car: TestPod.Car

    required public init?(map: Map) {

        car = Car(type: "mazda", year: 2010)
    }

    public init(name: String, age: Int, address: String, car: Car) {

        self.name = name
        self.age = age
        self.address = address
        self.car = car
        copyOfMan = self
    }
}

public class Car : Mappable, TestPod.Car {

    public var type: String = ""
    public var year: Int = 0
    public var addons: [TestPod.Addon] = []

    required public init?(map: Map) {

    }

    public init(type: String, year: Int) {

        self.type = type
        self.year = year
        addons = [Addon(autoStart: true)]
    }
}

public class Addon : Mappable, TestPod.Addon {

    public var autoStart: Bool = false

    required public init?(map: Map) {

    }

    public init(autoStart: Bool) {

        self.autoStart = autoStart
    }
}

What you did:

let hari = People(name: "Abdallah", age: 33, address: "Riyadh", car: Car(type: "Mazda", year: 2010))
        let newHari: People = Helper.testMan(hari)

then:

public class Helper {

    public static func testMan<T: People>(_ man: People) -> T {

        let string = man.toJSON()
        print(string)
        do {
            let data = try JSONSerialization.data(withJSONObject: man.toJSON(), options: [.prettyPrinted])
            print(data)
        }
        catch {
            print("error")
        }
        return T(JSON: string)!
    }
}

What you expected:

I exepected something like json string with all properties including cars and addons:

{"name":"Abdallah","age":33 .... }

What you got:

{"name":"Abdallah","age":33}

whats is wrong with this code ?

AbdallahJaja commented 5 years ago

Any help ?

yuvrajsinh-plackal commented 4 years ago

@AbdallahJaja Have you got any solution?