AlamofireObjectMapper icon indicating copy to clipboard operation
AlamofireObjectMapper copied to clipboard

AlamofireObjectMapper Skips Nested Dictionaries

Open brianramirez opened this issue 8 years ago • 3 comments

I have an issue with the latest release of AlamofireObjectMapper. I have an API that returns an array of JSON data and each item has 3 nested dictionaries.

I am using the ObjectMapper+Realm extension as well to map the JSON to be stored inside a realm database.

The JSON is correctly parsed, however the nested dictionaries are completely skipped despite being returned from the API correctly.

What might I be doing wrong?

This is what my Alamofire request look like:

_ = Alamofire.request(request).responseArray { (response: DataResponse<[Template]>) in { }

My JSON is structured like this:

[
    {
        "TemplateName": "Test Template",
        "TestMode": false,
        "StartDate": "2017-07-01T00:00:00+00:00",
        "EndDate": "2050-07-01T00:00:00+00:00",
        "LastModified": "2017-07-12T17:28:20.369+00:00",
        "Category": "Marketing",
        "IsActive": true,
        "CountryList": [
            "US",
            "CA"
        ],
        "TemplateImage": {
            "Width": 1024,
            "Height": 1024,
            "Url": "#",
            "ThumbnailUrl": "#"
        },
        "ExtraData": {},
        "AutoDate": {}
    }
]

My Model looks like this:

import Foundation
import ObjectMapper
import RealmSwift
import ObjectMapper_Realm

final class Template: Object, Mappable {
    
    @objc dynamic var templateName: String = ""
    @objc dynamic var testMode: Bool = false
    @objc dynamic var startDate: String = ""
    @objc dynamic var endDate: String = ""
    @objc dynamic var lastModified: String = ""
    @objc dynamic var isActive: Bool = false
    var templateImage: List<TemplateImage>?
    var extraData: List<ExtraData>?
    var autoDate: List<AutoDate>?
    
    override static func primaryKey() -> String {
        return "templateName"
    }
    
    required convenience init?(map: Map) {
        self.init()
    }
    
    func mapping(map: Map) {
        templateName <- map["TemplateName"]
        testMode <- map["TestMode"]
        startDate <- map["StartDate"]
        endDate <- map["EndDate"]
        lastModified <- map["LastModified"]
        isActive <- map["IsActive"]
        templateImage <- (map["TemplateImage"], ListTransform<TemplateImage>())
        extraData <- (map["ExtraData"], ListTransform<ExtraData>())
        autoDate <- (map["AutoDate"], ListTransform<AutoDate>())
    }

I have even tried adding the <- map["TemplateImage", nested: true] to the model mapping to no avail.

brianramirez avatar Apr 16 '18 23:04 brianramirez

@brianramirez Have you tried @objc dynamic var templateImage: TemplateImage? as it doesn't look to be a collection in the JSON payload.

I may be having a similar issue but need to diagnose further as the Realm model has been created but doesn't include any of the headers when looking via realm studio. Which is a bit strange.. so if you have found anything else about this it will be helpful to know.

christoff-1992 avatar Jun 20 '18 08:06 christoff-1992

seeing this for attributes that are Protocols instead of explicit class / struct types.

daviskoh avatar Jul 28 '18 21:07 daviskoh

Did anyone find a solution for this?

SigmundRakes avatar Nov 30 '18 20:11 SigmundRakes