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

Related item without attributes not in includes #109

Open
theriaultnicolas opened this issue Sep 6, 2019 · 3 comments
Open

Related item without attributes not in includes #109

theriaultnicolas opened this issue Sep 6, 2019 · 3 comments

Comments

@theriaultnicolas
Copy link

I have encountered a problem where the "includes" section of the answer doesn't include one of the related item because it has no attrbutes to show. There's only the Id and Type and a (not included)child object that are populated.

{
    "data": {
        "id": "1",
        "type": "contact-info",
        "attributes": {
            "first-name": "fName",
            "last-name": "lName",
            "birth-date": "0001-01-01T00:00:00",
        },
        "relationships": {
            "emails": {
                "data": [
                    {
                        "id": "4",
                        "type": "email-address"
                    },
                    {
                        "id": "5",
                        "type": "email-address"
                    }
                ]
            }
        }
    },
   "included": [
      {
         "id": "5",
         "type": "email-address",
         "attributes": {
            "email": "[email protected]"
         }
      }
   ]
}

The email with Id 4 should look like this within the include

   "included": [
      {
         "id": "4",
         "type": "email-address"
      },
      {
         "id": "5",
         "type": "email-address",
         "attributes": {
            "email": "[email protected]"
         }
      }
   ]

At first I was thinking that repeating the information for the email Id 4 would be redundant because there's no attributes. However, I think that it should be included even if it's only the id and type to at least tell the other party (front end) that the entry exists and is empty. The current answer let the other party think that the entry was not provided and they will try to get the entry through a separate query.

@alex-davies
Copy link
Collaborator

There was a feature where we didn't include objects if there was no additional information beyond what we could put in the relationship. The reason for this was to allow creating 'stub' objects that just contain an id and type so you can reference objects in the classes without having it in the included section. Ironcially your case is the exact opposite of that, you have a stub object you want to indicate that it is a full object

A possible workaround is to explicitly define ResourceIdentifier objects in your model which will force it into the includes. The original purposes of ResourceIdentifier was to define meta specifically in the identifier section that was different from the resource objects meta, although it also happens to force the item into the includes

var root = DocumentRoot.Create(new
{
    Id = "1234",
    Type = "contact-info",
    Emails = new List<ResourceIdentifier<EmailAddress>>()
    {
        new ResourceIdentifier<EmailAddress>
        {
            Value = new EmailAddress
            {
                Id = 4
            }
        },
        new ResourceIdentifier<EmailAddress>
        {
            Value = new EmailAddress
            {
                Id = 5,
                Email = "[email protected]"
            }
        }
    }
});

@theriaultnicolas
Copy link
Author

Thank you for your answer. Unfortunately, I won't be able to use your suggestion as I use the Relationship class to define my relationships in my dto. I went that way because I needed to manage the links for the relationship objects. However, looking at the code and documentation (https://github.com/codecutout/JsonApiSerializer#links), it would seem that I could've created a "Links" property in my dto to hold the "self" and "related" links. (please correct me if I'm wrong)

I recommended to my developer to add a "dummy" property as a workaround for now while I look into this. It will force the object in the includes and won't affect the front-end as the property is not defined on that side. It's less than ideal and ugly but at least it puts a band-aid on the issue for now.

@theriaultnicolas
Copy link
Author

I know it's not directly in link with the original question in this thread but I'm not sure where else to ask.

I've been exploring/thinking about this a bit more and how would you handle the case where you want to specify links for the relationship itself when the relationship is an array?

The cases I want to cover are:

  1. The Resource (base object) has a one to many relationship but they were not requested (not in the inlcudes). I want to provide the relationship with the proper link to the requestor so that he can fetch the related items when needed
  2. The Resource (base object) has a one to many relationship and they were requested. I want to provide the relationship with the object but without any links on the relationship itself as the object were already provided. The included items will have their "self" link populated
  3. The Resource (base object) has a one to one relationship but it was not requested (not in the inlcudes). I want to provide the relationship with the proper link to the requestor so that he can fetch the related item when needed
  4. The Resource (base object) has a one to one relationship and it was requested. I want to provide the relationship with the object but without any links on the relationship itself as the object were already provided. The included items will have their "self" link populated

How do I get the serializer to distinguis between each item's "self" link and the whole array relationship "self" and "related" links in cases 1 and 2?
How do I get the serializer to distinguish between the included item "self" link and the relationship "self" and "related" links in cases 3 and 4?

I used the "compound document" section in the JsonApi standard as reference: https://jsonapi.org/format/#document-compound-documents

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

2 participants