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

relationship meta when relationship is a collection

Open uddyami opened this issue 8 years ago • 5 comments

{
  "data": {
    "type": "people",
    "id": "9",
    "attributes": {
      "first-name": "Dan",
      "last-name": "Gebhardt",
      "twitter": "dgeb"
    },
    "links": {
      "self": "http://example.com/people/9"
    },
    "relationships":{
      "articles":[
        {
          "type": "articles",
          "id": "1",
          "meta":{
            "message":"123 testing"
          }
        },
        {
          "type": "articles",
          "id": "2",
          "meta":{
            "message":"bad testing"
          }
        }
      ]
    }
  }
}

how do i access message ???

uddyami avatar Apr 17 '17 16:04 uddyami

Hey @uddyami ,

Did you try creating a class:

public class ArticleMeta {
private String message;

// getters and setters
}

And than add it as a property to the Article type and annotate it with @Meta

jasminb avatar Apr 20 '17 22:04 jasminb

The problem is its not a meta on the object but in that relationship. If same article is in relationship with another person this breaks the idea

uddyami avatar Apr 27 '17 14:04 uddyami

Hello @uddyami,

It seams that relationships object that you provided as an example is invalid, it should look like this:

{
  "data": {
    "type": "people",
    "id": "9",
    "attributes": {
      "first-name": "Dan",
      "last-name": "Gebhardt",
      "twitter": "dgeb"
    },
    "links": {
      "self": "http://example.com/people/9"
    },
    "relationships": {
      "articles": {
        "data": [
          {
            "type": "articles",
            "id": "1",
            "meta": {
              "message": "Resource meta"
            }
          },
          {
            "type": "articles",
            "id": "2",
            "meta": {
              "message": "Resource meta"
            }
          }
        ],
        "meta": {
          "message": "This would be relationship meta"
        }
      }
    }
  }
}

Please see this example on the spec: http://jsonapi.org/format/#document-compound-documents

To access relationship meta, define an attribute with RelationshipMeta and set its value to the name of the relationship, in this case articles.

jasminb avatar Apr 27 '17 16:04 jasminb

Hey @jasminb To be more specific, what @uddyami meant is that we having problems with deserializing following response:

{
  "data": {
    "type": "people",
    "id": "9",
    "attributes": {
      "first-name": "Dan",
      "last-name": "Gebhardt",
      "twitter": "dgeb"
    },
    "relationships":{
      "publishers": {
        "data": [
          {
            "type": "publisher",
            "id": "id1"
          },
          {
            "type": "publisher",
            "id": "id2"
          }
        ]
      }
    },
    "included": [
      {
        "type": "articles",
        "id": "1",
        "attributes": {
          "article_name": "Name1"
        }
      },
      {
        "type": "articles",
        "id": "2",
        "attributes": {
          "article_name": "Name2"
        }
      },
      {
        "type": "articles",
        "id": "3",
        "attributes": {
          "article_name": "Name3"
        }
      },
      {
        "type": "articles",
        "id": "4",
        "attributes": {
          "article_name": "Name4"
        }
      },
      {
        "type": "publisher",
        "id": "id1",
        "relationships":{
          "articles": {
            "data": [
              {
                "type": "articles",
                "id": "1",
                "meta": {
                  "message": "Some message"
                }
              },
              {
                "type": "articles",
                "id": "2",
                "meta": {
                  "message": "Another message"
                }
              }
            ]
          }
        }
      },
      {
        "type": "publisher",
        "id": "id2",
        "relationships":{
          "articles": {
            "data": [
              {
                "type": "articles",
                "id": "3",
                "meta": {
                  "message": "Some message"
                }
              },
              {
                "type": "articles",
                "id": "4",
                "meta": {
                  "message": "Another message"
                }
              }
            ]
          }
        }
      }
    ]
  }
}

So seems like your lib replaces Resource Identifier Object "articles" inside Publisher relationships with full Article model from "included". BUT it ignores and doesn't merges meta block of Resource Identifier Object into Article model. But according to JSON API Specs we can do such thing (http://jsonapi.org/format/#document-resource-identifier-objects) - "A “resource identifier object” MAY also include a meta member, whose value is a meta object that contains non-standard meta-information."

We will be glad to have a fix for that or workaround suggestions (without changing response format). Thanks for your help.

kowalski89 avatar Apr 28 '17 12:04 kowalski89

Hey @kowalski89,

Sadly, there is no handling for the meta on the identifier object.

Just few days ago, I've introduced support for custom identifier object, maybe it could be extended to support meta on identifiers also. Let me think about it some more.

If you guys have ideas, please do not hesitate to write them here so we can discuss and move this forward.

Regards

jasminb avatar Apr 28 '17 13:04 jasminb