tristanhimmelman/AlamofireObjectMapper

Library doesn't attempt mapping when result is failure

Opened this issue · 0 comments

My API follows the standard rails convention of returning the object body back to the user, with an errors key present in the object body.

Request:

{
  "object":{
    "optional_key":"present",
    "rquired_key":"invalid_data"
  }
}

Response (422 status code):

{
  "object":{
    "optional_key":"present",
    "required_key":"invalid_data",
    "errors":{
      "required_key":[
        "Some descriptive error message"
      ]
    }
  }
}

Code:

let request = self.constructAlamofireRequest()
request.responseObject(keyPath: "something") { (response: DataResponse<SomeClass>) in
    switch response.result{
    case .success:
        if let someItem = response.result.value{
            successHandler(someItem)
        }else{
            failureHandler(nil)
        }
        break;
    case .failure:
        //Should probably handle 'unable to serialize' type errors here, haven't gotten that far
        failureHandler(response.result.value)
        break;
    }
}

Actual behavior:

Deserialization, as far as I can tell, isn't even attempted. response.result.value is nil in the case .failure even when deserialization is possible.

Expected behavior:

Even in case .failure, response.result.value should be set to a deserialize copy of SomeClass, if it's possible to deserialize. (If serialization is impossible, naturally let it fail and return the current value)