tristanhimmelman/AlamofireObjectMapper

Crash when mapping with valueOrFail

Closed this issue · 3 comments

Hi first of all GREAT library here :) ....

I'm getting a weird crash when the requested key is not present in the response

public struct Response: Mappable {
    //MARK: Properties
    internal var dictionary: [String:AnyObject]
    //MARK: Mappable protocols implementation
    public init?(_ map: Map){
        self.dictionary = map["data"].valueOrFail()
        if !map.isValid {
            return nil
        }  
    }
    public mutating func mapping(map: Map) {
        dictionary    <- map["data"]
    } 
}

The response is:

{
            "login" :{ 
                "token":"451165846sdgfsgs6847653",
                "user_id":1
            }
}

It crash in Map.swift line 73

public func valueOrFail<T>() -> T {
        if let value: T = value() {
            return value
        } else {
            // Collects failed count
            failedCount++

            // Returns dummy memory as a proxy for type `T`
            let pointer = UnsafeMutablePointer<T>.alloc(0)
            pointer.dealloc(0)
            return pointer.memory
        }
    }

exactly on return pointer.memory

Thanks

I can confirm that it crash using Alamofire 3.1.0 , if you install Alamofire 3.0.1 does not crash

I have the exact same problem. When the key is not available, it crashes on this exact same line. Is there another solution to this, other than switching to an older Alamofire version?

@ed-mejia @helkarli, it seems that valueOfFail is causing the problem here... At this point in time, valueOrFail is an experimental feature in ObjectMapper so it is not guaranteed to function correctly all the time.

My recommendation would be to use supported ObjectMapper mappings (https://github.com/Hearst-DD/ObjectMapper)