Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Relationships without included resources are decoded to NSNull #33

Open
shelefg opened this issue Dec 23, 2019 · 5 comments
Open

Relationships without included resources are decoded to NSNull #33

shelefg opened this issue Dec 23, 2019 · 5 comments

Comments

@shelefg
Copy link

shelefg commented Dec 23, 2019

Expected Behavior

Resources with relationships that are not included in the document should be decoded without losing the data that's specified in the relationship

Actual Behavior

The relationship key is set on the decoded dictionary, but the value is NSNull

Steps to Reproduce the Problem

The following code reproduces the issue:

let json = 
"""
{
  "data": {
    "type": "articles",
    "id": "1",
    "attributes": {
      "title": "Rails is Omakase"
    },
    "relationships": {
      "author": {
        "links": {
          "self": "http://example.com/articles/1/relationships/author",
          "related": "http://example.com/articles/1/author"
        },
        "data": {
          "type": "people",
          "id": "9"
        }
      }
    },
    "links": {
      "self": "http://example.com/articles/1"
    }
  }
}
"""

let result = try? Japx.Decoder.jsonObject(with: json.data(using: .utf8)!) // (result!["data"] as! [String: Any])["author"] is NSNull

@Truba
Copy link
Member

Truba commented Dec 23, 2019

Hi @galsh83 ,

There was an issue regarding this same thing: #18

We added support for this with flag parseNotIncludedRelationships in the JapxDecodingOptions

Hope this helps,
Happy Holidays,
VP

@shelefg
Copy link
Author

shelefg commented Dec 24, 2019

Thanks for the quick reply.

It seems that the Japx version Cocoapods resolved to install in my project is 2.0.2 because of another pod depending on an older version of Alamofire, so I didn't get the parseNotIncludedRelationships flag.I worked around this by depending on Japx without the Alamofire subspec for now, and it allowed me to integrate 2.2.0 and specify the flag.

The relationships resources now decode to pairs of type and id. However, any additional elements like meta or links that may be specified for a relationship are removed.

For the example I gave above, the decoding result is:

{
  "data": {
    "id": "1",
    "links": {
      "self": "http://example.com/articles/1"
    },
    "type": "articles",
    "author": {
      "type": "people",
      "id": "9"
    },
    "title": "Rails is Omakase"
  }
}

Thanks for your help!
Happy holidays

@shelefg
Copy link
Author

shelefg commented Jan 15, 2020

@Truba should I create a separate issue for missing meta & links?

@Truba
Copy link
Member

Truba commented Mar 3, 2020

Hi @galsh83
Sure a separate issue would be great 😄
I just have to admit, I'm a just a bit concerned on how to implement this regarding possible merge conflicts.

For example, we can have links or meta in author->relationship->article and also in included->article. For the same object of course. I could just go with always preferring one of those two links but it might yield unwanted results.

What do you think?

@Naveenkumar4screen
Copy link

Hi,
I am using objective-C code for encoding and decoding. While encoding I can't able to add included data in my request body. How i can add included the data in the request body any suggestions for that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants