jsonapi-converter icon indicating copy to clipboard operation
jsonapi-converter copied to clipboard

Circular relationships involving the `data` resource not being resolved properly during deserialization

Open soulporpoise opened this issue 9 years ago • 4 comments

Hi @jasminb! I'm seeing an issue with circular relationships, perhaps similar to https://github.com/jasminb/jsonapi-converter/issues/100, but regarding deserialization. Given models like:

@Type("user")
public class User {
  @Id
  public String id;
  public String name;
  @Relationship("campaign")
  public Campaign campaign;
}

@Type("campaign")
public class Campaign {
  @Id
  public String id;
  public String title;
  @Relationship("owner")
  public User owner;
}

and the following JSON from my API request:

{
  "data": {
    "attributes": {
      "name": "Cassidy"
    },
    "id": "123",
    "relationships": {
      "campaign": {
        "data": {
          "id": "456",
          "type": "campaign"
        }
      }
    },
    "type": "user"
  },
  "included": [
    {
      "attributes": {
        "title": "My Campaign"
      },
      "id": "456",
      "relationships": {
        "owner": {
          "data": {
            "id": "123",
            "type": "user"
          }
        }
      },
      "type": "campaign"
    }
  ]
}

When I execute the following:

User user = new ResourceConverter(BuildConfig.API_BASE_URL, User.class, Campaign.class)
        .readDocument(json.getBytes(), User.class)
        .get();

I end up with user.campaign.owner being an empty object (only the id field is populated). I would expect user == user.campaign.owner to be true in this case, but they are actually separate objects when I check them in the debugger.

However, this issue only appears to happen when the data resource is part of the circular relationship. If there is a circular relationship amongst the included resources, the deserialization handles this well and works perfectly (i.e. user.campaign.owner is populated with all fields, and is actually the same object in memory as user).

Thanks for the help! Hopefully I'm just doing something silly that can resolve this issue. Just started using the library this week and am loving the ease of use so far.

soulporpoise avatar Jan 21 '17 01:01 soulporpoise

Hey,

Thanks for reporting and providing steps to reproduce. Will take a look and fix it.

jasminb avatar Jan 26 '17 22:01 jasminb

@jasminb Any update on this? Any way I can be helpful?

soulporpoise avatar Feb 18 '17 06:02 soulporpoise

Hey @crcsaenz,

Will try to fix it soon, its not a simple fix due to how caching/parsing is done.

jasminb avatar Feb 18 '17 15:02 jasminb

@jasminb I've submitted a pull request that fixes this issue #129

soulporpoise avatar Jun 19 '17 20:06 soulporpoise