0

I am trying to post a contents of an entity(Articles) and its relationship(Authors) with Restkit object mapping.

Articles <->> Authors An article can have many authors.

My current object mapping is as below,

    RKObjectMapping *authorMapping = [RKObjectMapping requestMapping];
    [authorMapping addAttributeMappingsFromArray:@[@"name",@"email"]];

    RKObjectMapping *articleMapping = [RKObjectMapping requestMapping];
   [articleMapping addAttributeMappingsFromArray:@[@"title",@"body",@"date"]];

   RKRelationshipMapping * rel =[RKRelationshipMapping relationshipMappingFromKeyPath:@"author" toKeyPath:@"author" withMapping:authorMapping];
   [articleMapping addPropertyMapping:rel];

    RKObjectMapping *mapping = [RKObjectMapping requestMapping];
   [mapping addPropertyMapping:[RKRelationshipMapping relationshipMappingFromKeyPath:@"singleArticle" toKeyPath:@"singleArticle" withMapping:articleMapping]];

For this mapping I was expecting a JSON output with an array of authors as below,

  {
    "singleArticle": {
        "author": [
            {
                "email": "dadsa@",
                "name": "rk"
            },
            {
                "email": "3ldll",
                "name": "rjk"
            }
        ],
        "body": "body content",
        "date": "2014-05-16T15:54:40Z",
        "title": "some title"
    }
}

But the generated JSON Output turns out with all the fields in the author class as a separate object as below,

{
    "singleArticle": {
        "author": [
            {
                "email": "dadsa@"
            },
            {
                "name": "rk"
            },
            {
                "email": "3ldll"
            },
            {
                "name": "rjk"
            }
        ],
        "body": "body content",
        "date": "2014-05-16T15:54:40Z",
        "title": "some title"
    }
}

I am not sure how what I am missing here that messes up the JSON feed as above. Any thoughts to produce a proper JSON with an array of author objects?

  • Can you post your generated entity classes? I'm presuming you have standard dynamic properties? My first thought it's you've got some custom property accessors which are returning objects rather than basic types. But that doesn't explain why the properties of multiple entities are being grouped as a single array. Very odd! – scipilot May 17 '14 at 12:33
  • @scipilot I am using Mogenerator to generate the entity classes, do you think that would affect the output in some way. – user1332575 May 17 '14 at 13:32
  • It's isn't mogenerator at fault. It looks like you have too many managed object instances, each partially populated - verify that. – Wain May 17 '14 at 18:02
  • Tried that and can't find any issues. Also I tried making the relationship as one-to-one Articles<->Author and I was getting the expected JSON format with email and name as attributes of a single object. The splitting up of name and email as separate objects happens only in case of one-to-many relationship. – user1332575 May 17 '14 at 22:29
  • How did you check? Show the code where you create the objects and relationships and the log from your test. If it works for 1 then the mapping would seem to be correct... – Wain May 18 '14 at 08:54
  • Sorry - I don't know Mogenerator I've only done it the ol' fashioned manual way. – scipilot May 18 '14 at 09:07

0 Answers0