joukevandermaas / saule

JSON API library for ASP.Net Web API 2.
https://joukevandermaas.github.io/saule
MIT License
76 stars 37 forks source link

Deserializing null relationships #123

Closed yohanmishkin closed 8 years ago

yohanmishkin commented 8 years ago

Saule is fantastic! Thanks a million for all the work. Quick question:

My application is posting the following json to a Saule resource with a relationship and the deserializer is unable to deserialize the review object because its data is null. Any thoughts

{
  "data": {
    "attributes": {
      "name": null,
      "price": 12
    },
    "relationships": {
      "review": {
        "data": null
      }
    },
    "type": "book"
  }
}

The BookResource look like this:

public class BookResource : ApiResource
{
    public BookResource()
    {
        WithId("Id");
        Attribute("Name");
        Attribute("Price");
        HasMany<ReviewResource>("Review");
    }
}

I receive an "Object reference not set to an instance of an object." error during deserialization of the book's review.

Is there a simple way to override the deserializer to a null check perhaps or am I maybe doing something blatantly wrong?

joukevandermaas commented 8 years ago

Looking at the spec, it looks like your JSON should look like this:

{
  "data": {
    "attributes": {
      "name": null,
      "price": 12
    },
    "relationships": {
      "review": null
    },
    "type": "book"
  }
}

Note that instead of the data key being null, the review key itself is null.

That said, if this JSON was generated by e.g. Ember Data, my reading of the spec might be wrong. In that case, this is a bug and should be fixed. I'm also not sure that Saule does the right thing even with this change.

yohanmishkin commented 8 years ago

Yeah, it's not immediately clear how to interpret the spec, but the JSON was generated by Ember Data.

joukevandermaas commented 8 years ago

I'm pretty pragmatic about the spec in this.. if Emer Data generates this then Saule probably needs to support it. I'd appreciate a PR fixing this issue, which is probably caused by something we're doing in the ResourceDeserializer.

yohanmishkin commented 8 years ago

Sounds good. I'll give it a shot and circle back.

joukevandermaas commented 8 years ago

I published a new pre-release with the fix (thanks to @goo32).