0
votes

I'm trying to map an API result that has dynamic nested attributes. The result (json) is I have is this:

{
    "url": "https://api.github.com/gists/8901308",
    "id": "8901308",
    "html_url": "https://gist.github.com/8901308",
    "files": {
        "node-and-npm-in-30-seconds.sh": {
            "filename": "node-and-npm-in-30-seconds.sh",
            "type": "application/sh",
            "language": "Shell",
            "raw_url": "https://gist.github.com/braincrash/8901308/raw/bae861f7c4ab0c1ffd9962439e770b02f52c5dd7/node-and-npm-in-30-seconds.sh",
            "size": 352
        },
        "only-git-all-the-way.sh": {
            "filename": "only-git-all-the-way.sh",
            "type": "application/sh",
            "language": "Shell",
            "raw_url": "https://gist.github.com/braincrash/8901308/raw/eba9667b37218ffb41892411c94abd051b0e269a/only-git-all-the-way.sh",
            "size": 440
        }
    }
}

I can get all the attributes, but the files won't work. Here's my mapping:

RKEntityMapping *fileMapping = [RKEntityMapping mappingForEntityForName:@"File" inManagedObjectStore:managedObjectStore];

fileMapping.forceCollectionMapping = YES;
[fileMapping addAttributeMappingFromKeyOfRepresentationToAttribute:@"filename"];
[fileMapping addAttributeMappingsFromDictionary:@{@"(filename).raw_url": @"rawURL",
                                                  @"(filename).size": @"size"}];

RKEntityMapping *gistMapping = [RKEntityMapping mappingForEntityForName:@"Gist" inManagedObjectStore:managedObjectStore];
[gistMapping addAttributeMappingsFromDictionary:@{
                                                  @"id":             @"gistID",
                                                  @"url":            @"jsonURL",
                                                  @"html_url":            @"htmlURL"}];
gistMapping.identificationAttributes = @[ @"gistID" ];

[gistMapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"files" toKeyPath:@"files" withMapping:fileMapping]];

RKResponseDescriptor *responseDescriptor = [RKResponseDescriptor responseDescriptorWithMapping:gistMapping method:RKRequestMethodGET pathPattern:@"/gists/public" keyPath:nil statusCodes:RKStatusCodeIndexSetForClass(RKStatusCodeClassSuccessful)];

When I look at the File inside the Gist object, I only get the filename, but not the url nor size:

filename = "radiation.js";
gist = "0xb9742f0 <x-coredata://D37E442D-45BA-4A0E-B7A5-A349F75FA362/Gist/p21>";
rawURL = nil;
size = 0;

Thanks for the help!!

1
Probably because your file names contain dots so they will mess up key path navigation. Turn on trace logging to check. Do you have the option to change the JSON? - Wain
You are wright. I found a key without the dots and worked fine. Yo can add an answer with that and I'll mark it as correct. Do you know any workaround to this problem? Thanks - amcastror

1 Answers

0
votes

Your file names contain dots so they will mess up key path navigation.

RestKit does struggle with this a little and I don't think there is currently a solution. Some have been proposed (https://github.com/RestKit/RestKit/pull/1541/files) but I think it remains an open issue.

The problem is how to know whether a dot represents a key path to the traversed or not. In theory it is possible to determine but it isn't necessarily trivial to implement in a framework or performant. You may be able to debug around the usage of RKObjectMappingNestingAttributeKeyName and add a custom solution just for that part.